文章目录
基础命令(二)
通配符的使用
通配符
- *:匹配任意长度的任意字符
[root@localhost ~]# ls
5 a ab ac af anaconda-ks.cfg b bb
[root@localhost ~]# ls a*
ab ac af anaconda-ks.cfg
- ?:匹配任意单个字符
[root@localhost ~]# ls
ab abc ac af anaconda-ks.cfg b bb
[root@localhost ~]# ls a?
ab ac af
[root@localhost ~]# ls a??
abc
- []:匹配指定范围内的任意单个字符
- [abc],[a-m],[0-9]
[root@localhost ~]# touch ./{1..5} //创建数字一到五为名字的文件
[root@localhost ~]# touch ./{a..e} //创建a到e为名字的文件
[root@localhost ~]# ls
1 2 3 4 5 a b c d e ww
[root@localhost ~]# touch ./{11..14}
[root@localhost ~]# ls
1 11 12 13 14 2 3 4 5 a b c d e ww
[root@localhost ~]# touch ab ac abc
[root@localhost ~]# ls
1 11 12 13 14 2 3 4 5 a ab abc ac b c d e ww
[root@localhost ~]# ls [1-3]
1 2 3
[root@localhost ~]# ls [135]
1 3 5
[root@localhost ~]# ls [a-c]
a b c
[root@localhost ~]# ls [a-c][ab]
ab
[root@localhost ~]# ls [acd]
a c d
- [[:space:]]:表示空白字符
- [[:punct:]]:表示标点符号
- [[:lower:]]:表示小写字母
- [[:upper:]]:表示大写字母
- [[:alpha:]]:表示大小写字母
- [[:digit:]]:表示数字
- [[:alnum:]]:表示数字和大小写字母
[root@localhost ~]# touch ./{i..K}
[root@localhost ~]# ls
` _ ] 11 13 2 4 a abc b d f h K M O Q S U W X Z
^ [ 1 12 14 3 5 ab ac c e g i L N P R T V ww Y
[root@localhost ~]# ls [[:punct:]]
` ^ _ [ ]
[root@localhost ~]# ls [[:lower:]]
a b c d e f g h i
[root@localhost ~]# ls [[:upper:]]
K L M N O P Q R S T U V W X Y Z
[root@localhost ~]# ls [[:alpha:]]
a b c d e f g h i K L M N O P Q R S T U V W X Y Z
[root@localhost ~]# ls [[:digit:]]
1 2 3 4 5
[root@localhost ~]# ls [[:alnum:]]
1 2 3 4 5 a b c d e f g h i K L M N O P Q R S T U V W X Y Z
- [^]:匹配指定范围之外的任意单个字符
[root@localhost ~]# ls
` _ ] 11 13 2 4 a abc b d f h K M O Q S U W X Z
^ [ 1 12 14 3 5 ab ac c e g i L N P R T V ww Y
[root@localhost ~]# ls [^a-f] //取反
` ^ _ [ ] 1 2 3 4 5 g h i K L M N O P Q R S T U V W X Y Z
文件归档、压缩的方法
常见的归档与压缩文件格式
- .gz
- .bz2
- .xz
- .zip
- .Z
压缩工具之gzip
- Usage: gzip [OPTION]… [FILE]…
- 常用的OPTION:
- -d:解压缩,解压完成后会删除原文件
[root@localhost ~]# ls
a b c ww
[root@localhost ~]# which gzip //查看命令路径
/usr/bin/gzip
[root@localhost ~]# gzip a
[root@localhost ~]# ls
a.gz b c ww
[root@localhost ~]# gzip -d a
[root@localhost ~]# ls
a b c ww
压缩工具之bzip2
-
Usage: bzip2 [OPTION]… [FILE]…
-
常用的OPTION:
-
-k:压缩时保留原文件
-
-d:解压缩,解压完成后会删除原文件
[root@localhost ~]# which bzip2
/usr/bin/bzip2
[root@localhost ~]# ls
a b c ww
[root@localhost ~]# bzip2 b
[root@localhost ~]# ls
a b.bz2 c ww
[root@localhost ~]# bzip2 -d b.bz2
[root@localhost ~]# ls
a b c ww
[root@localhost ~]# bzip2 -k b
[root@localhost ~]# ls
a b b.bz2 c ww
压缩工具之xz
-
Usage: xz [OPTION]… [FILE]…
-
常用的OPTION:
-
-k:压缩时保留原文件
-
-d:解压缩,解压完成后会删除原文件
[root@localhost ~]# which xz
/usr/bin/xz
[root@localhost ~]# ls
a b b.bz2 c ww
[root@localhost ~]# xz c
[root@localhost ~]# ls
a b b.bz2 c.xz ww
[root@localhost ~]# xz -d c.xz
[root@localhost ~]# ls
a b b.bz2 c ww
压缩工具之zip
- zip压缩文件的两种方式:
- zip filename.zip file1 file2 …
- zip filename.zip DIR/*
zip压缩包要使用unzip工具来解压
[root@localhost ~]# which zip
/usr/bin/zip
[root@localhost ~]# ls
a b b.bz2 c ww
[root@localhost ~]# which unzip //查看结果没有unzip
/usr/bin/which: no unzip in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost ~]# yum -y install unzip //yum安装unzip
[root@localhost ~]# zip d.zip a b //将a和b压缩到d.zip
adding: a (stored 0%)
adding: b (stored 0%)
[root@localhost ~]# ls
a b b.bz2 c d.zip ww
//解压后会和原文件冲突,会提示是否覆盖,故移入目录ww
[root@localhost ~]# mv d.zip ww
[root@localhost ~]# cd ww
[root@localhost ww]# ls
anaconda-ks.cfg d.zip
[root@localhost ww]# unzip d.zip
Archive: d.zip
extracting: a
extracting: b
[root@localhost ww]# ls
a anaconda-ks.cfg b d.zip
[root@localhost ww]# mkdir f
[root@localhost ww]# ls
a anaconda-ks.cfg b d.zip f
[root@localhost ww]# cd f
[root@localhost f]# touch 1
[root@localhost f]# cd
[root@localhost ~]# cd ww
[root@localhost ww]# zip f.zip f/* //压缩到目录
adding: f/1 (stored 0%)
[root@localhost ww]# unzip f.zip
Archive: f.zip
extracting: f/1
[root@localhost ww]# ls
anaconda-ks.cfg f f.zip
[root@localhost ww]# ls f
1
压缩工具之compress
- Usage: compress [file …]
[root@localhost ww]# which comperss
/usr/bin/which: no comperss in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
木有安装包
解压需要使用uncompress命令
归档工具tar
-
Usage: tar [OPTION…] [FILE]…
-
常用的OPTION:
-
-c:创建归档文件
-
-f file.tar:指定要操作的归档文件
-
-x:还原归档
[root@localhost ~]# ls
a b c ww
[root@localhost ~]# tar cf a.tar a b
[root@localhost ~]# ls
a a.tar b c ww
[root@localhost ~]# tar xf a.tar -C ww //将解压的文件指定到目录ww
[root@localhost ~]# ls ww
a anaconda-ks.cfg b
- -v:显示归档过程
- -p:归档时保留权限信息。只有管理员才有权限用此选项
- -C:指定还原归档或解压时的目标目录
- -tf:不展开归档,直接查看归档了哪些文件
[root@localhost ~]# tar xpvf a.tar
a
b
[root@localhost ~]# ll
总用量 12
-rw-r--r--. 1 root root 0 9月 12 15:48 a
-rw-r--r--. 1 root root 10240 9月 12 16:55 a.tar
-rw-r--r--. 1 root root 0 9月 12 15:48 b
-rw-r--r--. 1 root root 0 9月 12 15:48 c
drwxr-xr-x. 2 root root 47 9月 12 16:55 ww
[root@localhost ~]# tar tf a.tar
a
b
- 常用的组合项:
- -zcf file.tar.gz:归档并调用gzip进行压缩
[root@localhost ~]# ls
a a.tar b c ww
[root@localhost ~]# tar zcf d.tar.gz a b c
[root@localhost ~]# ls
a a.tar b c d.tar.gz ww
[root@localhost ~]# file d.tar.gz
d.tar.gz: gzip compressed data, from Unix, last modified: Thu Sep 12 17:01:42 2019
- -jcf file.tar.bz2:归档并调用bzip2进行压缩
[root@localhost ~]# tar jcf e.tar.gz a b c
[root@localhost ~]# ls
a b c e.tar.gz ww
[root@localhost ~]# file e.tar.gz
e.tar.gz: bzip2 compressed data, block size = 900k
- -Jcf file.tar.xz:归档并调用xz进行压缩
[root@localhost ~]# tar Jcf d.tar.xz a b c
[root@localhost ~]# ls
a b c d.tar.xz e.tar.gz ww
[root@localhost ~]# file d.tar.xz
d.tar.xz: XZ compressed data
- xf file.tar.gz|file.tar.bz2|file.tar.xz:还原归档并自动根据压缩方式调用相应的工具解压
[root@localhost ~]# ls
a b c d.tar.xz e.tar.gz ww
[root@localhost ~]# file d.tar.xz
d.tar.xz: XZ compressed data
[root@localhost ~]# file e.tar.gz
e.tar.gz: bzip2 compressed data, block size = 900k
[root@localhost ~]# ls
a b c d.tar.xz e.tar.gz ww
[root@localhost ~]# rm -rf a b c
[root@localhost ~]# tar xf d.tar.xz
[root@localhost ~]# ls
a b c d.tar.xz e.tar.gz ww
[root@localhost ~]# rm -rf a b c
[root@localhost ~]# tar xf e.tar.gz
[root@localhost ~]# ls
a b c d.tar.xz e.tar.gz ww
文本排序与去重方法
文本排序命令sort
-
Usage: sort [OPTION]… [FILE]…
-
常用的OPTION:
-
-n:以数值大小进行排序
[root@localhost ~]# ls
b c ww
[root@localhost ~]# sort b
13
3
34
5
53
56
[root@localhost ~]# sort -n b
3
5
13
34
53
56
- -r:逆序排序
[root@localhost ~]# sort -n b
3
5
13
34
53
56
[root@localhost ~]# sort -r b
56
53
5
34
3
13
- -t:字段分隔符
- -k:以哪个字段为关键字进行排序
[root@localhost ~]# cp /etc/passwd .
[root@localhost ~]# ls
b c passwd ww
[root@localhost ~]# head passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# sort -n -t: -k4 passwd //以:为分隔符 以第四个字段为关键字进行排序
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
- -u:去重,排序后相同的行只显示一次
t@localhost ~]# echo '53' >>b
[root@localhost ~]# sort b
13
3
34
3 5 7
5
53
53
56
[root@localhost ~]# sort -u b
13
3
34
3 5 7
5
53
56
- -f:排序时忽略字符大小写
[root@localhost ~]# sort -f c
AA
asd
CD
DSA
Dse
fd
文本去重命令uniq
-
Usage: uniq [OPTION]… [INPUT [OUTPUT]]
-
常用的OPTION:
-
-c:显示文件中行重复的次数
-
-d:只显示重复的行
-
-u:只显示未重复的行
[root@localhost ~]# sort -n b |uniq -c
1 3 //前面一个数字是重复的次数,后面数字是哪个数字重复了
1 3 5 7
1 5
1 13
1 34
2 53
1 56
[root@localhost ~]# sort -n b |uniq -d
53
[root@localhost ~]# sort -n b |uniq -u
3
3 5 7
5
13
34
56
cut命令的用法
基础命令之cut
-
Usage: cut OPTION… [FILE]…
-
常用的OPTION:
-
-d:指定字段分隔符,默认是空格
-
-f:指定要显示的字段
-
-f 1,3:显示1和3字段
-
-f 1-3:显示1到3字段
[root@localhost ~]# cut -d: -f 1,3 passwd //处理规则的文件
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
games:12
ftp:14
nobody:99
systemd-network:192
dbus:81
polkitd:999
postfix:89
chrony:998
sshd:74
[root@localhost ~]# cut -d: -f 1-3 passwd
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4
sync:x:5
shutdown:x:6
halt:x:7
mail:x:8
operator:x:11
games:x:12
ftp:x:14
nobody:x:99
systemd-network:x:192
dbus:x:81
polkitd:x:999
postfix:x:89
chrony:x:998
sshd:x:74
sed与awk工具的简单用法
awk筛选、过滤
[root@localhost ~]# awk -F: '{print $1,$3}' passwd
//-F 以:为分隔符 '{print $1,$3 }' 输出第一行和第三行 最后接操作的文件
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
halt 7
mail 8
operator 11
games 12
ftp 14
nobody 99
systemd-network 192
dbus 81
polkitd 999
postfix 89
chrony 998
sshd 74
[root@localhost ~]# awk 'BEGIN{FS=":";OFS=":" } {print $1,$3}' passwd
root:0
bin:1
daemon:2
[root@localhost ~]# awk 'BEGIN{FS=":";OFS="?" } {print $1,$3}' passwd
root?0
bin?1
daemon?2
[root@localhost ~]# awk 'BEGIN{FS=":";OFS="++" } {print $1,$3}' passwd
root++0
bin++1
//格式: awk 'BEGIN{FS="输入的分隔符";OFS="输出的分隔符" } {print $几就是第几列,$几就是第几列}' passwd操作的文件名
sed直接修改文件内容
[root@localhost ~]# ls
b c passwd ww
[root@localhost ~]# cat b
13
34
5
56
53
3
3 5 7
53
[root@localhost ~]# sed 's/53/110/g' b //sed 's修改/目标内容/结果/g全局修改' 目标文件
13
34
5
56
110
3
3 5 7
110
[root@localhost ~]# cat b //修改但是不保存
13
34
5
56
53
3
3 5 7
53
[root@localhost ~]# sed 's/53/110/g' b >> f //用法可追加生成新文件
[root@localhost ~]# ls
b c f passwd ww
[root@localhost ~]# cat f
13
34
5
56
110
3
3 5 7
110
[root@localhost ~]# sed -i 's/53/110/g' b //加-i 将修改的内容保存到原文件
[root@localhost ~]# cat b
13
34
5
56
110
3
3 5 7
110
[root@localhost ~]# cat b
13
34 5 4 5 7 9 6
15 65 23 5 8 9 5
5
56
110
3
3 5 7
110
[root@localhost ~]# sed 's/5/!/2' b //将每行的第二个5修改为!
13
34 5 4 ! 7 9 6
15 6! 23 5 8 9 5
5
56
110
3
3 5 7
110
[root@localhost ~]# cat -n c
1 AA
2 CD
3 fd
4 asd
5 DSA
6 Dse
[root@localhost ~]# sed -n '/A/p' c //打印带有A的行
AA
DSA
[root@localhost ~]# sed -n '3,5p' c //打印第三到第五行
fd
asd
DSA
linux中文件命名规范
文件命名规范
- 长度不能超过255字符
- 不能使用/当文件名
- 严格区分大小写
文本过滤命令grep的用法
文本过滤命令grep
grep命令根据正则表达式搜索文本,并将符合正则表达式的文本显示出来。
默认使用基本正则表达式来过滤文本。
- Usage: grep [OPTION]… PATTERN [FILE]…
[root@localhost ~]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@localhost ~]# grep 'root' passwd //单引号中间的是模式,符合这个模式标准的过滤出来
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
- 常用的OPTION:
- -i:忽略大小写
[root@localhost ~]# cat c
AA
CD
fd
asd
DSA
Dse
[root@localhost ~]# grep -i 'a' c
AA
asd
DSA
- --color:匹配到的内容高亮显示
[root@localhost ~]# alias grep
alias grep='grep --color=auto'
//默认别名,高亮显示
- -v:显示没有被正则表达式匹配到的内容
[root@localhost ~]# grep -v 'root' passwd //取反
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
- -o:只显示被正则表达式匹配到的内容
- -E:使用扩展正则表达式
- -q:静默模式,不输出任何信息
[root@localhost ~]# grep -E 'bash' passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# grep -E 'bash|root' passwd //匹配到bash或root都打印出来
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# grep -Eq 'bash|root' passwd //内容不显示
[root@localhost ~]# echo $? //判断上一条命令是否成功执行 0成功 1失败
0
- -A #:此处的#必须是数字。被正则匹配到的内容以及其后面#行的内容都显示出来
- -B #:此处的#必须是数字。被正则匹配到的内容以及其前面#行的内容都显示出来
- -C #:此处的#必须是数字。被正则匹配到的内容及其前后各#行的内容都显示出来
[root@localhost ~]# cat -n passwd
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
16 polkitd:x:999:997:User for polkitd:/:/sbin/nologin
17 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
18 chrony:x:998:996::/var/lib/chrony:/sbin/nologin
19 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@localhost ~]# grep -A 2 'lp' passwd //后两行
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@localhost ~]# grep -B 2 'lp' passwd //前两行
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@localhost ~]# grep -C 2 'lp' passwd //前后两行
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
文件查找命令find的用法
-
语法:find [OPTION]… 查找路径 查找标准 查找到以后的处理动作
-
查找路径:默认为当前目录
文件查找命令find
- 查找标准:
- -name filename:对文件名精确匹配,支持通配符
[root@localhost ~]# find / -name pa*
/sys/fs/selinux/class/passwd
/sys/fs/selinux/class/passwd/perms/passwd
/etc/passwd
/etc/pam.d/passwd
/root/passwd
/usr/bin/passwd
[root@localhost ~]# find / -name pa?
/sys/devices/pci0000:00/0000:00:0f.0/graphics/fb0/pan
/usr/share/locale/paa
/usr/share/locale/pag
/usr/share/locale/pal
/usr/share/locale/pam
/usr/share/locale/pap
/usr/share/locale/pau
- -iname filename:文件名匹配时不区分大小写
[root@localhost ~]# find / -iname A?
/sys/bus/acpi/drivers/ac
/usr/bin/ar
/usr/bin/as
/usr/share/locale/aa
/usr/share/locale/ab
/usr/share/locale/ae
/usr/share/locale/af
/usr/share/locale/ak
/usr/share/locale/am
/usr/share/locale/an
/usr/share/locale/ar
/usr/share/locale/as
/usr/share/locale/av
/usr/share/locale/ay
/usr/share/locale/az
/usr/share/groff/1.22.2/font/devps/AB
/usr/share/groff/1.22.2/font/devps/AI
/usr/share/groff/1.22.2/font/devps/AR
/usr/share/vim/vim74/lang/af
- -regex pattern:基于正则表达式进行文件名匹配
- -user:查找某用户的所有文件
- -group:查找某组的所有文件
- -uid:根据UID进行查找
- -gid:根据GID进行查找
- -nouser:查找没有拥有者的文件
- -nogroup:查找没有属组的文件
[root@localhost ~]# ls
abc b c f passwd qwe ww
[root@localhost ~]# find -name a
[root@localhost ~]# find -name b
./b
[root@localhost ~]# find / -name passwd //查找passwd
/sys/fs/selinux/class/passwd
/sys/fs/selinux/class/passwd/perms/passwd
/etc/passwd
/etc/pam.d/passwd
/root/passwd
/usr/bin/passwd
[root@localhost ~]# useradd wangwei //创建用户wangwei
[root@localhost ~]# ls /home/ //查看家目录
wangwei
[root@localhost ~]# ll
总用量 16
drwxr-xr-x. 2 root root 6 9月 15 15:29 abc
-rw-r--r--. 1 root root 56 9月 15 14:51 b
-rw-r--r--. 1 root root 21 9月 14 17:36 c
-rw-r--r--. 1 root root 27 9月 15 14:40 f
-rw-r--r--. 1 root root 846 9月 14 17:24 passwd
drwxr-xr-x. 2 root root 6 9月 15 15:30 qwe
drwxr-xr-x. 2 root root 29 9月 13 20:20 ww
[root@localhost ~]# ll /home/
总用量 0
drwx------. 2 wangwei wangwei 62 9月 15 15:34 wangwei
[root@localhost ~]# userdel wangwei //删除用户wangwei
[root@localhost ~]# ll /home/
总用量 0
drwx------. 2 1000 1000 62 9月 15 15:34 wangwei
[root@localhost ~]# id 1000 //已经被删除
id: 1000: no such user
[root@localhost ~]# find / -nouser //查看没有拥有者的文件
find: ‘/proc/4705/task/4705/fd/6’: 没有那个文件或目录
find: ‘/proc/4705/task/4705/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/4705/fd/6’: 没有那个文件或目录
find: ‘/proc/4705/fdinfo/6’: 没有那个文件或目录
/var/spool/mail/wangwei
/home/wangwei
/home/wangwei/.bash_logout
/home/wangwei/.bash_profile
/home/wangwei/.bashrc
[root@localhost ~]# find / -nogroup //查看没有属组的文件
find: ‘/proc/4723/task/4723/fd/6’: 没有那个文件或目录
find: ‘/proc/4723/task/4723/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/4723/fd/6’: 没有那个文件或目录
find: ‘/proc/4723/fdinfo/6’: 没有那个文件或目录
/home/wangwei
/home/wangwei/.bash_logout
/home/wangwei/.bash_profile
/home/wangwei/.bashrc
[root@localhost ~]# find / -nogroup -exec rm -rf {} \; //查找没有属组的文件并删除
//find / -nogroup -exec执行 rm -rf {}括号代表前面命令执行的结果 \;
find: ‘/proc/4756/task/4756/fd/6’: 没有那个文件或目录
find: ‘/proc/4756/task/4756/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/4756/fd/6’: 没有那个文件或目录
find: ‘/proc/4756/fdinfo/6’: 没有那个文件或目录
find: ‘/home/wangwei’: 没有那个文件或目录
[root@localhost ~]# ll /home/
总用量 0
- -type:根据文件类型进行查找
[root@localhost ~]# ls
b c f passwd ww
[root@localhost ~]# mkdir abc
[root@localhost ~]# mkdir qwe
[root@localhost ~]# find -type d
.
./ww
./abc
./qwe
文件查找命令find(续一)
- 查找标准(续):
- -size:根据文件大小进行查找。如1k、1M、+10k、+10M、-1k、-10M
- +表示大于,-表示小于
[root@localhost ~]# find -size +1k
./.bash_history
./ww/anaconda-ks.cfg
[root@localhost ~]# find -size -1k
./.lesshst
[root@localhost ~]# find -size 1k
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./ww
./passwd
./c
./f
./b
./.viminfo
- -mtime:根据修改时间查找
- +5:表示查找5天以前修改的文件
- -5:表示查找5天以内修改的文件
- -atime:根据访问时间查找
- -ctime:根据改变时间查找
[root@localhost ~]# ls
b c f passwd ww
[root@localhost ~]# find -mtime -5
.
./.bash_history
./.lesshst
./ww
./passwd
./c
./f
./b
./.viminfo
[root@localhost ~]# find /opt/ -mtime +5
/opt/
- -perm mode:根据权限精确查找
- -perm –mode:文件权限能完全包含此mode时才符合条件
- -perm /mode:9位权限中有任何一位权限匹配都视为符合查找条件
文件查找命令find(续二)
- 多条件组合使用:
- -a
- -o
- -not
- !
- 例如:
- !A –a !B 与 !(A –o B) 相等
- !A –o !B 与 !(A –a B) 相等
[root@localhost ~]# ll
总用量 16
-rw-r--r--. 1 root root 56 9月 15 14:51 b
-rw-r--r--. 1 root root 21 9月 14 17:36 c
-rw-r--r--. 1 root root 27 9月 15 14:40 f
-rw-r--r--. 1 root root 846 9月 14 17:24 passwd
drwxr-xr-x. 2 root root 29 9月 13 20:20 ww
[root@localhost ~]# find -mtime -3
.
./.bash_history
./ww
./passwd
./c
./f
./b
./.viminfo
[root@localhost ~]# find -mtime -3 -a -name f //查找修改时间在三天以内,并且名字叫f的文件
./f
[root@localhost ~]# find -mtime -1 -o -name passwd //查找修改时间在一天内,或者名字叫passwd的文件
.
./.bash_history
./passwd
./c
./f
./b
./.viminfo
文件查找命令find(续三)
- 处理动作:默认动作是显示到屏幕上
- -print:打印到屏幕上
- -ls:类似ls –l的形式显示每一个文件的详细信息
- -delete:删除查找到的文件
- -fls /path/to/somefile:将查找到的所有文件的长格式信息保存至指定文件中
[root@localhost ~]# find -type f -ls //详细打印
34043433 4 -rw-r--r-- 1 root root 18 12月 29 2013 ./.bash_logout
34043434 4 -rw-r--r-- 1 root root 176 12月 29 2013 ./.bash_profile
34043435 4 -rw-r--r-- 1 root root 176 12月 29 2013 ./.bashrc
34043436 4 -rw-r--r-- 1 root root 100 12月 29 2013 ./.cshrc
34043437 4 -rw-r--r-- 1 root root 129 12月 29 2013 ./.tcshrc
33574988 4 -rw------- 1 root root 3854 9月 15 15:43 ./.bash_history
33575026 0 -rw------- 1 root root 0 9月 11 16:23 ./.lesshst
33574978 4 -rw------- 1 root root 1412 9月 3 15:02 ./ww/anaconda-ks.cfg
33585695 4 -rw-r--r-- 1 root root 846 9月 14 17:24 ./passwd
33585700 4 -rw------- 1 root root 767 9月 15 14:51 ./.viminfo
33585697 4 -rw-r--r-- 1 root root 56 9月 15 14:51 ./b
33585698 4 -rw-r--r-- 1 root root 21 9月 14 17:36 ./c
33575027 4 -rw-r--r-- 1 root root 27 9月 15 14:40 ./f
[root@localhost ~]# ls
abc abd b c f passwd ww
[root@localhost ~]# find -name b -delete
[root@localhost ~]# ls
abc abd c f passwd ww
[root@localhost ~]# find -type f -fls a.txt
[root@localhost ~]# ls
abc abd a.txt c f passwd ww
[root@localhost ~]# cat a.txt
34043433 4 -rw-r--r-- 1 root root 18 12月 29 2013 ./.bash_logout
34043434 4 -rw-r--r-- 1 root root 176 12月 29 2013 ./.bash_profile
34043435 4 -rw-r--r-- 1 root root 176 12月 29 2013 ./.bashrc
34043436 4 -rw-r--r-- 1 root root 100 12月 29 2013 ./.cshrc
34043437 4 -rw-r--r-- 1 root root 129 12月 29 2013 ./.tcshrc
33574988 4 -rw------- 1 root root 3854 9月 15 15:43 ./.bash_history
33575026 0 -rw------- 1 root root 0 9月 11 16:23 ./.lesshst
33574978 4 -rw------- 1 root root 1412 9月 3 15:02 ./ww/anaconda-ks.cfg
33585695 4 -rw-r--r-- 1 root root 846 9月 14 17:24 ./passwd
33585700 4 -rw------- 1 root root 767 9月 15 14:51 ./.viminfo
33585698 4 -rw-r--r-- 1 root root 21 9月 14 17:36 ./c
33575027 4 -rw-r--r-- 1 root root 27 9月 15 14:40 ./f
33585670 0 -rw-r--r-- 1 root root 0 9月 15 16:19 ./a.txt
- -ok COMMAND {} ;:对查找到的所有文件执行COMMAND,每次操作都需要用户确认
- -exec COMMAND {} ;:对查找到的所有文件执行COMMAND,操作不需要确认
[root@localhost ~]# find / -nogroup //查看没有属组的文件
find: ‘/proc/4723/task/4723/fd/6’: 没有那个文件或目录
find: ‘/proc/4723/task/4723/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/4723/fd/6’: 没有那个文件或目录
find: ‘/proc/4723/fdinfo/6’: 没有那个文件或目录
/home/wangwei
/home/wangwei/.bash_logout
/home/wangwei/.bash_profile
/home/wangwei/.bashrc
[root@localhost ~]# find / -nogroup -exec rm -rf {} \; //查找没有属组的文件并删除
//find / -nogroup -exec执行 rm -rf {}括号代表前面命令执行的结果 \;
find: ‘/proc/4756/task/4756/fd/6’: 没有那个文件或目录
find: ‘/proc/4756/task/4756/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/4756/fd/6’: 没有那个文件或目录
find: ‘/proc/4756/fdinfo/6’: 没有那个文件或目录
find: ‘/home/wangwei’: 没有那个文件或目录
- xargs:通过管道将查找到的内容给xargs处理,xargs后面直接跟命令即可
[root@localhost ~]# touch a b
[root@localhost ~]# ls
a abc abd b passwd
[root@localhost ~]# mv a b abc
[root@localhost ~]# ls
abc abd passwd
[root@localhost ~]# find -type d
.
./abc
./abd
[root@localhost ~]# find -type d |xargs rm -rf //删除
rm: refusing to remove "." or ".." directory: skipping "."
[root@localhost ~]# ls
passwd
[root@localhost ~]# find -type d
.
./sad
[root@localhost ~]# find -type d |xargs mv -t /opt //移动
mv: 无法将"." 移动至"/opt/.": 设备或资源忙
[root@localhost ~]# ls /opt
sad
- 注意:find传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令,而有些命令不能接受过多的参数,此时命令执行可能会失败,而xargs可规避此问题。