Linux基础命令四,索引的构建是在系统较


Linux基础命令四


目录
  • Linux基础命令四
    • 如何获取命令帮助
    • Linux常用命令(续)
    • 文件查找
    • 文件层级系统
    • 重定向与管道
    • bash字符串处理

如何获取命令帮助

内部命令

[root@lnh ~]# help pwd
pwd: pwd [-LP]
    Print the name of the current working directory.
    
    Options:
      -L	print the value of $PWD if it names the current working
    		directory
      -P	print the physical directory, without any symbolic links
    
    By default, `pwd' behaves as if `-L' were specified.
    
    Exit Status:
    Returns 0 unless an invalid option is given or the current directory
    cannot be read.
//因为内部命令比较少,所以一般不使用这种方法

外部命令

[root@lnh ~]# ls --help 
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print C-style escapes for nongraphic characters
      --block-size=SIZE      with -l, scale sizes by SIZE when printing them;
                               e.g., '--block-size=M'; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information);
                               with -l: show ctime and sort by name;
                               otherwise: sort by ctime, newest first
  -C                         list entries by columns
      --color[=WHEN]         colorize the output; WHEN can be 'always' (default
                               if omitted), 'auto', or 'never'; more info below
  -d, --directory            list directories themselves, not their contents
  -D, --dired                generate output designed for Emacs' dired mode
  -f                         do not sort, enable -aU, disable -ls --color
  -F, --classify             append indicator (one of */=>@|) to entries
      --file-type            likewise, except do not append '*'
      --format=WORD          across -x, commas -m, horizontal -x, long -l,
                               single-column -1, verbose -l, vertical -C
      --full-time            like -l --time-style=full-iso
  -g                         like -l, but do not list owner
      --group-directories-first
                             group directories before files;
                               can be augmented with a --sort option, but any
                               use of --sort=none (-U) disables grouping
  -G, --no-group             in a long listing, don't print group names
  -h, --human-readable       with -l and -s, print sizes like 1K 234M 2G etc.
      --si                   likewise, but use powers of 1000 not 1024
  -H, --dereference-command-line
                             follow symbolic links listed on the command line
      --dereference-command-line-symlink-to-dir
                             follow each command line symbolic link
                               that points to a directory
      --hide=PATTERN         do not list implied entries matching shell PATTERN
                               (overridden by -a or -A)
      --hyperlink[=WHEN]     hyperlink file names; WHEN can be 'always'
                               (default if omitted), 'auto', or 'never'
      --indicator-style=WORD  append indicator with style WORD to entry names:
                               none (default), slash (-p),
                               file-type (--file-type), classify (-F)
  -i, --inode                print the index number of each file
  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN
  -k, --kibibytes            default to 1024-byte blocks for disk usage;
                               used only with -s and per directory totals
  -l                         use a long listing format
  -L, --dereference          when showing file information for a symbolic
                               link, show information for the file the link
                               references rather than for the link itself
  -m                         fill width with a comma separated list of entries
  -n, --numeric-uid-gid      like -l, but list numeric user and group IDs
  -N, --literal              print entry names without quoting
  -o                         like -l, but do not list group information
  -p, --indicator-style=slash
                             append / indicator to directories
  -q, --hide-control-chars   print ? instead of nongraphic characters
      --show-control-chars   show nongraphic characters as-is (the default,
                               unless program is 'ls' and output is a terminal)
  -Q, --quote-name           enclose entry names in double quotes
      --quoting-style=WORD   use quoting style WORD for entry names:
                               literal, locale, shell, shell-always,
                               shell-escape, shell-escape-always, c, escape
                               (overrides QUOTING_STYLE environment variable)
  -r, --reverse              reverse order while sorting
  -R, --recursive            list subdirectories recursively
  -s, --size                 print the allocated size of each file, in blocks
  -S                         sort by file size, largest first
      --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
                               time (-t), version (-v), extension (-X)
      --time=WORD            with -l, show time as WORD instead of default
                               modification time: atime or access or use (-u);
                               ctime or status (-c); also use specified time
                               as sort key if --sort=time (newest first)
      --time-style=TIME_STYLE  time/date format with -l; see TIME_STYLE below
  -t                         sort by modification time, newest first
  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
  -u                         with -lt: sort by, and show, access time;
                               with -l: show access time and sort by name;
                               otherwise: sort by access time, newest first
  -U                         do not sort; list entries in directory order
  -v                         natural sort of (version) numbers within text
  -w, --width=COLS           set output width to COLS.  0 means no limit
  -x                         list entries by lines instead of by columns
  -X                         sort alphabetically by entry extension
  -Z, --context              print any security context of each file
  -1                         list one file per line.  Avoid '\n' with -q or -b
      --help     display this help and exit
      --version  output version information and exit

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).

The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT.
FORMAT is interpreted like in date(1).  If FORMAT is FORMAT1<newline>FORMAT2,
then FORMAT1 applies to non-recent files and FORMAT2 to recent files.
TIME_STYLE prefixed with 'posix-' takes effect only outside the POSIX locale.
Also the TIME_STYLE environment variable sets the default style to use.

Using color to distinguish file types is disabled both by default and
with --color=never.  With --color=auto, ls emits color codes only when
standard output is connected to a terminal.  The LS_COLORS environment
variable can change the settings.  Use the dircolors command to set it.

Exit status:
 0  if OK,
 1  if minor problems (e.g., cannot access subdirectory),
 2  if serious trouble (e.g., cannot access command-line argument).

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/ls>
or available locally via: info '(coreutils) ls invocation'
[root@lnh ~]# pwd --help
pwd: pwd [-LP]
    Print the name of the current working directory.
    
    Options:
      -L	print the value of $PWD if it names the current working
    		directory
      -P	print the physical directory, without any symbolic links
    
    By default, `pwd' behaves as if `-L' were specified.
    
    Exit Status:
    Returns 0 unless an invalid option is given or the current directory
    cannot be read.
//虽然这个是查看外部命令的帮助,但是内部命令也可以查看,我们一般都是使用这种方法来查看帮助,实在查看不了的才是用上面那个进行查看帮助

info COMMAND 在线文档(查看要查看的那个命令信息)
man COMMAND命令手册(一般都是用这个命令查看需要的命令帮助)
whatis COMMAND(查看命令在man文档里面的第几章)

[root@lnh ~]# whatis ls
ls (1)               - list directory contents
[root@lnh ~]# ls
anaconda-ks.cfg  passwd  test  tushanbu  xxb
[root@lnh ~]# whatis /usr/bin/passwd
openssl-passwd (1ssl) - compute password hashes
passwd (1)           - update user's authentication tokens
[root@lnh ~]# whatis passwd
openssl-passwd (1ssl) - compute password hashes
passwd (1)           - update user's authentication tokens
//可以查看在man文档里面哪一章节,可以直接在后面接关键字查看

man是分章节的,以下是每一章节的内容介绍

1 用户命令(/bin,/usr/bin,/usr/local/bin)
2 系统调用
3 库调用
4 特殊文件(设备文件)
5 文件格式(配置文件 的语法)
6 游戏
7 杂项(Miscellaneous)
8 管理命令(/sbin,/usr/sbin,/usr/local/sbin)

man手册注意事项:

[] 可选
<> 必选
... 可以出现多次
"管道符" 多选一
{} 分组
NAME 命令名称及功能简要说明
SYNOPSIS 用法说明,包括可用的选项
DESCRIPTION 命令功能的详尽说明,可能包括每一个选项的意义
OPTIONS 说明每一个选项的意义
FILES 此命令相关的配置文件
BUGS 报告bug
EXAMPLES 使用示例
SEE ALSO 另外参照

man翻屏:

向后翻一屏 SPACE
向前翻一屏 b
向后翻一行 enter
向前翻一行 k

查找:

/KEYWORD 向后
?KEYWORD 向前
n 下一个
N 前一个
q 退出

文件命名规则
1.长度不能超过255个字符;
2.不能使用/当文件名;
3.严格区分大小写

Linux常用命令(续)

文本查找 grep egrep fgrep
grep在过滤的时候会把你需要的那个关键字所在的那一行全部打印出来

[root@lnh ~]# ls
anaconda-ks.cfg  test  tushanbu  xxb
[root@lnh ~]# grep timezone anaconda-ks.cfg 
# System timezone
timezone Asia/Shanghai --isUtc --nontp
//以这个文件为例可以看见包括这个关键字所在的行全部打印出来了

-o只打印出那个关键字

[root@lnh ~]# grep -o timezone anaconda-ks.cfg 
timezone
timezone

-i忽略大小写打印出来

[root@lnh ~]# grep u anaconda-ks.cfg 
# Keyboard layouts
keyboard --xlayouts='us'
# System language
network  --bootproto=dhcp --device=ens33 --ipv6=auto --activate
# Run the Setup Agent on first boot
ignoredisk --only-use=sda
autopart
rootpw --iscrypted $6$V1klyt8LakL.Q9IE$YkxB.X16Ya3ljFYQILaSZRpfaJQ7gARtCLgBGf1rNeRT48PKEPq1t/n9sE1w7dmWIk0oAGWSuqgfwpYln9D3t/
%addon com_redhat_kdump --enable --reserve-mb='auto'
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
[root@lnh ~]# grep U anaconda-ks.cfg 
# Use graphical install
lang en_US.UTF-8
# Use CDROM installation media
timezone Asia/Shanghai --isUtc --nontp
[root@lnh ~]# grep -i u anaconda-ks.cfg 
# Use graphical install
# Keyboard layouts
keyboard --xlayouts='us'
# System language
lang en_US.UTF-8
network  --bootproto=dhcp --device=ens33 --ipv6=auto --activate
# Use CDROM installation media
# Run the Setup Agent on first boot
ignoredisk --only-use=sda
autopart
timezone Asia/Shanghai --isUtc --nontp
rootpw --iscrypted $6$V1klyt8LakL.Q9IE$YkxB.X16Ya3ljFYQILaSZRpfaJQ7gARtCLgBGf1rNeRT48PKEPq1t/n9sE1w7dmWIk0oAGWSuqgfwpYln9D3t/
%addon com_redhat_kdump --enable --reserve-mb='auto'
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty

--color匹配到的内容高亮显示

[root@lnh ~]# alias 
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot'
alias xzegrep='xzegrep --color=auto'
alias xzfgrep='xzfgrep --color=auto'
alias xzgrep='xzgrep --color=auto'
alias zegrep='zegrep --color=auto'
alias zfgrep='zfgrep --color=auto'
alias zgrep='zgrep --color=auto'
root@lnh ~]# which grep
alias grep='grep --color=auto'
	/usr/bin/grep
//可以看到grep是一个别名自带这个命令,可以进行设置别名或者设置环境变量

-v不显示你需要的那一行,其他行全部显示

[root@lnh ~]# cat anaconda-ks.cfg 
#version=RHEL8
# Use graphical install
graphical


%packages
^minimal-environment
kexec-tools

%end

# Keyboard layouts
keyboard --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=dhcp --device=ens33 --ipv6=auto --activate
network  --hostname=localhost.localdomain

# Use CDROM installation media
cdrom

# Run the Setup Agent on first boot
firstboot --enable

ignoredisk --only-use=sda
autopart
# Partition clearing information
clearpart --none --initlabel

# System timezone
timezone Asia/Shanghai --isUtc --nontp

# Root password
rootpw --iscrypted $6$V1klyt8LakL.Q9IE$YkxB.X16Ya3ljFYQILaSZRpfaJQ7gARtCLgBGf1rNeRT48PKEPq1t/n9sE1w7dmWIk0oAGWSuqgfwpYln9D3t/

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
//假如我们要查看password那一行
[root@lnh ~]# grep password anaconda-ks.cfg 
# Root password
[root@lnh ~]# grep -v password anaconda-ks.cfg 
#version=RHEL8
# Use graphical install
graphical


%packages
^minimal-environment
kexec-tools

%end

# Keyboard layouts
keyboard --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=dhcp --device=ens33 --ipv6=auto --activate
network  --hostname=localhost.localdomain

# Use CDROM installation media
cdrom

# Run the Setup Agent on first boot
firstboot --enable

ignoredisk --only-use=sda
autopart
# Partition clearing information
clearpart --none --initlabel

# System timezone
timezone Asia/Shanghai --isUtc --nontp

rootpw --iscrypted $6$V1klyt8LakL.Q9IE$YkxB.X16Ya3ljFYQILaSZRpfaJQ7gARtCLgBGf1rNeRT48PKEPq1t/n9sE1w7dmWIk0oAGWSuqgfwpYln9D3t/

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
//不显示passwd那一行

-q静默模式,不输出任何信息

[root@lnh ~]# grep  password anaconda-ks.cfg 
# Root password
[root@lnh ~]# grep -q  password anaconda-ks.cfg 
[root@lnh ~]# 
//用于后面我们执行脚本时所用的

-A打印出所需行的后一行

[root@lnh ~]# grep -q  password anaconda-ks.cfg 
[root@lnh ~]# grep keyboard anaconda-ks.cfg 
keyboard --xlayouts='us'
[root@lnh ~]# grep -A 1  keyboard anaconda-ks.cfg 
keyboard --xlayouts='us'
# System language
//a:after,打印出需要的那一行的后一行

-B打印出所需行的前一行

[root@lnh ~]# grep keyboard anaconda-ks.cfg 
keyboard --xlayouts='us'
[root@lnh ~]# grep -B 1  keyboard anaconda-ks.cfg 
# Keyboard layouts
keyboard --xlayouts='us'
//b:before打印出需要的那一行的前一行

-C打印所需行的前一行和后一行

[root@lnh ~]# grep keyboard anaconda-ks.cfg 
keyboard --xlayouts='us'
[root@lnh ~]# grep -C 1  keyboard anaconda-ks.cfg 
# Keyboard layouts
keyboard --xlayouts='us'
# System language
//c:context上下文,打印出需要行的前一行和后一行

-E

[root@lnh ~]# mkdir aa
[root@lnh ~]# cd aa/
[root@lnh aa]# touch abc ABC Abc ABc aBC abC
[root@lnh aa]# ls
abc  abC  aBC  Abc  ABc  ABC
[root@lnh aa]# ls|grep abc
abc
//查看abc
[root@lnh aa]# ls|grep -i abc
abc
abC
aBC
Abc
ABc
ABC
//忽略了大小全部打印出
[root@lnh aa]# ls|grep  abc|ABc
-bash: ABc: command not found
//错误用法把ABc当成命令了
[root@lnh aa]# ls|grep  'abc|ABc'
//没有过滤出来,因为|本来就是有特殊意义的
[root@lnh aa]# ls|grep  'abc\|ABc'
abc
ABc
//不推荐使用
[root@lnh aa]# ls|grep -E 'abc|ABc'
abc
ABc
//正则表达式
[root@lnh aa]# ls|egrep  'abc|ABc'
abc
ABc
//egrep=grep -E

fgrep不支持正则表达式,执行速度快

[root@lnh aa]# ls|fgrep  'abc|ABc'
[root@lnh aa]# ls|fgrep  'abc'
abc
[root@lnh aa]# ls|grep abc
abc
//用处不大,比较麻烦

文件查找

locate非实时,模糊匹配,查找是根据全系统文件数据库进行的,查找的速度快.依赖于事先构建的索引。索引的构建是在系统较为空闲时自动进行(周期性任务)语法:locate KEYWORD
updatedb手动生成文件数据库,索引构建过程需要遍历整个根文件系统,极消耗资源
find实时查找,精确性强,遍历指定目录中所有文件完成查找,查找速度慢,支持众多查找标准.查找路径:默认为当前目录.查找标准:默认为指定路径下的所有文件.语法:find [OPTION...] 查找路径 查找标准 查找到以后的处理动作
find 搜索路径 [参数] 搜索内容
-name 对文件名作精确匹配
find 查找路径 -name 查找内容

[root@lnh ~]# cd tushanbu/
[root@lnh tushanbu]# touch xbz Xbz xBZ xbz.txt
[root@lnh tushanbu]# ls
xbz  xBZ  Xbz  xbz.txt
[root@lnh ~]# find tushanbu/ -name "xbz"
tushanbu/xbz
[root@lnh ~]# find tushanbu/ -name "xBZ"
tushanbu/xBZ
[root@lnh ~]# find tushanbu/ -name "Xbz"
tushanbu/Xbz
[root@lnh ~]# find tushanbu/ -name "xbz.txt"
tushanbu/xbz.txt
//(find /tushanbu/  -name "xbz")全名称查找,带双引号查看更加好些也可以不带
[root@lnh ~]# find tushanbu/ -name "xbz*"
tushanbu/xbz
tushanbu/xbz.txt
//模糊查找(find /tushanbu/ -name "xbz*")
#### -iname 文件名匹配时不区分大小写
[root@lnh ~]# find -iname xbz
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
//在当前路径下面查找

-user username(用户名)根据属主来查找

[root@lnh ~]# find -user root
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./tushanbu
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./.viminfo
./.lesshst
./xxb
./test
./aa
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC
//根据用户名/主名查找

-group groupname(组名)根据属组来查找

[root@lnh ~]# find -group root
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./tushanbu
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./.viminfo
./.lesshst
./xxb
./test
./aa
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC
//属组名

Linux的组分两类 一个是用户的主要组(主组):用户必须属于一个且只有一个主组.组名同用户名,且仅包含一个用户:私有组,一个是用户的附加组(辅助组):一个用户可以属于零个或多个辅助组
-uid 根据UID进行查找,当用户被删除以后文件的属主会变为此用户的UID
-gid 根据GID进行查找,当用户被删除以后文件的属组会变为此用户的GID
-nouser查找没有属主的文件.用户被删除的情况下产生的文件,只有uid没有属主
-nogroup 查找没有属组的文件.组被删除的情况下产生的文件,只有gid没有属组
-type 根据文件类型来查找(f普通文件,d目录文件,c字符设备,b块文件,l软链接,p,s)

//find 查找路径 -type 文件类型字符
[root@lnh ~]# find -type f
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./.viminfo
./.lesshst
./xxb
./test
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC
//查找 /root/ 目录下的普通文件(find /root/ -type f)
//在当前路径下查找普通文件
[root@lnh ~]# find tushanbu/ -type f
tushanbu/xbz
tushanbu/Xbz
tushanbu/xBZ
tushanbu/xbz.txt
//在tushanbu路径下查找普通文件
[root@lnh ~]# find -type d
.
./tushanbu
./aa
//查找 /root/目录下的/目录文件(find /root/ -type d)
[root@lnh ~]# find tushanbu/ -type d
tushanbu/
//查找tushanbu路径下的目录文件

-size根据文件大小进行查找。如1k、1M,+10k、+10M,-1k、-1M, . +表示大于,-表示小于
[+|-]
#b(512Byte查找,默认单位)、#c(字节单位)#K(KB单位)、#M(MB单位)、#G(GB单位)
#Unit表示(从#-1到#之间的范围大小)
-#Unit表示(从0到#-1的范围大小)
+#Unit表示(大于#的所有)

//find 查找路径 -size 文件大小[大小单位]
[root@lnh ~]# find -size -1M
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC
//当前路径下小于1M的文件
[root@lnh ~]# find -size +1M
./test
//当前路径下查看大于1M的文件

-mtime修改时间

//find 查找路径 -mtime [+-]天数
[root@lnh ~]# find -mtime -3
.
./.bash_history
./tushanbu
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./.viminfo
./.lesshst
./xxb
./test
./aa
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC
[root@lnh ~]# find -mtime -1
.
./.bash_history
./tushanbu
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./aa
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC

-ctime改变时间

//find 查找路径 -ctime [+-]天数
[root@lnh ~]# find -ctime -1
.
./.bash_history
./tushanbu
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./aa
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC
[root@lnh ~]# find -ctime -2
.
./.bash_history
./tushanbu
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./aa
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC

-atime 访问时间,+5:5天前,-5:5天以内

//find 查找路径 -atime [+-]天数
[root@lnh ~]# find -atime -2
.
./.bash_logout
./.bash_profile
./.bashrc
./anaconda-ks.cfg
./.bash_history
./tushanbu
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./.lesshst
./aa
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC
[root@lnh ~]# find -atime -1
.
./.bash_logout
./.bash_profile
./.bashrc
./anaconda-ks.cfg
./.bash_history
./tushanbu
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./.lesshst
./aa
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC

-mmin 多少分钟修改过

//find 查找路径 -mmin [+-]分钟
[root@lnh ~]# find -mmin -100
.
./tushanbu
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./aa
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC

-cmin 多少分钟改变过

//find 查找路径 -cmin [+-]分钟
[root@lnh ~]# find -cmin -100
.
./.bash_history
./tushanbu
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./aa
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC

-amin多少分钟访问过,+5 :5分钟前.-5:5分钟以内

//find 查找路径 -amin [+-]分钟
[root@lnh ~]# find -amin -100
.
./.bash_history
./tushanbu
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./aa
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC

-perm mode根据权限精确查找

[root@lnh ~]# ll
total 102408
drwxr-xr-x. 2 root root        72 Jul  3 05:37 aa
-rw-------. 1 root root      1003 Jun 28 04:46 anaconda-ks.cfg
-rw-r--r--. 1 root root 104857600 Jun 30 15:45 test
drwxr-xr-x. 2 root root        39 Jul  3 06:15 tushanbu
-rw-r--r--. 1 root root        54 Jun 30 15:16 xxb
[root@lnh ~]# find -perm 755
./tushanbu
./aa

-perm -mode文件权限能完全包含此mode时才符合条件

[root@lnh ~]# ll
total 102408
drwxr-xr-x. 2 root root        72 Jul  3 05:37 aa
-rw-------. 1 root root      1003 Jun 28 04:46 anaconda-ks.cfg
-rw-r--r--. 1 root root 104857600 Jun 30 15:45 test
drwxr-xr-x. 2 root root        39 Jul  3 06:15 tushanbu
-rw-r--r--. 1 root root        54 Jun 30 15:16 xxb
[root@lnh ~]# find -perm -755
./tushanbu
./aa

-perm /mode 9位权限中有任何一位权限匹配都视为符合查找条件

[root@lnh ~]# ll
total 102408
drwxr-xr-x. 2 root root        72 Jul  3 05:37 aa
-rw-------. 1 root root      1003 Jun 28 04:46 anaconda-ks.cfg
-rw-r--r--. 1 root root 104857600 Jun 30 15:45 test
drwxr-xr-x. 2 root root        39 Jul  3 06:15 tushanbu
-rw-r--r--. 1 root root        54 Jun 30 15:16 xxb
[root@lnh ~]# find -perm /5
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./tushanbu
./tushanbu/xbz
./tushanbu/Xbz
./tushanbu/xBZ
./xxb
./test
./aa
./aa/abc
./aa/ABC
./aa/Abc
./aa/ABc
./aa/aBC
./aa/abC

组合条件:
-a组合查找
-o并集查找
-not
!对查找的内容取反
例:
!A -a !B = !(A -o B)
!A -o !B = !(A -a B)

[root@lnh ~]# dd if=/dev/zero of=test.log bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB, 500 MiB) copied, 0.314484 s, 1.7 GB/s
//创建一个空白且占500MB的文件
[root@lnh ~]# find /root/ -size +100M -o -type d
/root/
/root/tushanbu
/root/test.log
//find 查找路径 [选项] 查找内容 -o [选项] 查找内容
//查找文件大小大于100MB的文件,或者是目录文件
[root@lnh ~]# find /root/ -size +100M -a -type f
/root/test.log
//find 查找路径 [选项] 查找内容 -a [选项] 查找内容
//查找文件大小大于100MB的文件,并且是普通文件
[root@lnh ~]# find /root/ ! -size -100M
/root/test.log
//find 查找路径 ! [选项] 查找内容
//查找不小于100MB的文件

处理动作:默认为显示到屏幕上
-print显示
-ls类似ls -l的形式显示每一个文件的详细信息
-delete删除查找到的文件
-fls (/path/to/somefile) 查找到的所有文件的长格式信息保存至指定文件中
-ok COMMAND {} ; //对查找到的每个文件执行COMMAND,每次操作都需要用户确认
-exec COMMAND {} ; //对查找到的每个文件执行COMMAND,操作不需要确认
注意:find传递查找到的文件至后面指定的命令时,查找到所有
符合条件的文件一次性传递给后面的命令,而有些命令不能接受过多参数,
此时命令执行可能会失败。而xargs可规避此问题
xargs通过管道将查找到的内容给xargs处理,xargs后面直接跟命令即可
find 查找路径 [选项] 查找内容 -exec 命令2{};(注意这里的"{}“和”;“是标准格式,只要执行”-exec"选项,这两个符号必须完整输入)

[root@lnh ~]# find -perm -755 -ls
 67502946      0 drwxr-xr-x   2  root     root           39 Jul  3 06:15 ./tushanbu
 67507266      0 drwxr-xr-x   2  root     root           72 Jul  3 05:37 ./aa
//显示文件的详细信息
[root@lnh ~]# chmod 4777  bb
[root@lnh ~]# chmod 4777  aa
//赋予权限
[root@lnh ~]# ls
aa  anaconda-ks.cfg  bb  test  tushanbu  xxb
[root@lnh ~]# ll
total 12
drwsrwxrwx. 2 root root   72 Jul  3 05:37 aa
-rw-------. 1 root root 1003 Jun 28 04:46 anaconda-ks.cfg
-rwsrwxrwx. 1 root root    0 Jul  3 07:30 bb
-rw-r--r--. 1 root root  156 Jul  3 07:38 test
drwxr-xr-x. 2 root root   39 Jul  3 06:15 tushanbu
-rw-r--r--. 1 root root   54 Jun 30 15:16 xxb
[root@lnh ~]# find -perm -4777 -fls test 
[root@lnh ~]# cat test 
 67507266      0 drwsrwxrwx   2  root     root           72 Jul  3 05:37 ./aa
201329349      0 -rwsrwxrwx   1  root     root            0 Jul  3 07:30 ./bb
[root@lnh ~]# find -perm -4777 -delete
[root@lnh ~]# ls
anaconda-ks.cfg  test  tushanbu  xxb
[root@lnh ~]# ll
total 12
-rw-------. 1 root root 1003 Jun 28 04:46 anaconda-ks.cfg
-rw-r--r--. 1 root root  156 Jul  3 07:38 test
drwxr-xr-x. 2 root root   39 Jul  3 06:15 tushanbu
-rw-r--r--. 1 root root   54 Jun 30 15:16 xxb
//在当前目录进行查看然后删除,如果里面有文件是不能删除的
[root@lnh ~]# find -name xbz -exec cp {} /opt/ \;
[root@lnh ~]# ls /opt/
xbz
//查找xbz这个文件将其先cp到{}里面,然后将{}cp到opt目录下面(此操作是用户默认执行)
[root@lnh ~]# find -name xbz -ok cp {} /opt/ \;
< cp ... ./tushanbu/xbz > ? y
[root@lnh ~]# ls /opt/
xbz
//次操作和上面同理但是需要用户同意才可以
[root@lnh ~]# mkdir xbz
[root@lnh ~]# ll
total 12
-rw-------. 1 root root 1003 Jun 28 04:46 anaconda-ks.cfg
-rw-r--r--. 1 root root  156 Jul  3 07:38 test
drwxr-xr-x. 2 root root   39 Jul  3 06:15 tushanbu
drwxr-xr-x. 2 root root    6 Jul  3 08:06 xbz
-rw-r--r--. 1 root root   54 Jun 30 15:16 xxb
[root@lnh ~]# find -type d -name xbz | xargs rm -rf
[root@lnh ~]# ll
total 12
-rw-------. 1 root root 1003 Jun 28 04:46 anaconda-ks.cfg
-rw-r--r--. 1 root root  156 Jul  3 07:38 test
drwxr-xr-x. 2 root root   39 Jul  3 06:15 tushanbu
-rw-r--r--. 1 root root   54 Jun 30 15:16 xxb
//|管道符后面接xargs然后接命令
[root@lnh ~]# dd if=/dev/zero of=test.log bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB, 500 MiB) copied, 0.314484 s, 1.7 GB/s
//创建一个空白且占500MB的文件
[root@lnh ~]# find /root/ -size 500M -exec rm -f {} \;
[root@lnh ~]# ll
total 24
-rw-------. 1 root root 1003 Jun 28 04:46 anaconda-ks.cfg
-rw-r--r--. 1 root root  102 Jul  3 08:32 ls.bbbbbb
-rw-r--r--. 1 root root  102 Jul  3 08:26 ls.xxxxx
-rw-r--r--. 1 root root  156 Jul  3 07:38 test
drwxr-xr-x. 2 root root   54 Jul  3 10:09 tushanbu
-rw-r--r--. 1 root root    9 Jul  3 08:35 xxb
-rw-r--r--. 1 root root   80 Jul  3 08:20 xxb.z
//删除 /root/ 目录下大小为500MB的文件

文件层级系统

FHS 文件层级系统
/ 可以单独分区,LVM分区
/boot 1.系统启动相关的文件,如内核(vmlinuz)、initrd(initramfs)
2.以及grub(bootloader)。建议单独分区,基本分区
/dev 设备文件。不能单独分区
1. 设备文件 //关联至一个设备驱动程序,进而能够与之对应硬件设备进行通信
1.1块设备 //随机访问,数据块(比如硬盘)
1.2 字符设备 //也叫线性设备,线性访问,按字符为单位(比如鼠标、显示器)
1.3设备号 //主设备号(major)和次设备号(minor)
1.3.1 主设备号标识设备类型
1.3.2次设备号标识同一类型下的不同设备
2.设备文件只有元数据,没有数据
/etc 配置文件
/home 1.普通用户的家目录,每一个用户的家目录通常默认为/home/USERNAME.
2.建议单独分区
/root 管理员的家目录。不该单独分区
/lib 库文件
1.静态库 //.a
2.动态库 //.dll,.so(shared object)
3./lib/modules //内核模块文件
/media //挂载点目录,通常用来挂载移动设备
/mnt //挂载点目录,通常用来挂载额外的临时文件系统,比如另一块硬盘
/opt //可选目录,早期通常用来安装第三方程序
/proc 1.//伪文件系统,内核映射文件(伪文件系统实际上里面是没有任何内容的,
2.//开机之后才映射上去的)。不能单独分区
/sys 1.//伪文件系统,跟硬件设备相关的属性映射文件(伪文件系统实际上里面是没有
2.//任何内容的,开机之后才映射上去的)。不能单独分区
/tmp //临时文件,/var/tmp
/var 1.//可变化的文件,比如log、cache。存放日志信息、pid文件、lock文件,
2.//建议单独分区
/bin //可执行文件,用户命令
/sbin //管理命令
/usr //shared,read-only,全局共享只读文件。提供操作系统核心功能,可以单独分区
/usr/bin
/usr/sbin
/usr/lib
/usr/local //第三方软件安装路径
/usr/local/bin
/usr/local/sbin
/usr/local/lib
/usr/local/etc
/usr/local/man
/etc,/bin,/sbin,/lib 内是系统启动就需要用到的程序,这些目录不能挂载额外的分区,\必须在根文件系统的分区上
/usr/bin,/usr/sbin,/usr/lib 提供操作系统核心功能,/usr可以单独分区
/usr/local/bin,/usr/local/sbin,/usr/local/lib,/usr/local/etc\ /usr/local/man等等 在/usr/local目录下的内容都是第三方软件,建议单独分区

重定向与管道

系统设定:
默认输入设备      标准输入,STDIN,0  (键盘)
默认输出设备      标准输出,STDOUT,1 (显示器)
标准错误输出      STDERR,2 (显示器)
I/O重定向:
>:覆盖输出
>>:追加输出
[root@lnh ~]# ls
anaconda-ks.cfg  test  tushanbu  xxb
[root@lnh ~]# ls > xxb.z
[root@lnh ~]# cat xxb.z 
anaconda-ks.cfg
test
tushanbu
xxb
xxb.z
[root@lnh ~]# ls >> xxb.z
[root@lnh ~]# cat xxb.z 
anaconda-ks.cfg
test
tushanbu
xxb
xxb.z
anaconda-ks.cfg
test
tushanbu
xxb
xxb.z

2> 重定向错误输出
2>> 追加重定向错误输出

[root@lnh ~]# ls xbz 2> ls.xxxxx
[root@lnh ~]# cat ls.xxxxx 
ls: cannot access 'xbz': No such file or directory
[root@lnh ~]# ls xbz 2>> ls.xxxxx
[root@lnh ~]# cat ls.xxxxx 
ls: cannot access 'xbz': No such file or directory
ls: cannot access 'xbz': No such file or directory

&> 覆盖重定向标准输出或错误输出至同一个文件
&>> 追加重定向标准输出或错误输出至同一个文件

[root@lnh ~]# ls xxb xbbbb &> ls.bbbbbb
[root@lnh ~]# cat ls.bbbbbb 
ls: cannot access 'xbbbb': No such file or directory
xxb
[root@lnh ~]# ls xxb xbbbb &>> ls.bbbbbb
[root@lnh ~]# cat ls.bbbbbb 
ls: cannot access 'xbbbb': No such file or directory
xxb
ls: cannot access 'xbbbb': No such file or directory
xxb

< 输入重定向
<< Here Document

[root@lnh ~]# ls
anaconda-ks.cfg  ls.bbbbbb  ls.xxxxx  test  tushanbu  xxb  xxb.z
[root@lnh ~]# cat  > ls.bbbbbb < ls.xxxxx 
[root@lnh ~]# cat ls.xxxxx 
ls: cannot access 'xbz': No such file or directory
ls: cannot access 'xbz': No such file or directory
[root@lnh ~]# cat  > xxb < test 
[root@lnh ~]# cat xxb
 67507266      0 drwsrwxrwx   2  root     root           72 Jul  3 05:37 ./aa
201329349      0 -rwsrwxrwx   1  root     root            0 Jul  3 07:30 ./bb
[root@lnh ~]# cat  > xxb << test 
> eeee
> eee
> test
[root@lnh ~]# cat xxb
eeee
eee

管道 前一个命令的输出,作为后一个命令的输入。最后一个命令会在当前shell进程. 的子shell进程中执行
命令1 | 命令2 | 命令3 | ...
tee从标准输入读取数据,输出一份到屏幕上,一份保存到文件

[root@lnh ~]# ls
anaconda-ks.cfg  ls.bbbbbb  ls.xxxxx  test  tushanbu  xxb  xxb.z
[root@lnh ~]# ls |grep -iv "^w"|grep "x$"
ls.xxxxx
[root@lnh ~]# echo "hello tushanbu" | tee /tmp/hello.out
hello tushanbu
[root@lnh ~]# cat /tmp/hello.out
hello tushanbu

bash字符串处理

bash特性之截取变量的字符串

xbz=/usr/local/src
echo ${xbz#*/}    结果为    usr/local/src
echo ${xbz##*/}   结果为    src
echo ${xbz%/*}    结果为    /usr/local
[root@lnh ~]# xbz=/usr/local/src
[root@lnh ~]# echo ${xbz#*/}
usr/local/src
[root@lnh ~]# echo ${xbz##*/}
src
[root@lnh ~]#  echo ${xbz%/*} 
/usr/local

${xbz:offset:number}字符串切片

[root@lnh ~]# echo ${xbz:4:7}
/local/

取字符串的最右侧几个字符(从右往左)
${xbz: -lengh}
注意:冒号后必须有一空白字符

[root@lnh ~]# echo ${xbz: -4}
/src
查找替换
${xbz/src/xxb}    查找xbz所表示的字符串中,第一次被src所匹配到字符串,以xxb替换之
${xbz//src/xxb}   查找var所表示的字符串中,所有能被src所匹配到字符串,以xxb替换之
${xbz/#src/xxb}   查找var所表示的字符串中,行首被src所匹配到字符串,以xxb替换之
${xbz/%src/xxb}   查找var所表示的字符串中,行尾被src所匹配到字符串,以xxb替换之
[root@lnh ~]# echo ${xbz}
/usr/local/src
[root@lnh ~]# echo ${xbz/src/xxb}
/usr/local/xxb
[root@lnh ~]# echo ${xbz//src/xxb}
/usr/local/xxb
[root@lnh ~]# echo ${xbz/#src/xxb}
/usr/local/src
[root@lnh ~]# echo ${xbz/%src/xxb}
/usr/local/xxb
查找并删除
${xbz/src}      查找xbz所表示的字符串中,第一次被src所匹配到字符串,删除之
${xbz//src}     查找xbz所表示的字符串中,所有被src所匹配到字符串,删除之
${xbz/#src}     查找xbz所表示的字符串中,行首被src所匹配到字符串,删除之
${xbz/%src}     查找xbz所表示的字符串中,行尾被src所匹配到字符串,删除之
[root@lnh ~]# echo ${xbz/src}
/usr/local/
[root@lnh ~]# echo ${xbz//src}
/usr/local/
[root@lnh ~]# echo ${xbz/#src}
/usr/local/src
[root@lnh ~]# echo ${xbz/%src}
/usr/local/

相关内容