Linux-如何成为一个优秀的管家(文件篇②)

接《Linux-如何成为一个优秀的管家(文件篇①)》

四、整理文件

① 打包文件:tar(tape archive)
tar -c()f() [打包文件名] [要打包的文件/列表]
⑴ 使用tar将tmp目录下一批文件,打包为sample.tar
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ tar -cf sample.tar /tmp/f01 /tmp/f02 /tmp/f03 /tmp/f04
tar: Removing leading `/' from member names
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll
total 172
-rw-rw-r-- 1 kelly kelly    37 Feb  5 15:36 file05
-rw-rw-r-- 1 kelly kelly    26 Feb  5 15:36 file06
-rw-rw-r-- 1 kelly kelly 10240 Feb  5 17:05 sample.tar
-rw-rw-r-- 1 kelly kelly     0 Jan 26 17:39 sample.txt
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ls -l /tmp
total 1036
-rw-rw-r-- 1 kelly kelly 1048624 Feb  3 10:45 1m.txt
drwxrwxr-x 2 kelly kelly    4096 Jan 18 17:54 dirlevel01
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f01
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f02
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f03
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f04

我们可以发现,打包文件的大小甚至比打包前所有文件的大小之和还要大,原因是打包时tar命令会添加一些额外的信息,从而印证了tar命令是不会压缩文件的。

⑵ 打包整个目录
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll /tmp
total 1036
drwxrwxr-x 2 kelly kelly    4096 Feb  7 14:27 dirlevel01
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f01
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f02
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f03
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f04
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo tar -cf samdir.tar /tmp/dirlevel01
tar: Removing leading `/' from member names
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll
total 184
-rw-rw-r-- 1 kelly kelly    37 Feb  5 15:36 file05
-rw-rw-r-- 1 kelly kelly    26 Feb  5 15:36 file06
-rw-r--r-- 1 root  root  10240 Feb  7 14:31 samdir.tar
-rw-rw-r-- 1 kelly kelly     0 Jan 26 17:39 sample.txt
drwxrwxr-x 2 kelly kelly  4096 Feb  5 16:02 test
⑶ 列出打包文件中的所有文件名
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ tar -tf ./samdir.tar 
tmp/dirlevel01/
tmp/dirlevel01/t03.txt
tmp/dirlevel01/t01.txt
tmp/dirlevel01/t02.txt
⑷ 用delete选项删除打包文件中的某个特定文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ tar -f ./sample.tar --delete tmp/f04        
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ tar -tf ./sample.tar
tmp/f01
tmp/f02
tmp/f03
注意:

delete是一个长选项,因此前面有两个连字符而不是一个,要删除的文件名要紧跟在delete选项后面,用空格隔开。

⑸ 使用-A将文件包samdir.tar合并到sample.tar
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ tar -f ./sample.tar -A ./samdir.tar
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ tar -tf ./sample.tar
tmp/f01
tmp/f02
tmp/f03
tmp/dirlevel01/
tmp/dirlevel01/t03.txt
tmp/dirlevel01/t01.txt
tmp/dirlevel01/t02.txt
⑹ 用-r选项向打包文件中添加新文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ tar -f ./sample.tar -r ~/file05
tar: Removing leading `/' from member names
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ tar -tf ./sample.tar
tmp/f01
tmp/f02
tmp/f03
tmp/dirlevel01/
tmp/dirlevel01/t03.txt
tmp/dirlevel01/t01.txt
tmp/dirlevel01/t02.txt
home/kelly/file05
⑺ 使用-xf解包到指定要解包的目录
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ tar -xf ./sample.tar -C ./test
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ls -Rl ./test 
./test:
total 12
-rwx------ 1 kelly kelly   35 Jan 15 23:32 hello.sh
drwxrwxr-x 3 kelly kelly 4096 Feb  7 15:58 home
-rw-rw-r-- 1 kelly kelly    0 Jan 28 16:53 test01.txt
-rw-rw-r-- 1 kelly kelly    0 Feb  5 16:02 test02.txt
drwxrwxr-x 3 kelly kelly 4096 Feb  7 15:58 tmp

./test/home:
total 4
drwxrwxr-x 2 kelly kelly 4096 Feb  7 15:58 kelly

./test/home/kelly:
total 4
-rw-rw-r-- 1 kelly kelly 37 Feb  5 15:36 file05

./test/tmp:
total 4
drwxrwxr-x 2 kelly kelly 4096 Feb  7 14:27 dirlevel01
-rw-rw-r-- 1 kelly kelly    0 Feb  5 17:01 f01
-rw-rw-r-- 1 kelly kelly    0 Feb  5 17:01 f02
-rw-rw-r-- 1 kelly kelly    0 Feb  5 17:01 f03

./test/tmp/dirlevel01:
total 0
-rw-rw-r-- 1 kelly kelly 0 Feb  7 14:27 t01.txt
-rw-rw-r-- 1 kelly kelly 0 Feb  7 14:27 t02.txt
-rw-rw-r-- 1 kelly kelly 0 Feb  7 14:27 t03.txt
注意:

解包时,默认解包目录是当前目录,我们可以用-C选项来指定要解包的目录

② 压缩文件:gzip
gzip 要压缩的文件 | 列表
⑴ 压缩文件sample.tar
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll
total 200
-rw-r--r-- 1 root  root  10240 Feb  7 14:31 samdir.tar
-rw-rw-r-- 1 kelly kelly 20480 Feb  7 15:14 sample.tar
drwxrwxr-x 4 kelly kelly  4096 Feb  7 15:58 test
-rw-r--r-- 1 root  root  10240 Feb  5 16:03 test.tar
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ gzip ./sample.tar
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll
total 184
-rw-r--r-- 1 root  root  10240 Feb  7 14:31 samdir.tar
-rw-rw-r-- 1 kelly kelly   318 Feb  7 15:14 sample.tar.gz
drwxrwxr-x 4 kelly kelly  4096 Feb  7 15:58 test
-rw-r--r-- 1 root  root  10240 Feb  5 16:03 test.tar
⑵ 解压文件sample.tar.gz
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ gzip -d sample.tar.gz
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll
total 200
-rw-r--r-- 1 root  root  10240 Feb  7 14:31 samdir.tar
-rw-rw-r-- 1 kelly kelly 20480 Feb  7 15:14 sample.tar
drwxrwxr-x 4 kelly kelly  4096 Feb  7 15:58 test
-rw-r--r-- 1 root  root  10240 Feb  5 16:03 test.tar
⑶ 调节压缩比

压缩比功能通过一个数字选项实现,这个数字可以是1到0之间的任意整数,1表示最低压缩比,也即压缩后的文件最大,但压缩过程最快;9代表高压缩比,也即压缩后的文件最小,但压缩过程最慢;如果不加这个数字选项,gzip默认采用6这个值。
压缩同样大小的文件,使用不同的压缩比例进行对比

[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ ll
total 1300
-rw-rw-r-- 1 kelly kelly 438663 Feb  7 17:42 f01
-rw-rw-r-- 1 kelly kelly 438663 Feb  7 17:42 f02
-rw-rw-r-- 1 kelly kelly 438663 Feb  7 17:42 f03
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ gzip -9 f01
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ gzip   f02
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ gzip -1 f03
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ ll
total 28
-rw-rw-r-- 1 kelly kelly  4020 Feb  7 17:42 f01.gz
-rw-rw-r-- 1 kelly kelly  4536 Feb  7 17:42 f02.gz
-rw-rw-r-- 1 kelly kelly 11378 Feb  7 17:42 f03.gz
⑷ 打包压缩联合使用
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ tar -czf test.tar.gz /tmp/f01 /tmp/f02 /tmp/f03
tar: Removing leading `/' from member names
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll
total 204
-rw-r--r-- 1 root  root  10240 Feb  7 14:31 samdir.tar
-rw-rw-r-- 1 kelly kelly 20480 Feb  7 15:14 sample.tar
drwxrwxr-x 4 kelly kelly  4096 Feb  7 15:58 test
-rw-r--r-- 1 root  root  10240 Feb  5 16:03 test.tar
-rw-rw-r-- 1 kelly kelly   147 Feb  7 17:47 test.tar.gz
⑸ 解压缩解包联合使用
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ tar -xzf test.tar.gz 
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll
total 204
-rw-r--r-- 1 root  root  10240 Feb  7 14:31 samdir.tar
-rw-rw-r-- 1 kelly kelly 20480 Feb  7 15:14 sample.tar
drwxrwxr-x 4 kelly kelly  4096 Feb  7 15:58 test
-rw-r--r-- 1 root  root  10240 Feb  5 16:03 test.tar
-rw-rw-r-- 1 kelly kelly   147 Feb  7 17:47 test.tar.gz
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll /tmp/
total 1036
-rw-rw-r-- 1 kelly kelly 1048624 Feb  3 10:45 1m.txt
drwxrwxr-x 2 kelly kelly    4096 Feb  7 14:27 dirlevel01
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f01
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f02
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f03
③ 查找文件:find
find [查找范围] [查找条件] [动作]
⑴ 根据文件名查找:查找系统中名为 passwd 文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find /etc -name passwd
/etc/passwd
/etc/pam.d/passwd
注意:文件名需要精确名称,而不是关键字
⑵ 根据文件类型查找:-type
查找系统中,类型为l(符号链接文件)的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find /tmp -type l
/tmp/hello.sh_slink
⑶ 根据文件时间属性查找:-mtime -mmin -ctime -cmin -atime -amin(用法一模一样)
-mtime 和 -mmin:用法和文件内容修改时间相关
-atime 和 -amin:和文件被读取或执行时间相关
-ctime 和 -cmin:和文件属性修改时间相关

以-mtime -mmin为例子
查找tmp目录下3天内,内容发生改变的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find /tmp -mtime -3 -ls
1179649    4 drwxrwxrwt   9 root     root         4096 Jan 28 03:24 /tmp
1189215    0 lrwxrwxrwx   1 kelly    kelly          13 Jan 26 15:50 /tmp/hello.sh_slink -> /tmp/hello.sh

查找tmp目录下7天前,内容发生改变的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find /tmp -mtime +7 -ls
1179662    4 drwxrwxrwt   2 root     root         4096 Mar 29  2020 /tmp/.ICE-unix
1179658    4 drwxrwxrwt   2 root     root         4096 Mar 29  2020 /tmp/.font-unix

查找tmp目录下两天前的那天,内容发生改变的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find /tmp -mtime 2 -ls
1189215    0 lrwxrwxrwx   1 kelly    kelly          13 Jan 26 15:50 /tmp/hello.sh_slink -> /tmp/hello.sh

+n表示n天之前
-n表示n天之内
n表示正好在第n天

-mtime和-mmin的用法是一样的,只不过把时间单位改成了分钟
查找tmp目录下10分钟之前,内容发生改变的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find /tmp -mmin +10 -ls  
1179649    4 drwxrwxrwt   9 root     root         4096 Jan 28 16:45 /tmp
1179662    4 drwxrwxrwt   2 root     root         4096 Mar 29  2020 /tmp/.ICE-unix
1179658    4 drwxrwxrwt   2 root     root         4096 Mar 29  2020 /tmp/.font-unix

查找tmp目录下两小时之内,内容发生改变的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find /tmp -mmin -120 -ls
1179649    4 drwxrwxrwt   9 root     root         4096 Jan 28 16:45 /tmp
⑷ 根据文件大小查找:-size
查找tmp目录下,比3k字节小的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find /tmp -size -3k -ls
1189215    0 lrwxrwxrwx   1 kelly    kelly          13 Jan 26 15:50 /tmp/hello.sh_slink -> /tmp/hello.sh

查找tmp目录下,比100字节大的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find /tmp -size +100c -ls
1179649    4 drwxrwxrwt   9 root     root         4096 Jan 28 16:45 /tmp
1179662    4 drwxrwxrwt   2 root     root         4096 Mar 29  2020 /tmp/.ICE-unix
1179658    4 drwxrwxrwt   2 root     root         4096 Mar 29  2020 /tmp/.font-unix

查找tmp目录下,大小正好10m字节的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find /tmp -size 1M -ls 
1189225 1024 -rw-rw-r--   1 kelly    kelly     1048576 Jan 29 14:10 /tmp/1m.txt
⑸ 根据文件拥有者和从属用户组的属性进行查找:相关条件选项:-user,-uid,-group,-gid
查看用户的uid和gid
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ sudo cat /etc/passwd | grep kelly
kelly:x:1000:1000::/home/kelly:/bin/bash

在tmp目录下,查找拥有者是root用户的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ find /tmp -user root -ls
1179649    4 drwxrwxrwt   9 root     root         4096 Jan 29 17:53 /tmp
1179662    4 drwxrwxrwt   2 root     root         4096 Mar 29  2020 /tmp/.ICE-unix
1179658    4 drwxrwxrwt   2 root     root         4096 Mar 29  2020 /tmp/.font-unix

在tmp目录下,查找拥有者用户ID是1000的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ sudo find /tmp -uid 1000 -ls    
1189224    4 drwxrwxr-x   2 kelly    kelly        4096 Jan 18 17:54 /tmp/dirlevel01
1189225 1024 -rw-rw-r--   1 kelly    kelly     1048576 Jan 29 14:10 /tmp/1m.txt

在tmp目录下,查找从属于kelly用户组的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ sudo find /tmp -group kelly -ls
1189224    4 drwxrwxr-x   2 kelly    kelly        4096 Jan 18 17:54 /tmp/dirlevel01
1189225 1024 -rw-rw-r--   1 kelly    kelly     1048576 Jan 29 14:10 /tmp/1m.txt

在tmp目录下,查找从属于gid为1000的用户组的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ sudo find /tmp -gid 1000 -ls        
1189224    4 drwxrwxr-x   2 kelly    kelly        4096 Jan 18 17:54 /tmp/dirlevel01
1189225 1024 -rw-rw-r--   1 kelly    kelly     1048576 Jan 29 14:10 /tmp/1m.txt
⑹ 根据文件的权限进行查找:-perm
在tmp目录下,查找权限位644的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ sudo find /tmp -perm 664 -ls
1189225 1024 -rw-rw-r--   1 kelly    kelly     1048576 Jan 29 14:10 /tmp/1m.txt
⑺ 联合条件查找文件:与 -a;或 -o;非 !
知识点:

1、条件要用圆括号括起来
2、左右圆括号要用反斜杠来转义
3、左右圆括号和条件项之间应各有一个空格

在/tmp目录下,查找大小大于1k小于1M,在30分钟内文件内容被修改过的普通文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find /tmp \( -size +1k -a -size -10M -a -mmin -30 -a -type f \) -ls
1189225 1028 -rw-rw-r--   1 kelly    kelly     1048624 Feb  3 10:45 /tmp/1m.txt
⑻ 使用联合条件查找文件后,统一处理:-exec + shell命令
在/tmp目录下,查找大小大于1k小于1M的普通文件,并将它们都删除掉
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find /tmp \( -size +1k -a -size -10M -a -mmin -30 -a -type f \) -exec rm -rf {} \;
注意:
-ok和-exec的用处基本相同,-ok在执行每一个命令之前都会给出提示,让用户来确定是否执行,在删除文件时可以考虑使用-ok选项。

在当前用户家目录下,查找在10分钟内被创建或者被修改过的目录文件,并且将他们打包成与目录同名,后缀为.tar的打包文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo find ~ \( -mmin -10 -a -type d \) -exec tar -cf {}.tar {} \;
tar: Removing leading `/' from member names
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ls
file05  file06  file07_rename  hello_bak.sh  hello.sh  hello_slink  sample_hl02  sample.txt  test  test01  test.tar
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ tar -tf ./test.tar
home/kelly/test/
home/kelly/test/hello.sh
home/kelly/test/test02.txt
home/kelly/test/test01.txt
④ 查看文本文件
⑴ cat (concatenate)
查看etc下,passwd的文本文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ cat /etc/passwd
注意:

cat命令是一次将文件的全部内容都输出到命令行上,如果文件较大,输出端内容超出缓冲区的话,文件前面的内容就看不到了,而且cat命令也不提供翻页和查找的功能,因此只适合查找较小的文本文件。

⑵ less
查看log下,message的文本文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo less /var/log/messages
用法:

1、使用键盘上的上下箭头按键翻行,用page up和page down按键来上下翻页
2、假设查找带有“root”关键字的文本:;root关键字高亮显示
3、使用 n 可以跳到下一个关键字处,使用 N 回到上一个关键字处
4、查看完毕后,使用 q 退出less,回到命令行

⑶ head
查看message文件头20行(如不足20行则全部显示)
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo head -n 20 /var/log/messages
不跟-n和参数,查看文本文件时,默认显示文件头10行
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ sudo head /var/log/messages


⑷ tail

查看文本末尾几行,用法与head一样,此处不再赘述。

⑤ 通配符

*:代表任意长度字符串
*.txt:名字以.txt结尾的所有文件
删除tmp目录下所有以.txt结尾的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll /tmp
total 1036
-rw-rw-r-- 1 kelly kelly 1048624 Feb  3 10:45 1m.txt
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f01
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f02
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f03
-rw-rw-r-- 1 kelly kelly       0 Feb  5 17:01 f04
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ rm -f /tmp/*.txt
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll /tmp
total 8
-rw-rw-r-- 1 kelly kelly    0 Feb  5 17:01 f01
-rw-rw-r-- 1 kelly kelly    0 Feb  5 17:01 f02
-rw-rw-r-- 1 kelly kelly    0 Feb  5 17:01 f03
-rw-rw-r-- 1 kelly kelly    0 Feb  5 17:01 f04
?:代表任意单个字符
列出在bin目录下,所有长度为4个字符,并以sh结尾的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll /bin/??sh
-rwxr-xr-x 1 root root 964536 Apr  1  2020 /bin/bash
-rws--x--x 1 root root  23880 Apr  1  2020 /bin/chsh
[C1-C2],匹配c1-c2字符序列中的任意单一字符
将tmp目录下,名字以f0字样打头的,紧接0-9之间一个整数的文件,拷贝到当前用户家目录下
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ cp /tmp/f0[0-9] ~         
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll
total 204
-rw-rw-r-- 1 kelly kelly     0 Feb  8 17:55 f01
-rw-rw-r-- 1 kelly kelly     0 Feb  8 17:55 f02
-rw-rw-r-- 1 kelly kelly     0 Feb  8 17:55 f03
-rw-rw-r-- 1 kelly kelly     0 Feb  8 17:55 f04
-rw-r--r-- 1 root  root  10240 Feb  7 14:31 samdir.tar
-rw-rw-r-- 1 kelly kelly 20480 Feb  7 15:14 sample.tar
-rw-r--r-- 1 root  root  10240 Feb  5 16:03 test.tar
-rw-rw-r-- 1 kelly kelly   147 Feb  7 17:47 test.tar.gz
[C1,C2,...,Cn],匹配中括号之间字符列表中的任意单一字符
将tmp目录下,名字以f0字样打头的,紧接1 2 3中的任意一个字符的文件的访问权限改为700
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ chmod 700 /tmp/f0[1,2,3]
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll /tmp
total 8
-rwx------ 1 kelly kelly    0 Feb  5 17:01 f01
-rwx------ 1 kelly kelly    0 Feb  5 17:01 f02
-rwx------ 1 kelly kelly    0 Feb  5 17:01 f03
-rw-rw-r-- 1 kelly kelly    0 Feb  5 17:01 f04
{string1,string2,...,stringN},匹配string1,string2直至stringN中的某个字符串
删除当前用户家目录下,名字以f或者file字样打头的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll
total 204
-rw-rw-r-- 1 kelly kelly     0 Feb  8 17:55 f01
-rw-rw-r-- 1 kelly kelly     0 Feb  8 17:55 f02
-rw-rw-r-- 1 kelly kelly     0 Feb  8 17:55 f03
-rw-rw-r-- 1 kelly kelly     0 Feb  8 17:55 f04
-rw-rw-r-- 1 kelly kelly    37 Feb  5 15:36 file05
-rw-rw-r-- 1 kelly kelly    26 Feb  5 15:36 file06
-rw-rw-r-- 1 kelly kelly     0 Jan 15 15:55 file07_rename
-rw-r--r-- 1 root  root  10240 Feb  7 14:31 samdir.tar
-rw-rw-r-- 1 kelly kelly 20480 Feb  7 15:14 sample.tar
drwxrwxr-x 4 kelly kelly  4096 Feb  7 15:58 test
-rw-r--r-- 1 root  root  10240 Feb  5 16:03 test.tar
-rw-rw-r-- 1 kelly kelly   147 Feb  7 17:47 test.tar.gz
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ rm -rf {f,file}*
[kelly@iZbp18hjhpsqpbmjdflxkcZ ~]$ ll
total 196
-rw-r--r-- 1 root  root  10240 Feb  7 14:31 samdir.tar
-rw-rw-r-- 1 kelly kelly 20480 Feb  7 15:14 sample.tar
drwxrwxr-x 4 kelly kelly  4096 Feb  7 15:58 test
-rw-r--r-- 1 root  root  10240 Feb  5 16:03 test.tar
-rw-rw-r-- 1 kelly kelly   147 Feb  7 17:47 test.tar.gz
!:一般和[ ],{ }联用,表示对其中内容的取反
在tmp目录下,名字以test字样打头,紧跟着一个除0-9之间数字以外的任意字符的文件,打包成为一个叫做test.tar的文件
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ ll
total 8
-rwx------ 1 kelly kelly    0 Feb  5 17:01 f01
-rwx------ 1 kelly kelly    0 Feb  5 17:01 f02
-rwx------ 1 kelly kelly    0 Feb  5 17:01 f03
-rw-rw-r-- 1 kelly kelly    0 Feb  5 17:01 f04
-rw-rw-r-- 1 kelly kelly    0 Feb  8 18:19 test1
-rw-rw-r-- 1 kelly kelly    0 Feb  8 18:19 test2
-rw-rw-r-- 1 kelly kelly    0 Feb  8 18:19 test3
-rw-rw-r-- 1 kelly kelly    0 Feb  8 18:19 testx
-rw-rw-r-- 1 kelly kelly    0 Feb  8 18:19 testy
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ tar -cf test.tar /tmp/test[!0-9]
tar: Removing leading `/' from member names
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ ll
total 20
-rwx------ 1 kelly kelly     0 Feb  5 17:01 f01
-rwx------ 1 kelly kelly     0 Feb  5 17:01 f02
-rwx------ 1 kelly kelly     0 Feb  5 17:01 f03
-rw-rw-r-- 1 kelly kelly     0 Feb  5 17:01 f04
-rw-rw-r-- 1 kelly kelly     0 Feb  8 18:19 test1
-rw-rw-r-- 1 kelly kelly     0 Feb  8 18:19 test2
-rw-rw-r-- 1 kelly kelly     0 Feb  8 18:19 test3
-rw-rw-r-- 1 kelly kelly 10240 Feb  9 10:51 test.tar
-rw-rw-r-- 1 kelly kelly     0 Feb  8 18:19 testx
-rw-rw-r-- 1 kelly kelly     0 Feb  8 18:19 testy
[kelly@iZbp18hjhpsqpbmjdflxkcZ tmp]$ tar -tf /tmp/test.tar 
tmp/testx
tmp/testy

通配符的使用非常灵活,本篇只是讲述比较常用的部分,有兴趣的大佬可以去看看相关资料哈~


Linux文件篇完结啦!!学习过程真的是呕心沥血哈哈哈哈~本来以为执行过程很顺利的,结果还是时不时卡住。下一篇是Linux用户篇,继续努力,gogogo!

你可能感兴趣的:(Linux-如何成为一个优秀的管家(文件篇②))