9: 文件查找和打包压缩

1 文件查找

在文件系统上, 通过查找符合指定模式的文件

  • 非实时查找, 基于索引数据库: locate
  • 实时查找: find

1.1 locate

locate查询系统上预先创建的文件索引数据库, /var/lib/mlocate/mlocate.db
索引的构建是在系统较为空闲时自动进行(周期性任务), 执行updatedb可以手动更新数据库
索引构建过程需要遍历整个根文件系统,很消耗资源

工作特点:

  • 查找速度快
  • 模糊查找
  • 非实时查找
  • 只要目录或文件名包含指定的搜索模式, 就会返回
  • 只搜索用户具备读和执行权限的目录

格式:

locate [OPTION]... [PATTERN]...

常用选项:

-i 不区分大小写的搜索
-n N 只列举前N个匹配项目
-r 使用基本正则表达式

范例: 使用mlocate.db构建数据库

[01:58:04 root@centos8-2 ~]#yum -y install mlocate
[01:58:04 root@centos8-2 ~]#locate conf  # 查询路径和文件名中包含conf的, 是模糊搜索
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
[01:58:30 root@centos8-2 ~]#ll /var/lib/mlocate/mlocate.db
ls: cannot access '/var/lib/mlocate/mlocate.db': No such file or directory
[01:58:48 root@centos8-2 ~]#updatedb
[01:59:03 root@centos8-2 ~]#ll /var/lib/mlocate/mlocate.db
-rw-r----- 1 root slocate 1248982 Aug 20 01:59 /var/lib/mlocate/mlocate.db
[02:01:49 root@centos8-2 ~]#locate -n 3 conf # 只查看前三个匹配的文件
/boot/config-4.18.0-193.el8.x86_64
/boot/grub2/i386-pc/configfile.mod
/boot/loader/entries/f072eccf217a4374b5b44dc4461b3c54-0-rescue.conf

eg: 搜索文件名称或目录路径中包含"conf"的文件名或目录

locate conf #这也是locate默认功能, locate '关键字', 搜索名称或路径中包含'关键字'的文件或路径, 属于模糊搜索

eg: 搜索以'.conf'结尾的文件, 需要使用正则表达式

locate -r '\.conf$'

eg:

[02:05:47 root@centos8-2 ~]#locate -n 10 -ir '\.conf$'
/boot/loader/entries/f072eccf217a4374b5b44dc4461b3c54-0-rescue.conf
/boot/loader/entries/f072eccf217a4374b5b44dc4461b3c54-4.18.0-193.el8.x86_64.conf
/etc/dracut.conf
/etc/fuse.conf
/etc/host.conf
/etc/idmapd.conf
/etc/kdump.conf
/etc/krb5.conf
/etc/ld.so.conf
/etc/libaudit.conf
[02:07:13 root@centos8-2 ~]#locate -n 10 -ir '\.CONF$'
/boot/loader/entries/f072eccf217a4374b5b44dc4461b3c54-0-rescue.conf
/boot/loader/entries/f072eccf217a4374b5b44dc4461b3c54-4.18.0-193.el8.x86_64.conf
/etc/dracut.conf
/etc/fuse.conf
/etc/host.conf
/etc/idmapd.conf
/etc/kdump.conf
/etc/krb5.conf
/etc/ld.so.conf
/etc/libaudit.conf

新创建的文件, 使用locate是不能立即查到的

eg:

[02:09:14 root@centos8-2 /data/prac]#touch test.txt
[02:09:21 root@centos8-2 /data/prac]#locate test.txt
/usr/share/doc/pcre-devel/pcretest.txt
/usr/share/doc/pcre-devel/perltest.txt
/usr/share/doc/pcre2-devel/pcre2test.txt

需要手动更新mlocate.db

[02:09:25 root@centos8-2 /data/prac]#updatedb
[02:10:00 root@centos8-2 /data/prac]#locate test.txt
/data/prac/test.txt
/usr/share/doc/pcre-devel/pcretest.txt
/usr/share/doc/pcre-devel/perltest.txt
/usr/share/doc/pcre2-devel/pcre2test.txt
[02:10:03 root@centos8-2 /data/prac]#
[02:10:03 root@centos8-2 /data/prac]#touch hahaha.sh
[02:10:33 root@centos8-2 /data/prac]#locate hahaha.sh
[02:10:38 root@centos8-2 /data/prac]#

rm删除文件后, 还是可以用locate命令查到, 因为locate不是实时查找, 而是基于系统预先构建的mlocate.db

eg:

[02:12:30 root@centos8-2 /data/prac]#rm -rf test.txt
[02:12:42 root@centos8-2 /data/prac]#locate test.txt
/data/prac/test.txt

eg: locate可以同时搜索多个模式

[18:35:28 root@centos8 ~]#locate f1.txt test.txt
/root/f1.txt
/root/test.txt

1.2 find命令

find是实时查找, 通过遍历指定路径完成文件查找

工作特点:

  • 查找速度略慢
  • 精确查找
  • 实时查找
  • 查找条件丰富
  • 只搜索用户具备读和执行权限的目录

格式:

find [OPTION] ... [查找路径] [查找条件] [处理动作]

查找路径: 指定具体目标路径, 默认为当前目录
查找条件: 指定的查找标准, 可以为文件名, 大小, 类型, 权限等标准进行; 默认为找出指定路径下的所有文件
处理动作: 对符合条件的文件做操作, 默认输出至屏幕

注意: find默认会递归查找整个目录, 并把结果打印在屏幕

[root@demo-c8 ~]# tree 
.
├── anaconda-ks.cfg
├── d1
│?? ├── d2
│?? │?? ├── d3
│?? │?? │?? └── f3.txt
│?? │?? └── f2.txt
│?? └── f1.txt
├── dead.letter
├── Desktop
├── Documents
├── Downloads
├── initial-setup-ks.cfg
├── mail.txt
├── Music
├── Pictures
├── Public
├── Templates
└── Videos
[root@demo-c8 ~]# find
....
./Desktop
./Downloads
./Templates
./Public
./Documents
./Music
./Pictures
./Videos
./.pki
./.pki/nssdb
./.Xauthority
./.bash_history
./mail.txt
./dead.letter
./.mailrc
./.viminfo
./d1
./d1/d2
./d1/d2/d3
./d1/d2/d3/f3.txt
./d1/d2/f2.txt
./d1/f1.txt
[root@demo-c8 ~]# find | wc -l
163

1.2.1 指定搜索目录深度

-maxdepth level 最大搜索目录深度, 默认把指定的查找目录下的文件当做为第1级. 比如 find /root, 那么/root目录下的文件就是第一级
-mindepth level 最小搜索目录深度 
-maxdepth 2 -mindepth 2: 精确查找第二级2目录下的内容

/etc/fstab                     为第一级
/etc/samba/smb.conf            为第二级
/etc/dnf/protected.d/yum.conf  为第三级
1. find /etc -maxdepth 2 -mindepth 2, 精确的定位到第二级, 只搜索第二级目录下的内容

2. find /etc -maxdepth 3 -mindepth 1, 第一级到第三极

1.2.2 设置对每个目录, 先处理目录内的文件,再处理目录本身

默认先处理目录, 再处理文件

[root@demo-c8 ~]# tree /opt
/opt
├── d1
│   ├── d2
│   │   ├── d3
│   │   │   └── f4.txt
│   │   └── f3.txt
│   └── f2.txt
└── f1.txt

3 directories, 4 files
[root@demo-c8 ~]# find /opt 
/opt  # 默认先处理目录
/opt/d1
/opt/d1/d2
/opt/d1/d2/d3
/opt/d1/d2/d3/f4.txt  # 再从最内层开始处理文件
/opt/d1/d2/f3.txt
/opt/d1/f2.txt
/opt/f1.txt
find -d # the -d option is deprecated
find -depth # 推荐使用
[root@demo-c8 ~]# find /opt  -depth
/opt/d1/d2/d3/f4.txt
/opt/d1/d2/d3
/opt/d1/d2/f3.txt
/opt/d1/d2
/opt/d1/f2.txt
/opt/d1
/opt/f1.txt
/opt

1.2.3 根据文件名和inode查找

-name "文件名称": 支持glob, 如: *, ?, [], [^], 通配符要加单或双引号引起来, 否则只会搜索当前工作目录, 不会递归搜索
-iname "文件名称": 不区分字母大小写, 默认-name区分大小写
-inum n 按inode号查找
-samefile name 相同inode号的文件, 也就是硬链接文件
-links n 链接数为n的文件
-regex "PATTERN": 以PATTERN匹配整个文件路径, 而非文件名称
# 通过精确的名字查找
# 搜索/etc目录下的文件时, 就无须再指定/etc目录路径了, 直接在模式写fstab即可
# 此时, 只有/etc目录下, 文件名是fstab的文件才符合要求, -name查找, 在不使用通配符的情况下, 是精确查找

[root@demo-c8 ~]# find /etc -name fstab
/etc/fstab
# 通过通配符查找

[root@demo-c8 ~]# find /opt -name "*.txt"
/opt/d1/d2/d3/f4.txt
/opt/d1/d2/f3.txt
/opt/d1/f2.txt
/opt/f1.txt

注意: 用正则表达式搜索时, 要匹配全路径

find /opt -regex ".*\.txt$"
root@demo-c8 ~]# find /opt -regex ".*\.txt$"
/opt/d1/d2/d3/f4.txt
/opt/d1/d2/f3.txt
/opt/d1/f2.txt
/opt/f1.txt

1.2.4 根据属主,属组查找

  • -user USERNAME: 查找属主为指定用户(UID)的文件
  • -group GRPNAME: 查找属组为指定组(GID)的文件
  • -uid UserID: 查找属主为指定的UID号的文件
  • -gid GroupID; 查找属组为指定的GID号的文件
  • -nouser: 查找没有属主的文件
  • -nogroup: 查找没有属组的文件
[11:24:57 root@centos8 ~]#find /home -user david
/home/david
/home/david/.bash_logout
/home/david/.bash_profile
/home/david/.bashrc

查找出结果后, 默认是print到当前屏幕上, 也可接后续处理动作, -CMD

[11:27:58 root@centos8 ~]#find /home -user david -ls
   373327      0 drwx------   2  david    david          62 Nov  2 11:24 /home/david
   377687      4 -rw-r--r--   1  david    david          18 Nov  9  2019 /home/david/.bash_logout
   377694      4 -rw-r--r--   1  david    david         141 Nov  9  2019 /home/david/.bash_profile
   377695      4 -rw-r--r--   1  david    david         312 Nov  9  2019 /home/david/.bashrc

查找没有所有者和所属组的文件

删除用户或组时, 如果没有加-r选项, 那么其家目录还会存在, 这时家目录中的文件就是没有所有者和所属组的, 可以通过find搜索出来然后删除
find 路径 -nouser -nogroup 
[root@demo-c8 ~]# useradd test
[root@demo-c8 ~]# ll /home
total 4
drwx------.  3 test test   78 Sep 11 19:34 test
drwx------. 15 wang wang 4096 Sep 11 18:37 wang
[root@demo-c8 ~]# userdel test
[root@demo-c8 ~]# ll /home
total 4
drwx------.  3 1001 1001   78 Sep 11 19:34 test
drwx------. 15 wang wang 4096 Sep 11 18:37 wang
[root@demo-c8 ~]# find /home -nouser -nogroup
/home/test
/home/test/.mozilla
/home/test/.mozilla/extensions
/home/test/.mozilla/plugins
/home/test/.bash_logout
/home/test/.bash_profile
/home/test/.bashrc

注意: find的查找条件, 默认多个条件是并且关系

1.2.5 指定文件类型

-type TYPE

TYPE可以是以下形式:

f: 普通文件
d: 目录文件
l: 符号链接文件
s: 套接字文件
b: 块设备文件
c: 字符设备文件
p: 管道文件

查找/etc目录下软链接文件

find /etc/ -type l

 67390048      0 lrwxrwxrwx   1  root     root           19 Aug 15 16:55 /etc/mtab -> ../proc/self/mounts
  1703258      0 lrwxrwxrwx   1  root     root           50 May 14  2019 /etc/fonts/conf.d/66-sil-nuosu.conf -> /usr/share/fontconfig/conf.avail/66-sil-nuosu.conf
...

查找/etc目录下的目录文件

find /etc -type -d

[root@demo-c8 ~]# find /etc -type d -ls | less
 67149953     12 drwxr-xr-x 135  root     root         8192 Sep 11 19:34 /etc
      134      0 drwxr-xr-x   8  root     root          128 Aug 15 16:53 /etc/dnf
...

1.2.6 查找空文件或目录

查找空文件或目录

find /etc/ -empty 

只查找空目录

-empty 和 -type d无先后顺序要求, -empty为空目录, -type d为查找目录, 取交集就是只查找空目录

find /etc -empty -type d

1.2.7 组合条件

与: -a, 默认多个条件是与关系
或: -o
非: -not 或 !

find  -empty -type d
find  -empty -o -type d
find -not  -empty
find ! -empty

德摩根定律:

  • (非A)或(非B)=非(A且B)
  • (非A)且(非B)=非(A或B)

示例:

!A -a !B=!(A -o B)
!A -o !B=!(A -a B)

范例: 查看属主和属组都是wang的文件

[root@demo-c8 ~]# find / -user wang -group wang
...
/home/wang/Desktop
/home/wang/Downloads
/home/wang/Templates
/home/wang/Public
/home/wang/Documents
/home/wang/Music
/home/wang/Pictures
/home/wang/Videos
/home/wang/.pki
/home/wang/.pki/nssdb
/home/wang/.bash_history

范例: 查到属主是wang, 属组不是wang的文件

[root@demo-c8 opt]# chown  wang f1.txt 
[root@demo-c8 opt]# ll
total 0
drwxr-xr-x. 3 root root 30 Sep 11 19:04 d1
-rw-r--r--. 1 wang root  0 Sep 11 19:03 f1.txt
[root@demo-c8 opt]# find /opt -user wang -not -group  wang
/opt/f1.txt

案例: 非A且非B

搜索/etc目录下, 不是目录也不是软链接的文件

find /etc ! \( -type d -o -type l \)
find /etc ! -type d -a ! -type l

案例: 非A或非B

find /etc ! \( -type d -a -type l \)
find /etc ! -type d -o ! -type l

1.2.8 搜索时排除目录

比如把/proc目录排除在外, 因为/proc存的都是内存信息, 没搜索的必要

[12:45:58 root@centos8 ~]#touch uptime
[12:51:03 root@centos8 ~]#find / -name uptime
/proc/uptime #  内存文件
/root/uptime  # 磁盘文件
/usr/bin/uptime # 磁盘命令
[12:52:54 root@centos8 ~]#find / -path '/proc' -a -prune -o -name uptime
/proc
/root/uptime
/usr/bin/uptime

范例: 查找/etc目录下, 但不包括/etc/sane.d目录, 所有以.conf后缀结尾的文件

[root@demo-c8 ~]# find /etc -path "/etc/sane.d" -a -prune -o -name "*.conf"

注意: 排除了目录后, 目录还是会显示, 但是搜索的时候并不会进到该目录

-path '/proc' -a -prune 相当于一组条件, 排除/proc目录, 优先级高
-name uptime 是一组条件, 排除了/proc目录进行搜索, 两者用-o或关系, -o的优先级低
所以排除目录和搜索条件的前后顺序无所谓
find /etc -path "/etc/sane.d" -a -prune -o -name "*.conf"
也可以把-path和-a括起来, 更直观一些, 把-path和-a作为整体
find /etc -name "*.conf"  -o  \( -path "/etc/sane.d" -a -prune \)

范例: 搜索时排除多个目录, 用-o相连, \(-path "路径A" -o -path "路径B" \)

[19:09:29 root@centos8 ~]#find / \( -path '/proc' -o -path '/usr' \) -a -prune -o -name conf # 括号括起来后要有个空格隔开
/proc
/usr

范例: 查找/etc下, 除了/etc/sane.d和/etc/fonts两个目录的所有.conf后缀的文件

find /etc \( -path "/etc/sane.d" -o -path "/etc/fonts" \) -a -prune -o -name "*.conf"

范例: 排除/proc和/sys目录

[root@demo-c8 ~]# find / \( -path "/sys" -o -path "/proc" \) -a -prune -o -type f -name "*.conf"
[root@demo-c8 ~]# find / \( -path "/sys" -o -path "/proc" \) -a -prune -o -type f -a -mmin -1

1.2.9 按文件大小来查找

-size [+|-] #UNIT
  常用单位: k, M, G, c (字节), 大小写敏感

-#UNIT: [0,#-1]
  如: -6k 表示 [0,5k] 小于等于5k, 左右闭区间
      -1M表示小于等于0M, 也就是空文件
      查找小于等于指定大小的文件时, 要在指定的数字基础上, 加1

#UNIT: (#-1,#]
  如: 6k 表示 (5k,6k] 不带正负号, 就是 (数字减一, 原数字], 左开右闭
      1M表示(0M,1M)

+#UNIT: (#,∞)
  如: +6k 表示 (6k,∞) 大于6k, 左右开区间 

构建文件

  • dd命令可以用来构建测试文件
if表示从/dev/zero这个文件里拿数据
of表示生成的数据路径
bs默认表示以1个字节作为单位, byte, 也可以指定以1M为单位, 1M=1024Kb
count=1024表示生成1024个bs, 也就是1024个字节, 1024bytes=1Kbytes
[root@demo-c8 opt]# dd if=/dev/zero of=f1.img bs=1 count=1024
1024+0 records in
1024+0 records out
1024 bytes (1.0 kB, 1.0 KiB) copied, 0.0013233 s, 774 kB/s
[root@demo-c8 opt]# dd if=/dev/zero of=f2.img bs=1 count=2048
2048+0 records in
2048+0 records out
2048 bytes (2.0 kB, 2.0 KiB) copied, 0.00223205 s, 918 kB/s
[root@demo-c8 opt]# dd if=/dev/zero of=f3.img bs=1 count=1500
1500+0 records in
1500+0 records out
1500 bytes (1.5 kB, 1.5 KiB) copied, 0.00171508 s, 875 kB/s
[root@demo-c8 opt]# ll
total 12
-rw-r--r--. 1 root root 1024 Sep 11 21:18 f1.img
-rw-r--r--. 1 root root 2048 Sep 11 21:20 f2.img
-rw-r--r--. 1 root root 1500 Sep 11 21:20 f3.img

dd if=/dev/zero of=f4.img bs=1K count=1024
dd if=/dev/zero of=f5.img bs=1M count=2
dd if=/dev/zero of=f6.img bs=1K count=1000
[root@demo-c8 opt]# ll -h
total 4.0M
-rw-r--r--. 1 root root  1.0K Sep 11 21:18 f1.img
-rw-r--r--. 1 root root  2.0K Sep 11 21:20 f2.img
-rw-r--r--. 1 root root  1.5K Sep 11 21:20 f3.img
-rw-r--r--. 1 root root  1.0M Sep 11 21:26 f4.img
-rw-r--r--. 1 root root  2.0M Sep 11 21:26 f5.img
-rw-r--r--. 1 root root 1000K Sep 11 21:27 f6.img

注意: find不加正负号, 默认是范围查找

范例: 搜索条件为1024字节, 表示大小在(1023b到1024b]之间的文件

[root@demo-c8 opt]# ll -h
total 5.0M
-rw-r--r--. 1 root root  1.0K Sep 11 21:18 f1.img # 1k=1024b
-rw-r--r--. 1 root root  2.0K Sep 11 21:20 f2.img
-rw-r--r--. 1 root root  1.5K Sep 11 21:20 f3.img
-rw-r--r--. 1 root root  1.0M Sep 11 21:26 f4.img
-rw-r--r--. 1 root root  2.0M Sep 11 21:26 f5.img
-rw-r--r--. 1 root root 1000K Sep 11 21:27 f6.img
-rw-r--r--. 1 root root  1.0M Sep 11 21:36 f7.img

[root@demo-c8 opt]#find -size 1024 # 要写单位
[root@demo-c8 opt]#find -size 1024c #1024字节, 大于1023小于等于1024字节
./f1.img

范例: 搜索条件为1k, 表示大小在(0,1k]之间大小的文件

[root@demo-c8 opt]# find /home/wang -size 1k
 35079930      0 drwxr-xr-x   4  wang     wang           39 Aug 15 16:53 /home/wang/.mozilla
 68356819      0 drwxr-xr-x   2  wang     wang            6 May 14  2019 /home/wang/.mozilla/extensions
100663429      0 drwxr-xr-x   2  wang     wang            6 May 14  2019 /home/wang/.mozilla/plugins
   666369      4 -rw-r--r--   1  wang     wang           18 Nov  9  2019 /home/wang/.bash_logout
   666370      4 -rw-r--r--   1  wang     wang          141 Nov  9  2019 /home/wang/.bash_profile
   666371      4 -rw-r--r--   1  wang     wang          312 Nov  9  2019 /home/wang/.bashrc
101475485      0 drwx------  11  wang     wang          215 Aug 15 17:18 /home/wang/.config
   666253      4 -rw-------   1  wang     wang          256 Aug 15 17:18 /home/wang/.config/pulse/cookie
   666278      4 -rw-r--r--   1  wang     wang            1 Aug 15 17:18 /home/wang/.config/pulse/eaab49a3b5f746ae847c443ec9bc62c4-default-sink
   666279      4 -rw-r--r--   1  wang     wang            1 Aug 15 17:18 /home/wang/.config/pulse/eaab49a3b5f746ae847c443ec9bc62c4-default-source
 68360836      0 drwx------   3  wang     wang           27 Aug 15 17:18 /home/wang/.config/gnome-session
101475487      0 drwx------   2  wang     wang            6 Aug 15 17:18 /home/wang/.config/gnome-session/saved-session
   666254      0 drwx------   3  wang     wang           17 Aug 15 17:18 /home/wang/.config/ibus
 35080396      0 drwx------   2  wang     wang           53 Aug 15 17:18 /home/wang/.config/ibus/bus
 35080397      4 -rw-rw-r--   1  wang     wang          168 Aug 15 17:18 /home/wang/.config/ibus/bus/eaab49a3b5f746ae847c443ec9bc62c4-unix-0
   666255      0 drwxr-xr-x   2  wang     wang           18 Aug 15 17:18 /home/wang/.config/dconf
 35080404      0 drwx------   3  wang     wang           21 Aug 15 17:18 /home/wang/.config/evolution
 68360851      0 drwx------   2  wang     wang           33 Aug 15 17:18 /home/wang/.config/evolution/sources
 68360852      4 -rw-r--r--   1  wang     wang          993 Aug 15 17:18 /home/wang/.config/evolution/sources/system-proxy.source
101475497      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.config/goa-1.0
101475502      4 -rw-------   1  wang     wang          633 Aug 15 17:18 /home/wang/.config/user-dirs.dirs
101475503      4 -rw-rw-r--   1  wang     wang            5 Aug 15 17:18 /home/wang/.config/user-dirs.locale
 35080408      0 drwx------   2  wang     wang            6 Aug 15 17:18 /home/wang/.config/gconf
101475514      0 drwx------   2  wang     wang           23 Aug 15 17:18 /home/wang/.config/gtk-3.0
101475515      4 -rw-rw-r--   1  wang     wang          132 Aug 15 17:18 /home/wang/.config/gtk-3.0/bookmarks
101475405      4 -rw-rw-r--   1  wang     wang            3 Aug 15 17:18 /home/wang/.config/gnome-initial-setup-done
 35080429      0 drwxrwxr-x   2  wang     wang           22 Aug 15 17:18 /home/wang/.config/yelp
 35080430      4 -rw-rw-r--   1  wang     wang           51 Aug 15 17:18 /home/wang/.config/yelp/yelp.cfg
   666399      4 -rw-------   1  wang     wang           16 Aug 15 17:18 /home/wang/.esd_auth
   666401      4 -rw-------   1  wang     wang          310 Aug 15 17:18 /home/wang/.ICEauthority
   666397      0 drwx------   3  wang     wang           19 Aug 15 17:18 /home/wang/.local
 35080393      0 drwx------  13  wang     wang          230 Aug 15 17:18 /home/wang/.local/share
 68360838      0 drwx------   2  wang     wang           48 Aug 15 17:18 /home/wang/.local/share/keyrings
 68360843      4 -rw-------   1  wang     wang          105 Aug 15 17:18 /home/wang/.local/share/keyrings/login.keyring
 68360845      4 -rw-------   1  wang     wang          207 Aug 15 17:18 /home/wang/.local/share/keyrings/user.keystore
101475488      0 drwx------   2  wang     wang           38 Aug 15 17:18 /home/wang/.local/share/gnome-shell
 35080398      0 drwx------   7  wang     wang           79 Aug 15 17:18 /home/wang/.local/share/evolution
 68360844      0 drwxr-xr-x   4  wang     wang           33 Aug 15 17:18 /home/wang/.local/share/evolution/addressbook
101475491      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/evolution/addressbook/trash
 68360864      0 drwx------   3  wang     wang           39 Aug 15 17:18 /home/wang/.local/share/evolution/addressbook/system
101475517      0 drwx------   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/evolution/addressbook/system/photos
   666257      0 drwxr-xr-x   4  wang     wang           33 Aug 15 17:18 /home/wang/.local/share/evolution/calendar
 35080399      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/evolution/calendar/trash
   666274      0 drwx------   2  wang     wang           26 Aug 15 17:18 /home/wang/.local/share/evolution/calendar/system
   666275      4 -rw-r--r--   1  wang     wang          173 Aug 15 17:18 /home/wang/.local/share/evolution/calendar/system/calendar.ics
 68360846      0 drwxr-xr-x   3  wang     wang           19 Aug 15 17:18 /home/wang/.local/share/evolution/mail
101475492      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/evolution/mail/trash
   666258      0 drwxr-xr-x   3  wang     wang           19 Aug 15 17:18 /home/wang/.local/share/evolution/memos
 35080400      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/evolution/memos/trash
 68360847      0 drwxr-xr-x   3  wang     wang           19 Aug 15 17:18 /home/wang/.local/share/evolution/tasks
101475493      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/evolution/tasks/trash
 35080409      4 -rw-rw-r--   1  wang     wang          542 Aug 15 17:18 /home/wang/.local/share/gsettings-data-convert
 68360855      0 drwx------   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/sounds
101475504      0 drwx------   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/applications
   666266      0 drwxrwxr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/icc
101475507      0 drwxr-xr-x   4  wang     wang           44 Aug 15 17:18 /home/wang/.local/share/flatpak
   666269      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/flatpak/db
   666281      0 drwxr-xr-x   7  wang     wang           89 Aug 15 17:18 /home/wang/.local/share/flatpak/repo
   666286      4 -rw-r--r--   1  wang     wang           68 Aug 15 17:18 /home/wang/.local/share/flatpak/repo/config
 35080421      0 drwxr-xr-x   3  wang     wang           19 Aug 15 17:18 /home/wang/.local/share/flatpak/repo/tmp
 35080423      0 drwxrwxr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/flatpak/repo/tmp/cache
 68360867      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/flatpak/repo/extensions
101475394      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/flatpak/repo/state
   666284      0 drwxr-xr-x   5  wang     wang           49 Aug 15 17:18 /home/wang/.local/share/flatpak/repo/refs
 35080422      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/flatpak/repo/refs/heads
 68360868      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/flatpak/repo/refs/mirrors
101475395      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/flatpak/repo/refs/remotes
   666285      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/flatpak/repo/objects
   666272      0 drwxr-xr-x   3  wang     wang           18 Aug 15 17:18 /home/wang/.local/share/tracker
 35080414      0 drwxr-xr-x   2  wang     wang           73 Aug 15 17:19 /home/wang/.local/share/tracker/data
 68360860      0 drwxr-xr-x   2  wang     wang           37 Aug 15 17:18 /home/wang/.local/share/gnome-settings-daemon
 35080417      0 drwx------   2  wang     wang           43 Aug 15 17:19 /home/wang/.local/share/gvfs-metadata
 35080387      4 -rw-------   1  wang     wang           64 Aug 15 17:19 /home/wang/.local/share/gvfs-metadata/home
 68360875      0 drwx------   5  wang     wang           68 Aug 15 17:18 /home/wang/.local/share/webkitgtk
101475406      0 drwx------   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/webkitgtk/localstorage
   666296      0 drwx------   3  wang     wang           15 Aug 15 17:18 /home/wang/.local/share/webkitgtk/deviceidhashsalts
 35080431      0 drwx------   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/webkitgtk/deviceidhashsalts/1
 68360877      0 drwx------   3  wang     wang           23 Aug 15 17:18 /home/wang/.local/share/webkitgtk/databases
101475408      0 drwx------   2  wang     wang            6 Aug 15 17:18 /home/wang/.local/share/webkitgtk/databases/indexeddb
 68360842      0 drwx------  10  wang     wang          232 Aug 15 17:18 /home/wang/.cache
101475490      0 drwx------   2  wang     wang            6 Aug 15 17:18 /home/wang/.cache/libgweather
   666259      0 drwx------   8  wang     wang           94 Aug 15 17:18 /home/wang/.cache/evolution
 35080401      0 drwxr-xr-x   3  wang     wang           19 Aug 15 17:18 /home/wang/.cache/evolution/addressbook
 68360848      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.cache/evolution/addressbook/trash
101475494      0 drwxr-xr-x   3  wang     wang           19 Aug 15 17:18 /home/wang/.cache/evolution/calendar
   666260      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.cache/evolution/calendar/trash
 35080402      0 drwxr-xr-x   3  wang     wang           19 Aug 15 17:18 /home/wang/.cache/evolution/mail
 68360849      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.cache/evolution/mail/trash
101475495      0 drwxr-xr-x   3  wang     wang           19 Aug 15 17:18 /home/wang/.cache/evolution/memos
   666261      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.cache/evolution/memos/trash
 35080403      0 drwxr-xr-x   3  wang     wang           19 Aug 15 17:18 /home/wang/.cache/evolution/sources
 68360850      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.cache/evolution/sources/trash
101475496      0 drwxr-xr-x   3  wang     wang           19 Aug 15 17:18 /home/wang/.cache/evolution/tasks
   666262      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.cache/evolution/tasks/trash
   666264      0 drwxr-xr-x   2  wang     wang           31 Aug 15 17:18 /home/wang/.cache/gnome-shell
101475508      0 drwxr-xr-x   2  wang     wang          229 Aug 15 17:18 /home/wang/.cache/tracker
101475509      4 -rw-r--r--   1  wang     wang            2 Aug 15 17:18 /home/wang/.cache/tracker/db-version.txt
101475511      4 -rw-r--r--   1  wang     wang           11 Aug 15 17:18 /home/wang/.cache/tracker/db-locale.txt
101475519      4 -rw-r--r--   1  wang     wang           22 Aug 15 17:18 /home/wang/.cache/tracker/parser-version.txt
101475392      4 -rw-rw-r--   1  wang     wang           11 Aug 15 17:18 /home/wang/.cache/tracker/locale-for-miner-apps.txt
101475402      4 -rw-rw-r--   1  wang     wang           10 Aug 15 17:18 /home/wang/.cache/tracker/last-crawl.txt
101475403      4 -rw-rw-r--   1  wang     wang            5 Aug 15 17:18 /home/wang/.cache/tracker/first-index.txt
   666280      0 drwxrwxr-x   2  wang     wang           33 Aug 15 17:18 /home/wang/.cache/gstreamer-1.0
 68360866      0 drwxr-xr-x   3  wang     wang           26 Aug 15 17:18 /home/wang/.cache/flatpak
101475393      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.cache/flatpak/system-cache
101475400      0 drwxrwxr-x   5  wang     wang           55 Aug 15 17:18 /home/wang/.cache/gnome-software
   666290      0 drwxrwxr-x   3  wang     wang           23 Aug 15 17:18 /home/wang/.cache/gnome-software/fwupd
 35080427      0 drwxrwxr-x   3  wang     wang           18 Aug 15 17:18 /home/wang/.cache/gnome-software/fwupd/remotes.d
 68360873      0 drwxrwxr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.cache/gnome-software/fwupd/remotes.d/lvfs
 35080428      0 drwxrwxr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.cache/gnome-software/shell-extensions
 68360874      0 drwxrwxr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/.cache/gnome-software/odrs
 68360876      0 drwx------   3  wang     wang           25 Aug 15 17:18 /home/wang/.cache/yelp
101475407      0 drwx------   3  wang     wang           24 Aug 15 17:18 /home/wang/.cache/yelp/WebKitCache
   666297      0 drwx------   3  wang     wang           31 Aug 15 17:18 /home/wang/.cache/yelp/WebKitCache/Version\ 14
   666298      4 -rw-------   1  wang     wang            8 Aug 15 17:18 /home/wang/.cache/yelp/WebKitCache/Version\ 14/salt
 35080432      0 drwx------   2  wang     wang            6 Aug 15 17:18 /home/wang/.cache/yelp/WebKitCache/Version\ 14/Blobs
 35080405      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/Desktop
 68360853      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/Downloads
101475500      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/Templates
   666402      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/Public
 35080407      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/Documents
 68360854      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/Music
101475501      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/Pictures
   666405      0 drwxr-xr-x   2  wang     wang            6 Aug 15 17:18 /home/wang/Videos
 35080410      0 drwxrw----   3  wang     wang           19 Aug 15 17:18 /home/wang/.pki
 68360856      0 drwxrw----   2  wang     wang            6 Aug 15 17:18 /home/wang/.pki/nssdb
   666422      4 -rw-------   1  wang     wang          130 Sep 11 18:37 /home/wang/.bash_history

范例: 搜索条件为1024c, 表示大小在(1023b, 1024b]之间的文件

[18:38:52 root@centos-7-1 ~]#find -size 1024c  # (1023b,1024b]
./f1.img

1k和1024c是相等的, 但是 -size 1k,表示(0-1k], 大于0小于等于1k; 
而1024c表示(1023c,1024c], 大于1023字节, 小于等于1024字节

范例: 搜索条件为-1k, k表示的是[0,1-1k]也就是[0,0]=0, 文件大小为0

[root@demo-c8 opt]# ll -h
total 5.0M
-rw-r--r--. 1 root root  1.0K Sep 11 21:18 f1.img
-rw-r--r--. 1 root root  2.0K Sep 11 21:20 f2.img
-rw-r--r--. 1 root root  1.5K Sep 11 21:20 f3.img
-rw-r--r--. 1 root root  1.0M Sep 11 21:26 f4.img
-rw-r--r--. 1 root root  2.0M Sep 11 21:26 f5.img
-rw-r--r--. 1 root root 1000K Sep 11 21:27 f6.img
-rw-r--r--. 1 root root  1.0M Sep 11 21:36 f7.img
-rw-r--r--. 1 root root     0 Sep 11 21:49 uptime
[root@demo-c8 opt]# find /opt -size -1k #-1
/opt/uptime
[root@demo-c8 opt]# find /opt -size -1M
/opt/uptime

范例: 查找/opt目录下, 大于1M的文件

[root@demo-c8 opt]# ll -h
total 5.0M
-rw-r--r--. 1 root root  1.0K Sep 11 21:18 f1.img
-rw-r--r--. 1 root root  2.0K Sep 11 21:20 f2.img
-rw-r--r--. 1 root root  1.5K Sep 11 21:20 f3.img
-rw-r--r--. 1 root root  1.0M Sep 11 21:26 f4.img
-rw-r--r--. 1 root root  2.0M Sep 11 21:26 f5.img
-rw-r--r--. 1 root root 1000K Sep 11 21:27 f6.img
-rw-r--r--. 1 root root  1.0M Sep 11 21:36 f7.img
[root@demo-c8 opt]# find /opt -size +1M # +1M, 不包括1M
/opt/f5.img

1.2.10 按照时间戳查找

#以"天"为单位
-atime [+|-] #
-mtime
-ctime

-#: [0,#)
#: [#,#+1)
+#: [#+1,∞)


比如: #=3 
-3 表示[0,3)天, 不包括第三天 
3 表示[3,4) 三天到四天内, 包括三天不包括第四天
+3 [4,∞],表示大于四天, 包括第四天


#以"分钟"为单位
-amin
-mmin
-cmin

案例: 查看最近一分钟内被修改的文件, 最近一分钟[]0-59s], 最近一天[]0-23:59:59])

[19:58:28 root@centos8 ~]#find /etc/ -mmin -1

案例: 验证创建账号会修改哪些文件

[root@demo-c8 opt]# useradd haha
[root@demo-c8 opt]# find /etc -mmin -1
/etc
/etc/group
/etc/gshadow
/etc/passwd
/etc/shadow
/etc/subgid
/etc/subuid
image.png

1.2.11 根据权限查找

-perm [/|-]MODE

MODE: 直接加模式, 是精确匹配, 755, 所有者是7, 所属组是5, other是5

/MODE: 只要ugo, 任何一个对象能匹配一位即可, 或的关系
555表示以下6种情况:
属主有r
或者x
或者属组有r
或者x
或者other有r
或者x, 也就是只针对5来匹配, 但是5表示读和执行,所以只要任何一个对象有读或者执行的权限即可, 并不是精确匹配555权限

-MODE, 并且的关系, 必须每一个对象同时至少拥有对应的权限才行,
比如 555, 表示
owner有读且执行权限, group有读且执行权限, other有读且执行权限. 
这里的-MODE和MODE精确匹配不一样. 精确匹配是只每一位都是与对应的权限精确匹配, 不能多也不能少, 而-555是至少的意思, owner至少有rx, group至少有rx, other至少有rx, 至于w权限有没有并不关心

/和-是模糊匹配,  而MODE是精确匹配
-555 -(r-x,r-x,r-x): owner至少有读且有执行, 并且group至少有读有执行, 并且其他人至少有读有执行
/555 owner有读或执行, 或者group有读或执行, 或者其他人有读或执行

案例:

555
精确匹配就是u-5, g-5, o-5
/555表示u有读或执行,或者g有读或执行,或者other有读或执行, 这里rwx也是有rw, 只要包含读或执行即可
-555,表示u有读且执行同时g有读且执行同时o有读且执行, 这里rwx也是有rw, 只要包含rw即可

如果是0则表示不关注这一对象的权限, 无所谓, 只对模糊查找有效

案例:

find -perm 755 会匹配权限模式恰好是755的文件
/222表示只要当任意人有写权限时, 就会匹配
-222表示每个人都有写权限时才匹配
-002表示只有当其他人有写权限时才会匹配, -002等于-2. 所有者和所属组权限不关心, 只关心other. 结果也等于/002, 注意 0是不关心, 不是匹配全部

/700 表示属主对目录有读或者写或者执行任意一个权限就行, 并不是必须同时拥有rwx

范例: 找到家目录中, 其他用户有写权限的文件, 然后把写权限删除

# 本范例, -002和/002是等价的
find ~ -perm -002 -exec chmod o-w () \;
find ~ -perm /002 -exec chmod o-w () \;

范例: 找到/data/prac目录下, 权限为644的并且以.sh结尾的普通文件, 然后加上执行权限

find /data/prac -perm 644 -type f -name '*.sh' -exec  chmod +x {} \;

1.2.12 处理动作

-print 默认处理的动作, 显示至屏幕
-ls 类似于对查找到的文件执行`ls -l`命令
-fls file 查找到的文件的长葛市信息保存至指定文件中, 相当于 -ls > file, 把找到的文件名称, 重定向到文件里
-delete 删除查找到的文件, 慎用, 因为会直接把返回的文件, 全部删除
-ok COMMAND {} \; 对查找到的每个文件,执行由COMMAND指定的命令, 对于每个文件执行命令之前, 会交互式要求用户确认
-exec COMMAND {} \; 对查找到的每个文件,执行由COMMAND指定的命令, 对于每个文件执行命令之前, 不会交互式要求用户确认
{ } 用于引用查找到的文件名称自身
COMMAND可以是任意Shell文件管理命令

范例: 把符合条件的文件信息, 另存到文件里

[root@demo-c8 opt]# find /etc -name "*.conf" -fls conf.txt
[root@demo-c8 opt]# ll
-rw-r--r--. 1 root root   46628 Sep 11 22:44 conf.txt

范例: 备份配置文件, 添加.orig扩展名

[21:11:34 root@CentOS-8 /data/prac]#touch lvs.conf nginx.conf httpd.conf
[21:18:19 root@CentOS-8 /data/prac]#find -name '*.conf' -exec  cp {} {}.orig \;

范例: 交互式删除/opt目录下, 以.img结尾的文件

root@demo-c8 ~]# find /opt -name "*.img" -ok rm {} \;
< rm ... /opt/f1.img > ? y
< rm ... /opt/f2.img > ? y
< rm ... /opt/f3.img > ? y
< rm ... /opt/f4.img > ? y
< rm ... /opt/f5.img > ? y
< rm ... /opt/f6.img > ? y
< rm ... /opt/f7.img > ? y

[root@demo-c8 ~]# ll /opt
total 48
-rw-r--r--. 1 root root 46628 Sep 11 22:44 conf.txt
-rw-r--r--. 1 root root     0 Sep 11 21:49 uptime

范例: 删除存在时间超过三天以上的属主为joe的临时文件

# 超过三天, 表示从第四天开始算起, 所以是+3, 表示[4, +∞)
[21:21:19 root@CentOS-8 /data/prac]#find /tmp -ctime +3 -a -user joe -ok rm {} \;

范例: 找到当前目录下大于100M的文件, 并且移动到/tmp目录下

find -size +100M -exec mv {} /tmp \;

范例: 在用户主家目录中寻找可被其它用户写入的文件, 然后把其他用户的写权限删除

find ~ -perm -002  -exec chmod o-w {} \;

范例: 查找/data下的权限为644,后缀为sh的普通文件,并增加执行权限

find /data –type f -perm 644  -name "*.sh" –exec chmod 755 {} \;

1.3 参数替换xargs

很多命令后面接的都是参数, 比如 ls anaconda.log, 这些文件是以参数形式传给命令的, 而不是以标准输入重定向形式, 因为ls等命令不支持标准输入重定向. 而有些命令是可以支持标准输入重定向比如 bc, cat等命令

如果想把标准输入传给那些不支持标准输入的命令作为参数, 可以利用xargs, xargs可以读取管道符传递的标准输入的数据, 然后默认以空格作为分隔, 将每个数据传给后面的命令作为参数, 进行执行. 另外, 当命令不支持多个参数时, 也可以用xargs解决

管道符传递是以标准输入传递, 再配合xargs把标准输入转换成一个参数, 传递给后面不支持标准输入的命令

注意:文件名或者是其他意义的名词内含有空格符的情况

find 经常和 xargs 命令进行组合,形式如下:

find | xargs COMMAND
[root@demo-c8 ~]# echo anaconda-ks.cfg | ls -l # 由于ls不支持标准输入, 只能接受参数, 所以即使有了管道符也不起作用
total 16
-rw-------. 1 root root 1385 Aug 15 17:06 anaconda-ks.cfg
drwxr-xr-x. 3 root root   30 Sep 11 18:40 d1
-rw-------. 1 root root  305 Sep 10 09:39 dead.letter
drwxr-xr-x. 2 root root    6 Aug 15 17:19 Desktop
drwxr-xr-x. 2 root root    6 Aug 15 17:19 Documents
drwxr-xr-x. 2 root root    6 Aug 15 17:19 Downloads
-rw-r--r--. 1 root root 1495 Aug 15 17:17 initial-setup-ks.cfg
-rw-r--r--. 1 root root   10 Sep 10 09:39 mail.txt
drwxr-xr-x. 2 root root    6 Aug 15 17:19 Music
drwxr-xr-x. 2 root root    6 Aug 15 17:19 Pictures
drwxr-xr-x. 2 root root    6 Aug 15 17:19 Public
drwxr-xr-x. 2 root root    6 Aug 15 17:19 Templates
-rw-r--r--. 1 root root    0 Sep 11 20:43 uptime
drwxr-xr-x. 2 root root    6 Aug 15 17:19 Videos

[root@demo-c8 ~]# echo anaconda-ks.cfg | xargs ls -l #通过xargs把管道符传递的标准输入, 转换成参数, 交给ls执行
-rw-------. 1 root root 1385 Aug 15 17:06 anaconda-ks.cfg
[root@demo-c8 ~]# echo anaconda-ks.cfg | xargs ls 
anaconda-ks.cfg

范例: 利用xargs创建账号

#useradd支持参数, 不支持标准输入, 可以利用xargs传参

[root@demo-c8 ~]# echo test1 | useradd 
Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]

Options:
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account

[root@demo-c8 ~]# echo test1 | xargs useradd 

范例: 多行文本转换成一行, 利用空格隔开

oot@demo-c8 ~]# seq 10 | xargs 
1 2 3 4 5 6 7 8 9 10

范例: 一行转换成多行

[root@demo-c8 ~]# echo {1..10} | xargs -n1
1
2
3
4
5
6
7
8
9
10
[root@demo-c8 ~]# echo {1..10} | xargs -n2  # -n表示一行几个元素
1 2
3 4
5 6
7 8
9 10
[root@demo-c8 ~]# echo {1..10} | xargs printf "%s %s\n"
1 2
3 4
5 6
7 8
9 10

范例: 删除当前目录下的大量文件

[root@demo-c8 opt]# touch f{1..20}.txt
[root@demo-c8 opt]# ls
conf.txt  f10.txt  f11.txt  f12.txt  f13.txt  f14.txt  f15.txt  f16.txt  f17.txt  f18.txt  f19.txt  f1.txt  f20.txt  f2.txt  f3.txt  f4.txt  f5.txt  f6.txt  f7.txt  f8.txt  f9.txt  uptime
[root@demo-c8 opt]# ls | xargs rm
[root@demo-c8 opt]# ls
[root@demo-c8 opt]# 

范例: 对查找到的文件进行排序显示

[root@demo-c8 opt]# find -name ".sh" | xargs ls -Sl
total 80
-rw-r--r--. 1 root root 302 Sep 11 23:27 f11.txt
-rw-r--r--. 1 root root   3 Sep 11 23:24 f10.txt
-rw-r--r--. 1 root root   3 Sep 11 23:24 f12.txt
-rw-r--r--. 1 root root   3 Sep 11 23:24 f13.txt
-rw-r--r--. 1 root root   3 Sep 11 23:24 f14.txt
-rw-r--r--. 1 root root   3 Sep 11 23:24 f15.txt
-rw-r--r--. 1 root root   3 Sep 11 23:24 f16.txt
-rw-r--r--. 1 root root   3 Sep 11 23:24 f17.txt
-rw-r--r--. 1 root root   3 Sep 11 23:24 f18.txt
-rw-r--r--. 1 root root   3 Sep 11 23:24 f19.txt
-rw-r--r--. 1 root root   3 Sep 11 23:24 f20.txt
-rw-r--r--. 1 root root   2 Sep 11 23:24 f1.txt
-rw-r--r--. 1 root root   2 Sep 11 23:24 f2.txt
-rw-r--r--. 1 root root   2 Sep 11 23:24 f3.txt
-rw-r--r--. 1 root root   2 Sep 11 23:24 f4.txt
-rw-r--r--. 1 root root   2 Sep 11 23:24 f5.txt
-rw-r--r--. 1 root root   2 Sep 11 23:24 f6.txt
-rw-r--r--. 1 root root   2 Sep 11 23:24 f7.txt
-rw-r--r--. 1 root root   2 Sep 11 23:24 f8.txt
-rw-r--r--. 1 root root   2 Sep 11 23:24 f9.txt

范例: 利用xargs批量创建用户,和删除用户

useradd命令只能接收一个参数, 而xargs默认是把标准输入整理到一行, 用空格隔开, 再传给命令, 所以默认情况下, xargsuseradd传多个参数时不会成功

此时, 需要将多个参数, 从一行, 改为多行, 每行只放一个参数, 再传给命令, 而命令每次只从xargs里读取一个参数, 进行操作

[root@demo-c8 ~]# echo admin{1..10} | xargs -n1 useradd
[root@demo-c8 ~]# for i in {1..10}; do id admin$i; done
uid=1004(admin1) gid=1004(admin1) groups=1004(admin1)
uid=1005(admin2) gid=1005(admin2) groups=1005(admin2)
uid=1006(admin3) gid=1006(admin3) groups=1006(admin3)
uid=1007(admin4) gid=1007(admin4) groups=1007(admin4)
uid=1008(admin5) gid=1008(admin5) groups=1008(admin5)
uid=1009(admin6) gid=1009(admin6) groups=1009(admin6)
uid=1010(admin7) gid=1010(admin7) groups=1010(admin7)
uid=1011(admin8) gid=1011(admin8) groups=1011(admin8)
uid=1012(admin9) gid=1012(admin9) groups=1012(admin9)
uid=1013(admin10) gid=1013(admin10) groups=1013(admin10)
[root@demo-c8 ~]# echo admin{1..10} | xargs -n1 userdel
[root@demo-c8 ~]# for i in {1..10}; do id admin$i; done
id: ‘admin1’: no such user
id: ‘admin2’: no such user
id: ‘admin3’: no such user
id: ‘admin4’: no such user
id: ‘admin5’: no such user
id: ‘admin6’: no such user
id: ‘admin7’: no such user
id: ‘admin8’: no such user
id: ‘admin9’: no such user
id: ‘admin10’: no such user

useradd不同, rm和ls这种命令是可以接收多个参数的, 所以无需进行再处理, 就可以传给命令

[root@demo-c8 ~]# find /opt -name "*.txt" | xargs ls 
/opt/f10.txt  /opt/f12.txt  /opt/f14.txt  /opt/f16.txt  /opt/f18.txt  /opt/f1.txt   /opt/f2.txt  /opt/f4.txt  /opt/f6.txt  /opt/f8.txt
/opt/f11.txt  /opt/f13.txt  /opt/f15.txt  /opt/f17.txt  /opt/f19.txt  /opt/f20.txt  /opt/f3.txt  /opt/f5.txt  /opt/f7.txt  /opt/f9.txt

范例: 当文件名称中有特殊字符时, 比如空格, 那么当把搜索结果, 通过管道xargs传个其他命令时, 会显示找不到文件, 因为管道会按照特殊符号把文件名分隔开来

[root@demo-c8 opt]# touch "aaa???bbb.txt"
[root@demo-c8 opt]# find . -type f | xargs ls -l
ls: cannot access './aaa': No such file or directory
ls: cannot access 'bbb.txt': No such file or directory
-rw-r--r--. 1 root root 0 Sep 12 00:37 './aaa???bbb.txt'
# 构建名称带空格的文件
[root@demo-c8 opt]# touch "aaa bbb.txt"
[root@demo-c8 opt]# ll
total 0
-rw-r--r--. 1 root root 0 Sep 12 00:34 'aaa bbb.txt'
# 搜索/opt目录下的普通文件, 并且通过xargs传给ls -l显示
[root@demo-c8 opt]# find . -type f 
./aaa bbb.txt
[root@demo-c8 opt]# find . -type f  | xargs ls -l
ls: cannot access './aaa': No such file or directory # 此时, 因为文件本身名称有空格, 所以管道会把这个文件按照空格分隔成两个文件
ls: cannot access 'bbb.txt': No such file or directory
# xargs默认以空格作为分隔符, 所以如果文件名有特殊符号或者空格时, 要手动指定xargs以空, 也就是nul为分隔符

# -print0表示以nul空, 作为分隔符, 而后面的-0表示用前面定义的空nul为分隔符来从管道取文件名
[root@demo-c8 opt]# find . -type f -print0 | xargs -0 ls -l
-rw-r--r--. 1 root root 0 Sep 12 00:34 './aaa bbb.txt'
-rw-r--r--. 1 root root 0 Sep 12 00:37 './aaa???bbb.txt'

范例: 查询/bin目录有特殊权限的文件, 并按照大小排序

find /bin/ -perm /7000 | xargs ls -Sl   #  # SUID, SGID, Sticky三者有任何一个即可
#此命令和上面有何区别? 
find /bin/ -perm -7000 | xargs ls -Sl   # SUID, SGID, Sticky三者都有

范例: 错误命令, ls不支持标准输入, 所以也就不支持管道

find /sbin/ -perm /700 | ls -l

范例: 利用xargs生成变量, 并发执行多个进程

# {}代表的就是seq生成的数据, 通过xargs传给wget命令, 相当于变量循环
# -P: 并发处理, -P10, 开启10个进程同时进行操作, 比如要下载100个文件, -P10就是同一时间, 同时下载10个文件
seq 100 | xargs -i -P10 wget -P /data hjttp://10.0.0.8/{}/html

范例: -exec vs xargs

# 二者都能实现相同的目的, 但是-exec执行效率远低于xargs
[root@demo-c8 ~]# find /etc -name "*.conf" | xargs ls -l | wc -l
402
[root@demo-c8 ~]# find /etc -name "*.conf" -exec ls -l {} \; | wc -l
402

范例: 当要删除或者显示的文件过多时, 这时如果用*通配符, 那么会显示要处理的文件过多, 此时可以用xargs, 一个一个传给命令

# 删除当前目录下的大量文件

ls | xargs rm

练习
1、查找/var目录下属主为root,且属组为mail的所有文件
2、查找/var目录下不属于root、lp、gdm的所有文件
3、查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件
4、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件
5、查找/etc目录下大于1M且类型为普通文件的所有文件
6、查找/etc目录下所有用户都没有写权限的文件
7、查找/etc目录下至少有一类用户没有执行权限的文件
8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件

2. 文件压缩和解压缩

2.1 compress和uncompress

此工具来自于ncompress

格式:

# 压缩
compress [options] file1 file2 ... filen

# 解压缩
uncompress file1.Z file2.Z ... file3.Z

常用选项:

-d 解压缩, 相当于uncompress
-c 将结果输出至标准输出, 不删除原文件, compress默认在压缩后, 会把原文件删除
-v 显示详情

范例: compress压缩文件

# 准备测试文件
[root@demo-c8 ~]# yum -y install ncompress
[root@demo-c8 ~]# cd /opt
[root@demo-c8 opt]# ll -h
total 90M
-rw-r--r--. 1 root root 90M Sep  5 18:30 btmp-34
# 默认情况, compress压缩文件后, 会把原文件删除, 产生的压缩文件以.Z为后缀
[root@demo-c8 opt]# compress btmp-34 
[root@demo-c8 opt]# ll -h
total 2.5M
-rw-r--r--. 1 root root 2.5M Sep  5 18:30 btmp-34.Z

范例: zcat, 解压缩并且把文件内容打印到屏幕

[root@demo-c8 opt]# zcat btmp-34.Z

范例: zcat, 解压缩并且把文件内容重定向到文件中

[root@demo-c8 opt]# zcat btmp-34.Z > btmp-34
[root@demo-c8 opt]# ll -h
total 92M
-rw-r--r--. 1 root root  90M Sep 12 10:08 btmp-34
-rw-r--r--. 1 root root 2.5M Sep  5 18:30 btmp-34.Z

范例: uncompress解压缩

[root@demo-c8 opt]# ll -h
total 92M
-rw-r--r--. 1 root root  90M Sep 12 10:08 btmp-34
-rw-r--r--. 1 root root 2.5M Sep  5 18:30 btmp-34.Z
[root@demo-c8 opt]# uncompress btmp-34.Z 
btmp-34 already exists.
Do you wish to overwrite btmp-34 (y or n)?  # 有同名文件时, 会提示是否覆盖

范例: compress同时压缩多个文件

[root@demo-c8 opt]# cp btmp-34 btmp-341
[root@demo-c8 opt]# cp btmp-34 btmp-342
[root@demo-c8 opt]# cp btmp-34 btmp-343
[root@demo-c8 opt]# ll
total 367168
-rw-r--r--. 1 root root 93351936 Sep 12 10:08 btmp-34
-rw-r--r--. 1 root root 93351936 Sep 12 10:30 btmp-341
-rw-r--r--. 1 root root 93351936 Sep 12 10:30 btmp-342
-rw-r--r--. 1 root root 93351936 Sep 12 10:30 btmp-343
-rw-r--r--. 1 root root  2569476 Sep  5 18:30 btmp-34.Z
[root@demo-c8 opt]# compress btmp-341 btmp-342  btmp-343
[root@demo-c8 opt]# ll -h
total 99M
-rw-r--r--. 1 root root  90M Sep 12 10:08 btmp-34
-rw-r--r--. 1 root root 2.5M Sep 12 10:30 btmp-341.Z
-rw-r--r--. 1 root root 2.5M Sep 12 10:30 btmp-342.Z
-rw-r--r--. 1 root root 2.5M Sep 12 10:30 btmp-343.Z
-rw-r--r--. 1 root root 2.5M Sep  5 18:30 btmp-34.Z

2.2 gzip和gunzip

格式:

gzip [option]... file...

常用选项:

-k keep, 保留原文件,CentOS 8 新特性
-d 解压缩,相当于gunzip
-c 结果输出至标准输出,保留原文件不改变
-# 指定压缩比,#取值为1-9,值越大压缩比越大

范例:

# 解压缩
gunzip file.gz

# 解压缩并且把文件内容打印到屏幕
zcat file.gz

范例:

gzip -c messages >messages.gz
gzip -c -d messages.gz > messages
zcat messages.gz > messages
cat messages | gzip > m.gz

范例: gzip默认也会把原文件删除

[root@demo-c8 opt]# ll -h
total 99M
-rw-r--r--. 1 root root  90M Sep 12 10:08 btmp-34
[root@demo-c8 opt]# gzip btmp-34
[root@demo-c8 opt]# ll -h
-rw-r--r--. 1 root root 1.3M Sep 12 10:08 btmp-34.gz

范例: 解压缩

# gunzip等同于gzip -d
[root@demo-c8 opt]# gunzip btmp-34.gz
[root@demo-c8 opt]# ll -h
total 99M
-rw-r--r--. 1 root root  90M Sep 12 10:08 btmp-34

范例: gzip也支持同时压缩或解压缩多个文件

[root@demo-c8 opt]# cp btmp-34 btmp-341
[root@demo-c8 opt]# cp btmp-34 btmp-342
[root@demo-c8 opt]# gzip btmp-341 btmp-342
[root@demo-c8 opt]# ll
total 103756
-rw-r--r--. 1 root root 93351936 Sep 12 10:08 btmp-34
-rw-r--r--. 1 root root  1300527 Sep 12 10:35 btmp-341.gz
-rw-r--r--. 1 root root  1300527 Sep 12 10:35 btmp-342.gz

2.3 bzip2和bunzip2

来自于bzip2

格式:

bzip2 [option]... file...

常用选项:

-k keep, 保留原文件
-d 解压缩
-c 结果输出至标准输出,保留原文件不改变
-# 1-9,压缩比,默认为9

范例:

bunzip2 file.bz2 解压缩
bzcat file.bz2 解压缩, 并且把文件内容打印到屏幕

2.4 xz和unxz

来自于xz

格式:

xz [OPTION]... FILE ...

常用选项:

-k keep, 保留原文件
-d 解压缩
-c 结果输出至标准输出,保留原文件不改变
-# 压缩比,取值1-9,默认为6

范例:

unxz file.xz 解压缩
xzcat file.xz 解压缩, 并且把文件内容打印到屏幕

compress, gzip, bzip2和xz都只能压缩文件, 不能压缩文件夹

四种压缩工具的压缩比比较:

xz > bzip2 > gzip > compress

压缩比越高意味着cpu消耗越大, 所以要根据实际情况, 选择压缩工具. 文本文本压缩效果比较好, 而二进制文件压缩效果一般

2.5 zip和unzip

zip 可以实现打包多个目录和多个文件成一个文件并压缩,但可能会丢失文件属性信息,如:所有者和组信息,一般建议使用 tar 代替

来自于zip 和 unzip 包

范例:

#打包并压缩, 此时会把/etc/sysconfig整个目录打包并压缩
#要求目标文件所在目录必须实现存在, 否则要手动创建
zip  -r /backup/sysconfig.zip /etc/sysconfig/
#不包括目录本身,只打包目录内的文件和子目录, 那就要先进入到目录内, 再执行zip, 不能写/etc/sysconfig/*, 因为这样还是打包的目录本身
cd /etc/sysconfig; zip -r /root/sysconfig.zip * 
#默认解压缩至当前目录
unzip /backup/sysconfig.zip  
#解压缩至指定目录,如果指定目录不存在,会在其父目录(必须事先存在)下自动生成
unzip /backup/sysconfig.zip  -d /tmp/config  
cat /var/log/messages | zip messages  -
#-p 表示管道
unzip -p message.zip   > message

范例: 利用zip, 打包并压缩整个/etc/sysconfig目录

# 目标文件所在路径必须实现存在, 否则要手动创建
[root@demo-c8 opt]# zip -r ./zip/sysconfig.zip /etc/sysconfig
zip I/O error: No such file or directory
[root@demo-c8 opt]# mkdir zip
[root@demo-c8 opt]# zip -r ./zip/sysconfig.zip /etc/sysconfig
[root@demo-c8 opt]# ll -h zip
total 20K
-rw-r--r--. 1 root root 18K Sep 12 10:50 sysconfig.zip

zip打包压缩后, 原始文件或目录不会被删除, zip无法打包压缩单个文件

打包压缩和解包解压缩时, 一定要注意路径问题, 否则一旦路径写的不对. 很有可能解包解压缩后, 会把另一个路径或者相同路径内的同名文件给替换了

3. 文件打包解包

3.1 tar

tar可以对多个目录或者多个文件打包成一个文件, 并且可以压缩, 保留文件属性不丢失, 常用于备份功能

选项:

-c 打包, 打包后, 文件以.tar为后缀
-p 保留属性, 默认就保留
-f 打包到文件, 一般目标文件以.tar或者.tar.gz结尾
-v 显示过程
-t -f 预览打包文件内容

-x 解包, 可以解开任何格式, 会自动调用文件需要的压缩工具, 要求本地有对应的压缩工具, 默认解包到当前目录
-C 解包到其他目录

范例: 创建归档文件, 保留权限

[root@demo-c8 opt]# tar cvf etc.tar /etc
root@demo-c8 opt]# ll -h
-rw-r--r--.   1 root root  26M Sep 12 11:15 etc.tar

范例: 追加文件至归档, 注意: 不支持将文件追加到压缩文件

[root@demo-c8 opt]# ll -h etc.tar 
-rw-r--r--. 1 root root 26M Sep 12 11:15 etc.tar
[root@demo-c8 opt]# tar rf etc.tar ~/anaconda-ks.cfg 
tar: Removing leading `/' from member names
tar: Removing leading `/' from hard link targets
[root@demo-c8 opt]# tar tvf etc.tar | grep anaconda-ks.cfg
-rw------- root/root      1385 2022-08-15 17:06 root/anaconda-ks.cfg

范例: 解包到指定目录

[root@demo-c8 opt]# tar xvf etc1.tar -C /tmp
[root@demo-c8 opt]# ll /tmp
drwxr-xr-x. 135 root root 8192 Sep 12 09:51 etc

tar支持同时打包多个文件或者多个目录

案例: 将/home整个目录打包到当前目录

# cvf是基本的打包组合

tar cvf home.tar /home

预览包内的内容

[23:36:46 root@CentOS-8 ~]#tar tvf home.tar 

将一个目录比如/home打包后, 其内部的文件都是以home/相对路径为起始的, 解包到某个目录后, 显示为home目录

[root@demo-c8 opt]# ll
drwxr-xr-x. 3 root root       18 Aug 15 17:05 home

tar本身只是打包, 但是可以配合压缩选项, 把打好的包进行压缩

-z gzip
-j bzip2
-J xz
tar zcvf etc.tar.gz /etc
xz压缩比最高, 但是效率最低, 压缩过程消耗时间长
gzip压缩比最低, 老牌的压缩工具
bzip2 折中, 推荐使用
[root@demo-c8 opt]# tar -zcvf etc.tar.gz /etc
[root@demo-c8 opt]# tar -jcvf etc.tar.bz2 /etc
[root@demo-c8 opt]# tar -Jcvf etc.tar.xz /etc

[root@demo-c8 opt]# ll -h
total 14M
-rw-r--r--. 1 root root 5.9M Sep 12 11:22 etc.tar.gz
-rw-r--r--. 1 root root 4.3M Sep 12 11:22 etc.tar.bz2
-rw-r--r--. 1 root root 3.7M Sep 12 11:22 etc.tar.xz

注意: 如果只想打包或者压缩文件, 而不包括目录本身, 就先进到目录里再操作, 否则即使写绝对路径, 目录也会被打包或者压缩进去

范例: 利用 tar 进行文件复制, 大量文件情况下, 比cp效率高

[root@demo-c8 ~]# ll -h /data
total 8.7M
-rw-r--r--. 1 root root 8.7M Sep  5 18:09 access_log
[root@demo-c8 ~]# mkdir /backup
[root@demo-c8 ~]# ll /backup/
total 0
[root@demo-c8 ~]# tar c /data | tar x -C /backup
tar: Removing leading `/' from member names

 # 会把整个/data目录拷贝到/backup目录下

[root@demo-c8 ~]# ll -h /backup/
total 0
drwxr-xr-x. 2 root root 155 Sep 12 11:39 data
[root@demo-c8 ~]# ll /backup/data -h
total 8.7M
-rw-r--r--. 1 root root 8.7M Sep  5 18:09 access_log
[root@demo-c8 data]# du -sh /data
8.7M    /data
[root@demo-c8 data]# du -sh /backup/
8.7M    /backup/
# 如果只是拷贝/data目录内的内容, 不复制/data目录本身, 就先进入到/data目录

[root@demo-c8 ~]# cd /data; tar c * | tar x -C /backup
[root@demo-c8 data]# ll /backup/
total 8868
-rw-r--r--. 1 root root 9080283 Sep  5 18:09 access_log
drwxr-xr-x. 2 root root      24 Sep 12 11:40 data

范例: 排除文件

--exclude=文件路径/文件名, 排除的文件要写到打包目标路径后面
[root@demo-c8 data]# ll
total 8868
-rw-r--r--. 1 root root 9080283 Sep  5 18:09 access_log
-rw-r--r--. 1 root root       0 Sep 12 11:45 f10.txt
-rw-r--r--. 1 root root       0 Sep 12 11:45 f1.txt
-rw-r--r--. 1 root root       0 Sep 12 11:45 f2.txt
-rw-r--r--. 1 root root       0 Sep 12 11:45 f3.txt
-rw-r--r--. 1 root root       0 Sep 12 11:45 f4.txt
-rw-r--r--. 1 root root       0 Sep 12 11:45 f5.txt
-rw-r--r--. 1 root root       0 Sep 12 11:45 f6.txt
-rw-r--r--. 1 root root       0 Sep 12 11:45 f7.txt
-rw-r--r--. 1 root root       0 Sep 12 11:45 f8.txt
-rw-r--r--. 1 root root       0 Sep 12 11:45 f9.txt
[root@demo-c8 data]# tar zcf /opt/f.tar.gz --exclude=access_log *
[root@demo-c8 data]# ll /opt/f.tar.gz 
-rw-r--r--. 1 root root 176 Sep 12 11:45 /opt/f.tar.gz
[root@demo-c8 data]# tar tvf /opt/f.tar.gz 
-rw-r--r-- root/root         0 2022-09-12 11:45 f10.txt
-rw-r--r-- root/root         0 2022-09-12 11:45 f1.txt
-rw-r--r-- root/root         0 2022-09-12 11:45 f2.txt
-rw-r--r-- root/root         0 2022-09-12 11:45 f3.txt
-rw-r--r-- root/root         0 2022-09-12 11:45 f4.txt
-rw-r--r-- root/root         0 2022-09-12 11:45 f5.txt
-rw-r--r-- root/root         0 2022-09-12 11:45 f6.txt
-rw-r--r-- root/root         0 2022-09-12 11:45 f7.txt
-rw-r--r-- root/root         0 2022-09-12 11:45 f8.txt
-rw-r--r-- root/root         0 2022-09-12 11:45 f9.txt

范例: 从文件列表中, 取要打包或者排除的文件名

-X 指定要排除的文件
-T 指定要打包压缩的文件
二者有一个即可, 把文件名写到文件里, 文件名必须精确, 前后不能有空格
# 只打包压缩tar.txt文件中的文件
tar zcvf /tmp/f.tar.gz -T tar.txt 
# 除了untar.txt中的文件, 剩下都打包并压缩
tar zcvf /tmp/f.tar.gz -X untar.txt

3.2 split

split命令可以分割一个大文件为多个相同大小的小文件

选项:

-b SIZE : 指定每个文件的大小
-d : 指定生成的每个小文件, 通过数字后缀区分, 默认是字母后缀

范例: 切割文件, 以默认的字母后缀区分

[root@demo-c8 data]# split -b 1M access_log  access_log_split

[root@demo-c8 data]# ll -h
total 18M
-rw-r--r--. 1 root root 8.7M Sep  5 18:09 access_log
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitaa
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitab
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitac
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitad
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitae
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitaf
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitag
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitah
-rw-r--r--. 1 root root 676K Sep 12 11:58 access_log_splitai

范例: 以数字为后缀切割

[root@demo-c8 data]# split -d -b 1M access_log  access_log_split
[root@demo-c8 data]# ll -h 
total 26M
-rw-r--r--. 1 root root 8.7M Sep  5 18:09 access_log
-rw-r--r--. 1 root root 1.0M Sep 12 11:59 access_log_split00
-rw-r--r--. 1 root root 1.0M Sep 12 11:59 access_log_split01
-rw-r--r--. 1 root root 1.0M Sep 12 11:59 access_log_split02
-rw-r--r--. 1 root root 1.0M Sep 12 11:59 access_log_split03
-rw-r--r--. 1 root root 1.0M Sep 12 11:59 access_log_split04
-rw-r--r--. 1 root root 1.0M Sep 12 11:59 access_log_split05
-rw-r--r--. 1 root root 1.0M Sep 12 11:59 access_log_split06
-rw-r--r--. 1 root root 1.0M Sep 12 11:59 access_log_split07
-rw-r--r--. 1 root root 676K Sep 12 11:59 access_log_split08
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitaa
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitab
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitac
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitad
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitae
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitaf
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitag
-rw-r--r--. 1 root root 1.0M Sep 12 11:58 access_log_splitah
-rw-r--r--. 1 root root 676K Sep 12 11:58 access_log_splitai

split不仅可以切割纯文本, 也可以切割打包压缩文件

[root@demo-c8 data]# ll -h
total 5.9M
-rw-r--r--. 1 root root 5.9M Sep 12 12:00 etc.tar.gz
[root@demo-c8 data]# split -d -b 1M etc.tar.gz etc.tar.gz_split
[root@demo-c8 data]# ll -h
total 12M
-rw-r--r--. 1 root root 5.9M Sep 12 12:00 etc.tar.gz
-rw-r--r--. 1 root root 1.0M Sep 12 12:01 etc.tar.gz_split00
-rw-r--r--. 1 root root 1.0M Sep 12 12:01 etc.tar.gz_split01
-rw-r--r--. 1 root root 1.0M Sep 12 12:01 etc.tar.gz_split02
-rw-r--r--. 1 root root 1.0M Sep 12 12:01 etc.tar.gz_split03
-rw-r--r--. 1 root root 1.0M Sep 12 12:01 etc.tar.gz_split04
-rw-r--r--. 1 root root 897K Sep 12 12:01 etc.tar.gz_split05

范例: 将切割的文件, 重新组合

# 先通过通配符, 找到所有切割的小文件, 因为切割时, 小文件都是按照后缀顺序生成的, 所以cat也是按照顺序进行打开和重组
[root@demo-c8 data]# cat etc.tar.gz_s* > etc1.tar.gz
[root@demo-c8 data]# ll -h
total 24M
-rw-r--r--. 1 root root 5.9M Sep 12 12:01 etc1.tar.gz
-rw-r--r--. 1 root root 5.9M Sep 12 12:00 etc.tar.gz

3.3 cpio

cpio 是历史悠久的打包解包工具,不是压缩解压缩工具, 不过目前也已较少使用

cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解开以“.cpio”或者“.tar”结尾的文件

常见的通过cpio打包的文件:

#  ASCII cpio archive (SVR4 with no CRC)
[root@demo-c8 ~]# file /boot/initramfs-0-rescue-eaab49a3b5f746ae847c443ec9bc62c4.img 
/boot/initramfs-0-rescue-eaab49a3b5f746ae847c443ec9bc62c4.img: ASCII cpio archive (SVR4 with no CRC)

通过cpio打包的文件, 要用cpio工具解包

格式:

cpio [选项] > 文件名或者设备名
cpio [选项] < 文件名或者设备名

常用选项:

-o   output模式,打包,将标准输入传入的文件名打包后发送到标准输出
-i input模式,解包,对标准输入传入的打包文件名解包到当前目录
-t 预览,查看标准输入传入的打包文件中包含的文件列表
-O filename 输出到指定的归档文件名
-A 向已存在的归档文件中追加文件
-I filename 对指定的归档文件名解压
-F filename 使用指定的文件名替代标准输入或输出
-d 解包生成目录,在cpio还原时,自动的建立目录
-v 显示打包过程中的文件名称

范例:

#将etc目录备份
find ./etc -print | cpio -ov > bak.cpio
#将/data内容追加bak.cpio
find /data | cpio -oA -F bak.cpio 
#内容预览
cpio –tv < etc.cpio
#解包文件
cpio –idv < etc.cpio

tar vs cpio

tar命令在打包时, 会把打包的目录的/前缀去掉, 也就是保留相对路径
这样解包后, 文件会保存在指定的目录, 以相对路径形式

cpio命令在打包时, 会保留目录的/前缀, 这样一旦解包后
文件不会被保存到指定的目录, 因为cpio打包时, 保留的是绝对路径

不要在/根目录下, 进行文件或目录的解包和打包操作, 容易破坏根目录

范例: tar 和 cpio 打包/etc目录效果对比

[root@demo-c8 opt]# tar -zcvf etc.tar.gz /etc
[root@demo-c8 opt]# tar -tvf etc.tar.gz
...                      # 可以看到tar打包后的文件是去掉了/前缀的
-rw-r--r-- root/root     60352 2017-05-11 21:00 etc/mime.types
-rw-r--r-- root/root        28 2022-08-15 17:05 etc/vconsole.conf
-rw-r--r-- root/root        19 2022-08-15 17:05 etc/locale.conf
-rw-r--r-- root/root        13 2022-08-15 17:05 etc/hostname
[root@demo-c8 opt]# find /etc -print | cpio -ov > etc.cpio
[root@demo-c8 opt]# cpio -tv < etc.cpio 
                                     # 可以看到cpio打包后的文件保留了/前缀
...-rw-r--r--   1 root     root        60352 May 11  2017 /etc/mime.types
-rw-r--r--   1 root     root           28 Aug 15 17:05 /etc/vconsole.conf
-rw-r--r--   1 root     root           19 Aug 15 17:05 /etc/locale.conf
-rw-r--r--   1 root     root           13 Aug 15 17:05 /etc/hostname

范例: 解决cpio保留/前缀

[root@demo-c8 ~]# cd / # 此时, 需要先进到根目录, 才能使用./etc相对路径
[root@demo-c8 /]# find ./etc -print | cpio -ov > /opt/etc1.cpio
[root@demo-c8 /]# cd /opt
[root@demo-c8 opt]# ll
total 64672
-rw-r--r--. 1 root root 25877504 Sep 12 12:20 etc1.cpio
-rw-r--r--. 1 root root 25879552 Sep 12 12:17 etc.cpio
[root@demo-c8 opt]# cpio -tv < etc1.cpio 
...
-rw-r--r--   1 root     root        60352 May 11  2017 etc/mime.types
-rw-r--r--   1 root     root           28 Aug 15 17:05 etc/vconsole.conf
-rw-r--r--   1 root     root           19 Aug 15 17:05 etc/locale.conf
-rw-r--r--   1 root     root           13 Aug 15 17:05 etc/hostname

范例: 解包/解压缩时, 工具要和文件后缀一致

[root@demo-c8 ~]# cp /boot/initramfs-4.18.0-193.el8.x86_64.img /data -a
[root@demo-c8 ~]# cd /data
[root@demo-c8 data]# ll
total 49620
-rw-------. 1 root root 50808400 Aug 15 17:06 initramfs-4.18.0-193.el8.x86_64.img
[root@demo-c8 data]# file initramfs-4.18.0-193.el8.x86_64.img 
initramfs-4.18.0-193.el8.x86_64.img: gzip compressed data, max compression, from Unix, original size 93817856

[root@demo-c8 data]# gzip -d initramfs-4.18.0-193.el8.x86_64.img
gzip: initramfs-4.18.0-193.el8.x86_64.img: unknown suffix -- ignored

/boot/initramfs-4.18.0-193.el8.x86_64.img文件虽然是通过gzip压缩的, 但是其后缀是.img, 而gzip要求后缀是.gz, 所以会报错. 需要先给.img文件添加.gz后缀, 然后再解压

[root@demo-c8 data]# mv initramfs-4.18.0-193.el8.x86_64.img  initramfs-4.18.0-193.el8.x86_64.img.gz
[root@demo-c8 data]# gzip -d initramfs-4.18.0-193.el8.x86_64.img
[root@demo-c8 data]# file initramfs-4.18.0-193.el8.x86_64.img 
initramfs-4.18.0-193.el8.x86_64.img: ASCII cpio archive (SVR4 with no CRC)
[root@demo-c8 data]# cpio -tv < initramfs-4.18.0-193.el8.x86_64.img
...
lrwxrwxrwx   1 root     root           20 Apr 24  2020 usr/share/unimaps -> /usr/lib/kbd/unimaps
drwxr-xr-x   3 root     root            0 Apr 24  2020 var
lrwxrwxrwx   1 root     root           11 Apr 24  2020 var/lock -> ../run/lock
lrwxrwxrwx   1 root     root            6 Apr 24  2020 var/run -> ../run
drwxr-xr-x   2 root     root            0 Apr 24  2020 var/tmp
183238 blocks

总结: /boot/initramfs-4.18.0-193.el8.x86_64.img文件, 是由cpio进行打包后, 再通过gzip压缩的一个小型的Linux系统

[root@demo-c8 data]# cpio -idv < initramfs-4.18.0-193.el8.x86_64.img
[root@demo-c8 data]# ls
bin  dev  etc  init  initramfs-4.18.0-193.el8.x86_64.img  lib  lib64  proc  root  run  sbin  shutdown  sys  sysroot  tmp  usr  var

范例: cpio只是打包和解包, 并不会压缩和解压缩

[root@demo-c8 opt]# find . -name  "btmp-34" -print | cpio -ov > btmp.cpio
./btmp-34
182329 blocks
[root@demo-c8 opt]# ll -h
total 179M
-rw-r--r--. 1 root root 90M Sep  5 18:30 btmp-34
-rw-r--r--. 1 root root 90M Sep 12 12:48 btmp.cpio
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容