文件查找和打包压缩

文件查找和打包压缩

文件查找

  • 内荣概述:在文件系统上查找符合条件的文件
  • 两个基本命令
    • locate,非实时查找(数据库查找)
    • find,实时查找

locate命令

  • locate概念:

    • 查询系统上预建的文件索引数据库
      • /var/lib/mlocate/mlocate.db
    • 依赖于事先构建的索引
      • 索引的构建是在系统较为空闲时自动进行(周期性任务),管理员手动更新数据库(updatedb)
    • 索引构建过程需要遍历整个根文件系统,极消耗IO资源
    • 工作特点
      • 查找速度快
      • 模糊查找
      • 非实时查找
      • 搜索的是文件的全路径,不仅仅是文件名
      • 可能只搜索用户具备读取和执行权限的目录
  • 格式:

    locate [OPTION]... [PATTERN]...
    
  • 常用选项:

    -i 不区分大小写的搜索
    -n N 只列举前N个匹配项目
    -r 使用基本正则表达式
    
  • 范例:#搜索名称或路径中包含“conf”的文件

    [root@centos8 data]# locate conf | head -1
    /boot/config-4.18.0-80.el8.x86_64
    
  • 范例:搜索以”.conf“结尾的文件

    [root@centos8 data]# locate -r '\.conf$' |head -10
    /boot/loader/entries/fa929950bbfb4398b4c571a22b812fe4-0-rescue.conf
    /boot/loader/entries/fa929950bbfb4398b4c571a22b812fe4-4.18.0-80.el8.x86_64.conf
    /etc/asound.conf
    /etc/brltty.conf
    /etc/chrony.conf
    /etc/dleyna-server-service.conf
    /etc/dnsmasq.conf
    /etc/dracut.conf
    /etc/fprintd.conf
    /etc/fuse.conf
    [root@centos8 data]#
    
  • 范例:搜索 .conf 有关的文件只列前十个,不区分大小写。

    [root@centos8 data]# locate -n 10 -ir '\.conf$'
    /boot/loader/entries/fa929950bbfb4398b4c571a22b812fe4-0-rescue.conf
    /boot/loader/entries/fa929950bbfb4398b4c571a22b812fe4-4.18.0-80.el8.x86_64.conf
    /etc/asound.conf
    /etc/brltty.conf
    /etc/chrony.conf
    /etc/dleyna-server-service.conf
    /etc/dnsmasq.conf
    /etc/dracut.conf
    /etc/fprintd.conf
    /etc/fuse.conf
    

find命令

  • find命令概念:

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

  • 工作特点:

    • 查找速度略慢
    • 精确查找
    • 实时查找
    • 可能只搜索用户具备读取和执行权限的目录
  • 格式:

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

    • -maxdepth level 最大搜索目录深度,指定目录下的文件为第1级

    • -mindepth level 最小搜索目录深度

    • 范例:可以限定最大目录深度和最小目录深度

      find /etc -maxdepth 2 -mindepth 2对每个目录先处理目录内的文件,再处理目录本身
      
    • 对每个目录先处理目录内的文件,再处理目录本身

      • -depth -d

        [root@centos8 ~]# find -d  / find -depth
        ./.bash_logout
        ./.bash_profile
        ./.bashrc
        ./.cshrc
        ./.tcshrc
        ./anaconda-ks.cfg
        ./.cache/dconf/user
        ./.cache/dconf
        ./.cache
        ./.dbus/session-bus/fa929950bbfb4398b4c571a22b812fe4-9
        ./.dbus/session-bus
        ./.dbus
        ./.config/ibus/bus
        ./.config/ibus
        ./.config/pulse/fa929950bbfb4398b4c571a22b812fe4-device-volumes.tdb
        ./.config/pulse/fa929950bbfb4398b4c571a22b812fe4-stream-volumes.tdb
        ./.config/pulse/fa929950bbfb4398b4c571a22b812fe4-card-database.tdb
        ./.config/pulse/cookie
        ./.config/pulse/fa929950bbfb4398b4c571a22b812fe4-default-sink
        ./.config/pulse/fa929950bbfb4398b4c571a22b812fe4-default-source
        ./.config/pulse
        ./.config
        ./initial-setup-ks.cfg
        ./.esd_auth
        ./.bash_history
        ./.Xauthority
        ./test/hosts
        ./test
        .
        
  • 根据文件名和inode查找

    • 常见选项:

    • -name "文件名称":支持使用glob,如:*, ?, [], [^],通配符要加双引号引起来
      -iname "文件名称":不区分字母大小写
      -inum n 按inode号查找
      -samefile name 相同inode号的文件
      -links n 链接数为n的文件
      -regex “PATTERN”:以PATTERN匹配整个文件路径,而非文件名称
      
      • 范例:精确搜索

        [root@centos8 ~]# find / -name passwd
        /sys/fs/selinux/class/passwd
        /sys/fs/selinux/class/passwd/perms/passwd
        /etc/pam.d/passwd
        /etc/passwd
        /var/lib/sss/mc/passwd
        /usr/bin/passwd
        /usr/share/licenses/passwd
        /usr/share/doc/passwd
        /usr/share/bash-completion/completions/passwd
        [root@centos8 ~]#
        
      • 范例:模糊搜索,包含passwd所有文件

        [root@centos8 ~]# find / -name "passwd*"
        /sys/fs/selinux/class/passwd
        /sys/fs/selinux/class/passwd/perms/passwd
        /etc/pam.d/passwd
        /etc/passwd-
        /etc/passwd
        /var/lib/sss/mc/passwd
        /usr/bin/passwd
        /usr/share/licenses/passwd
        /usr/share/doc/passwd
        /usr/share/doc/perl-Net-SSLeay/examples/passwd-cb.pl
        /usr/share/locale/bg/LC_MESSAGES/passwd.mo
        /usr/share/locale/ca/LC_MESSAGES/passwd.mo
        /usr/share/locale/cs/LC_MESSAGES/passwd.mo
        /usr/share/locale/da/LC_MESSAGES/passwd.mo
        /usr/share/locale/de/LC_MESSAGES/passwd.mo
        /usr/share/locale/el/LC_MESSAGES/passwd.mo
        /usr/share/locale/en_GB/LC_MESSAGES/passwd.mo
        /usr/share/locale/es/LC_MESSAGES/passwd.mo
        /usr/share/locale/fi/LC_MESSAGES/passwd.mo
        /usr/share/locale/fr/LC_MESSAGES/passwd.mo
        /usr/share/locale/gl/LC_MESSAGES/passwd.mo
        /usr/share/locale/hr/LC_MESSAGES/passwd.mo
        /usr/share/locale/hu/LC_MESSAGES/passwd.mo
        /usr/share/locale/id/LC_MESSAGES/passwd.mo
        /usr/share/locale/it/LC_MESSAGES/passwd.mo
        /usr/share/locale/ja/LC_MESSAGES/passwd.mo
        /usr/share/locale/ka/LC_MESSAGES/passwd.mo
        /usr/share/locale/ko/LC_MESSAGES/passwd.mo
        /usr/share/locale/nb/LC_MESSAGES/passwd.mo
        /usr/share/locale/nl/LC_MESSAGES/passwd.mo
        /usr/share/locale/pl/LC_MESSAGES/passwd.mo
        /usr/share/locale/pt_BR/LC_MESSAGES/passwd.mo
        /usr/share/locale/ro/LC_MESSAGES/passwd.mo
        /usr/share/locale/ru/LC_MESSAGES/passwd.mo
        /usr/share/locale/sk/LC_MESSAGES/passwd.mo
        /usr/share/locale/sl/LC_MESSAGES/passwd.mo
        /usr/share/locale/sq/LC_MESSAGES/passwd.mo
        /usr/share/locale/sr/LC_MESSAGES/passwd.mo
        /usr/share/locale/sv/LC_MESSAGES/passwd.mo
        /usr/share/locale/tr/LC_MESSAGES/passwd.mo
        /usr/share/locale/uk/LC_MESSAGES/passwd.mo
        /usr/share/locale/vi/LC_MESSAGES/passwd.mo
        /usr/share/locale/zh_CN/LC_MESSAGES/passwd.mo
        /usr/share/locale/zh_TW/LC_MESSAGES/passwd.mo
        /usr/share/locale/ar/LC_MESSAGES/passwd.mo
        /usr/share/locale/as/LC_MESSAGES/passwd.mo
        /usr/share/locale/ast/LC_MESSAGES/passwd.mo
        /usr/share/locale/bn/LC_MESSAGES/passwd.mo
        /usr/share/locale/bn_IN/LC_MESSAGES/passwd.mo
        /usr/share/locale/bs/LC_MESSAGES/passwd.mo
        /usr/share/locale/cy/LC_MESSAGES/passwd.mo
        /usr/share/locale/et/LC_MESSAGES/passwd.mo
        /usr/share/locale/eu/LC_MESSAGES/passwd.mo
        /usr/share/locale/fa/LC_MESSAGES/passwd.mo
        /usr/share/locale/gu/LC_MESSAGES/passwd.mo
        /usr/share/locale/he/LC_MESSAGES/passwd.mo
        /usr/share/locale/hi/LC_MESSAGES/passwd.mo
        /usr/share/locale/hy/LC_MESSAGES/passwd.mo
        /usr/share/locale/is/LC_MESSAGES/passwd.mo
        /usr/share/locale/kn/LC_MESSAGES/passwd.mo
        /usr/share/locale/ku/LC_MESSAGES/passwd.mo
        /usr/share/locale/lo/LC_MESSAGES/passwd.mo
        /usr/share/locale/mk/LC_MESSAGES/passwd.mo
        /usr/share/locale/ml/LC_MESSAGES/passwd.mo
        /usr/share/locale/mr/LC_MESSAGES/passwd.mo
        /usr/share/locale/ms/LC_MESSAGES/passwd.mo
        /usr/share/locale/my/LC_MESSAGES/passwd.mo
        /usr/share/locale/nds/LC_MESSAGES/passwd.mo
        /usr/share/locale/nn/LC_MESSAGES/passwd.mo
        /usr/share/locale/or/LC_MESSAGES/passwd.mo
        /usr/share/locale/pa/LC_MESSAGES/passwd.mo
        /usr/share/locale/pt/LC_MESSAGES/passwd.mo
        /usr/share/locale/si/LC_MESSAGES/passwd.mo
        /usr/share/locale/sr@latin/LC_MESSAGES/passwd.mo
        /usr/share/locale/ta/LC_MESSAGES/passwd.mo
        /usr/share/locale/te/LC_MESSAGES/passwd.mo
        /usr/share/locale/ur/LC_MESSAGES/passwd.mo
        /usr/share/locale/wa/LC_MESSAGES/passwd.mo
        /usr/share/man/man5/passwd.5.gz
        /usr/share/man/man1/passwd.1.gz
        /usr/share/man/man3/passwd2des.3.gz
        /usr/share/man/ja/man1/passwd.1.gz
        /usr/share/vim/vim80/ftplugin/passwd.vim
        /usr/share/vim/vim80/syntax/passwd.vim
        /usr/share/augeas/lenses/dist/passwd.aug
        /usr/share/bash-completion/completions/passwd
        /usr/share/awk/passwd.awk
        
      • 范例:根据节点编号搜索

        [root@centos8 ~]# ll /etc/passwd -i
        134796888 -rw-r--r--. 1 root root 2502 Mar 16 18:54 /etc/passwd
        [root@centos8 ~]# find / -inum 134796888
        /etc/passwd
        
      • 范例:创建一个软连接,搜索节点编号

        [root@centos8 ~]# ln /etc/passwd /etc/sysconfig/passwd.link   "创建一个软链接"
        [root@centos8 ~]# find / -inum 134796888       “搜索节点编号”
        /etc/passwd                                    "会出现一个链接,一个文件"
        /etc/sysconfig/passwd.link
        [root@centos8 ~]#
        
      • 范例:查看节点编号和文件属性

        [root@centos8 ~]# find / -inum 128 -ls
              128      0 dr-xr-xr-x  20  root     root          259 Apr  6 17:03 /
              128     12 -rw-r--r--   1  root     root         9892 Mar 16 18:54 /boot/grub2/i386-pc/jpeg.mod
              128      0 drwxr-xr-x   2  root     root           68 Apr  6 11:59 /data
              128      0 -rw-r--r--   1  root     root         4096 Apr  7 10:42 /sys/devices/system/memory/memory3/online
              128      0 -r--r--r--   1  root     root            0 Apr  6 17:15 /sys/fs/cgroup/blkio/system.slice/irqbalance.service/blkio.throttle.io_serviced
        
      • 查看相同节点编号

        [root@centos8 ~]# find / -samefile /
        find: ‘/proc/76775/task/76775/fd/6’: No such file or directory
        find: ‘/proc/76775/task/76775/fdinfo/6’: No such file or directory
        find: ‘/proc/76775/fd/7’: No such file or directory
        find: ‘/proc/76775/fdinfo/7’: No such file or directory
        
        "报错是内存信息的文件会随时变化就会报错"
        
      • 使用正则表达式搜索文件

        [root@centos8 ~]# find / -regex '.*\.jpg$'
        /usr/share/cups/www/images/smiley.jpg
        /usr/share/pixmaps/faces/astronaut.jpg
        /usr/share/pixmaps/faces/bicycle.jpg
        /usr/share/pixmaps/faces/book.jpg
        /usr/share/pixmaps/faces/calculator.jpg
        
  • 根据属主、属组查找

    • 常见选项:

      -user USERNAME:查找属主为指定用户(UID)的文件
      -group GRPNAME: 查找属组为指定组(GID)的文件
      -uid UserID:查找属主为指定的UID号的文件
      -gid GroupID:查找属组为指定的GID号的文件
      -nouser:查找没有属主的文件
      -nogroup:查找没有属组的文件
      
    • 范例:查找属主为指定用户(UID)的文件

      [root@centos8 ~]# find / -user yanghaipeng
      find: ‘/proc/76898/task/76898/fd/6’: No such file or directory
      find: ‘/proc/76898/task/76898/fdinfo/6’: No such file or directory
      find: ‘/proc/76898/fd/7’: No such file or directory
      find: ‘/proc/76898/fdinfo/7’: No such file or directory
      /var/spool/mail/yanghaipeng
      /home/yanghaipeng
      /home/yanghaipeng/.mozilla
      /home/yanghaipeng/.mozilla/extensions
      /home/yanghaipeng/.mozilla/plugins
      /home/yanghaipeng/.bash_logout
      /home/yanghaipeng/.bash_profile
      /home/yanghaipeng/.bashrc
      /home/yanghaipeng/.config
      
      
  • 根据文件类型查找

    -type TYPE
    TYPE可以是以下形式:
    f: 普通文件
    d: 目录文件
    l: 符号链接文件
    s:套接字文件
    b: 块设备文件
    c: 字符设备文件
    p: 管道文件
    
    • 范例:搜索etc文件下的前十个目录文件

      [root@centos8 home]# find /etc -type d -ls |head -10
      134320257     12 drwxr-xr-x 134  root     root         8192 Apr  7 10:58 /etc
      201326722      0 drwxr-xr-x   8  root     root          128 Mar 16 18:49 /etc/dnf
            134      0 drwxr-xr-x   2  root     root          191 May 14  2019 /etc/dnf/modules.d
      134329871      0 drwxr-xr-x   2  root     root            6 May 14  2019 /etc/dnf/aliases.d
      201327706      0 drwxr-xr-x   2  root     root            6 May 14  2019 /etc/dnf/modules.defaults.d
            820      0 drwxr-xr-x   3  root     root           89 Mar 16 18:51 /etc/dnf/plugins
      134877459      0 drwxr-xr-x   2  root     root            6 May 15  2019 /etc/dnf/plugins/copr.d
       67110542      0 drwxr-xr-x   2  root     root           59 Mar 16 18:52 /etc/dnf/protected.d
      134329872      0 drwxr-xr-x   2  root     root           37 Mar 16 18:49 /etc/dnf/vars
      134320275      0 drwxr-xr-x   3  root     root           38 Mar 16 18:50 /etc/fonts
      [root@centos8 home]#
      
  • 空文件或目录

    • 格式:

      -empt
      
      可以与其它的逻辑的与或非进行组合
      find /app -type d -empty
      
    • 范例:搜索etc文件夹下的空文件和空目录

      [root@centos8 home]# find /etc -empty
      /etc/crypttab
      /etc/dnf/aliases.d
      /etc/dnf/modules.defaults.d
      /etc/dnf/plugins/copr.d
      /etc/X11/applnk
      /etc/X11/xinit/Xclients.d
      /etc/skel/.mozilla/extensions
      /etc/skel/.mozilla/plugins
      
  • 组合条件

    • 与:-a,两个条件必须同时满足

    • 或:-o,两个条件满足其一即可

    • 非:-not, !,对条件取反的

    • 德·摩根定律:

      • (非 A) 或 (非 B) = 非(A 且 B)
      • (非 A) 且 (非 B) = 非(A 或 B)
    • 示例:

      • !A -a !B = !(A -o B)
      • !A -o !B = !(A -a B)
    • 范例:对比查找优先级范例

      [root@centos8 etc]# find /etc/ -type d -o -type l -ls  “查看etc目录下的目录文件或软连接文件满足其中一个就显示详细属性”
      [root@centos8 etc]# find /etc/ -type d -o -type l |wc -l   “统计etc目录下的目录文件或软练接文件的个数”
      632
      [root@centos8 etc]# find /etc/ -type d -o -type l -ls |wc -l   “查看目录文件和软连接文件但是输出先统计了软连接文件个数”
      249
      [root@centos8 etc]# find /etc/ -type l -ls |wc -l  “统计软链接个数”
      249
      [root@centos8 etc]# find /etc/ \( -type d -o -type l \) -ls |wc -l   “先查看etc文件目录下的所有目录文件或软连接详细属性,再统计个数”
      632
      
    • 范例:取反

      [root@centos8 etc]# find /tmp -not -user root
      /tmp/.X11-unix/X1024
      /tmp/.ICE-unix/1190
      /tmp/.X1024-lock
      [root@centos8 etc]#
      
    • 范例:德摩根定律:

      [root@centos8 ~]# useradd xiaoming
      [root@centos8 ~]# useradd xiaohong
      [root@centos8 ~]# useradd xiaolan
      [root@centos8 ~]# cd /home
      [root@centos8 home]# ll
      total 4
      drwx------.  3 test        test          78 Apr  7 10:58 test
      drwx------.  3 xiaohong    xiaohong      78 Apr  7 11:35 xiaohong
      drwx------.  3 xiaolan     xiaolan       78 Apr  7 11:35 xiaolan
      drwx------.  3 xiaoming    xiaoming      78 Apr  7 11:35 xiaoming
      [root@centos8 home]#
      [root@centos8 home]# find ! \( -user xiaoming -o -user xiaohong \)  "查找除了小红小明账号的其他账号文件"
      .
      ./test
      ./test/.mozilla
      ./test/.mozilla/extensions
      ./test/.mozilla/plugins
      ./test/.bash_logout
      ./test/.bash_profile
      ./test/.bashrc
      ./xiaolan
      ./xiaolan/.mozilla
      ./xiaolan/.mozilla/extensions
      ./xiaolan/.mozilla/plugins
      ./xiaolan/.bash_logout
      ./xiaolan/.bash_profile
      ./xiaolan/.bashrc
      
      find ! -user xiaoming -a ! -user xiaohong
      find ! \( -user xiaoming -o -user xiaohong \)
      "两个命令相同"
      
    • 范例:找出/tmp目录下,属主不是root,且文件名不以f开头的文件

      find /tmp ( -not -user root -a -not -name 'f*' ) -ls
      find /tmp -not ( -user root -o -name 'f*' ) –ls
      
  • 排除目录

    • 范例:查找/etc/下,除/etc/sane.d目录的其它所有.conf后缀的文件

      find /etc -path '/etc/sane.d' -a -prune -o -name "*.conf"
      ""
      
    • 范例:查找/etc/下,除/etc/sane.d和/etc/fonts两个目录的所有.conf后缀的文件

      find /etc \( -path "/etc/sane.d" -o -path "/etc/fonts" \) -a -prune -o -name
      "*.conf"
      
    • 范例:排除/proc和/sys目录

      find / \( -path "/sys" -o -path "/proc" \) -a -prune -o -type f -a -mmin -1
      
  • 根据文件大小来查找

    • 格式:

      -size [+|-]#UNIT
      常用单位:k, M, G,c(byte),注意大小写敏感
      #UNIT: (#-1, #]   
      如:6k 表示(5k,6k]  "搜索6k文件,实则搜索的是5k-6k之间的文件"
      -#UNIT:[0,#-1]
      如:-6k 表示[0,5k]
      +#UNIT:(#,∞)
      如:+6k 表示(6k,∞)
      
      find / -size +10G
      
    • 范例:

      [root@centos8 home]# dd if=/dev/zero of=/data/f1.img bs=1M count=10  "建立一个10M文件"
      10+0 records in
      10+0 records out
      10485760 bytes (10 MB, 10 MiB) copied, 0.00312204 s, 3.4 GB/s
      [root@centos8 home]# find / -size 10M   
      /data/f1.img
      find: ‘/proc/77781/task/77781/fd/5’: No such file or directory
      find: ‘/proc/77781/task/77781/fdinfo/5’: No such file or directory
      find: ‘/proc/77781/fd/6’: No such file or directory
      find: ‘/proc/77781/fdinfo/6’: No such file or directory
      /var/lib/sss/mc/initgroups
      /var/cache/PackageKit/8/metadata/BaseOS-8-x86_64/packages/iwl7260-firmware-25.30.13.0-94.el8.1.noarch.rpm
      /usr/lib64/dri/i965_dri.so
      
  • 根据时间戳

    • 格式:

      以“天”为单位:
      -atime [+|-]#
      #: [#,#+1)
      +#: [#+1,∞]
      -#: [0,#)
      -mtime
      -ctime
      以“分钟”为单位:
      -amin
      -mmin
      -cmin
      
  • 根据权限查找

    • 格式:

      -perm [/|-]MODE
      MODE: 精确权限匹配
      /MODE:任何一类(u,g,o)对象的权限中只要能一位匹配即可,或关系,+ 从CentOS 7开始淘汰
      -MODE:每一类对象都必须同时拥有指定权限,与关系
      0 表示不关注
      
      • 说明:
        • find -perm 755 会匹配权限模式恰好是755的文件
        • 只要当任意人有写权限时,find -perm +222就会匹配
        • 只有当每个人都有写权限时,find -perm -222才会匹配
        • 只有当其它人(other)有写权限时,find -perm -002才会匹配
    • 范例:

      #搜索只要有4的权限
      [root@centos8 home]# find /data/ -perm /444
      /data/
      /data/m.txt.Z
      /data/m2.txt
      /data/m.txt.gz
      /data/m2.txt.gz
      /data/f1.img
      [root@centos8 home]#
      
      #所属组的权限
      [root@centos8 home]# find /data/ -perm -444
      /data/
      /data/m.txt.Z
      /data/m2.txt
      /data/m2.txt.gz
      /data/f1.img
      [root@centos8 home]#
      
  • 处理动作

    • 常用选项:

      - -print:默认的处理动作,显示至屏幕
      - -ls:类似于对查找到的文件执行“ls -l”命令
      - -delete:删除查找到的文件
      - -fls file:查找到的所有文件的长格式信息保存至指定文件中
      - -ok COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会交互式要求用户确认
      - -exec COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令
      - {}: 用于引用查找到的文件名称自身
      
    • find传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令

    • 范例:

      #备份配置文件,添加.orig这个扩展名
      find -name ".conf" -exec cp {} {}.orig \;
      #提示删除存在时间超过3天以上的joe的临时文件
      find /tmp -ctime +3 -user joe -ok rm {} \;
      #在主目录中寻找可被其它用户写入的文件
      find ~ -perm -002 -exec chmod o-w {} \;
      #查找/data下的权限为644,后缀为sh的普通文件,增加执行权限
      find /data –type f -perm 644 -name "*.sh" –exec chmod 755 {} \;
      
  • 参数替换xargs

    • 介绍:

      • 由于很多命令不支持管道|来传递参数,而日常工作中有这个必要,所以就有了
        xargs命令。

      • xargs用于产生某个命令的参数,xargs 可以读入 stdin 的数据,并且以空格符
        或回车符将 stdin 的数据分隔成为arguments

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

      • 有些命令不能接受过多参数,命令执行可能会失败,xargs可以解决

      • 范例:

         ls f |xargs rm
         find /sbin -perm +700 |ls -l 这个命令是错误的
         find /sbin -perm +7000 | xargs ls –l 查找特殊权限的文件
         `find -name ‘.log’ -print0 | xargs -0 rm`
        
    • 格式:

      find | xargs COMMAND
      
    • 范例:

      #显示10个数字
      [root@centos8 ~]#seq 10 | xargs
      1 2 3 4 5 6 7 8 9 10
      #删除当前目录下的大量文件
      ls | xargs rm
      #
      find -name "*.sh" | xargs ls -Sl
      [root@centos8 data]#echo {1..10} |xargs
      1 2 3 4 5 6 7 8 9 10
      [root@centos8 data]#echo {1..10} |xargs -n1
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      [root@centos8 data]#echo {1..10} |xargs -n2
      1 2
      3 4
      5 6
      7 8
      9 10
      #批量创建和删除用户
      echo user{1..10} |xargs -n1 useradd
      echo user{1..100} | xargs -n1 userdel -r
      #这个命令是错误的
      find /sbin/ -perm /700 | ls -l
      #查找有特殊权限的文件,并排序
      find /bin/ -perm /7000 | xargs ls -Sl
      #此命令和上面有何区别?
      find /bin/ -perm -7000 | xargs ls -Sl
      #以字符nul分隔
      find -type f -name "*.txt” -print0 | xargs -0 rm
      #并发执行多个进程
      seq 100 |xargs -i -P10 wget -P /data http://10.0.0.8/{}.html
      #并行下载视频
      seq 199 | xargs -i -P3 you-get https://www.bilibili.com/video/BV1Et411E7dx?p={}
      
    • find示例:

      #备份配置文件,添加.orig这个扩展名
      find -name "*.conf" --exec cp {} {}.orig \
      #提示删除存在时间超过3天以上的joe的临时文件
       find /tmp -ctime +3 -user joe -ok rm {} \;
      #在主目录中寻找可被其它用户写入的文件
       find ~ -perm -002 -exec chmod o-w {} \;
      #查找/data下的权限为644,后缀为sh的普通文件,增加执行权限
       find /data –type f -perm 644 -name “*.sh” –exec chmod 755 {} \;
      #查看/home的目录
       find /home –type d -ls
      
  • 练习:

    • 查找/var目录下属主为root,且属组为mail的所有文件

      find /var -user root -group mail  -type f
      
    • 查找/var目录下不属于root、lp、gdm的所有文件

      find /var -not -user root -a -not -user lp -a -not -user gdm 
      find /var -not \(-user root -user lp -user gdm\)
      
    • 查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件

      find /var -mtime -7 -a -not \(-user root -o -user postfix)\ |xargs ls
      
    • 查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件

      find / -nouser -o -nogroup -type f -atime -7
      
    • 查找/etc目录下大于1M且类型为普通文件的所有文件

      find /etc -type f -size +1M
      
    • 查找/etc目录下所有用户都没有写权限的文件

      find /etc -type f -not -perm +222
      find /etc -type f -not -perm /222
      
    • 查找/etc目录下至少有一类用户没有执行权限的文件

      find /etc -not -perm -111 -ls
      
    • 查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件

      find /etc/init.d/ -type f -perm -113
      

文件压缩及归档

  • 文件压缩解压缩命令:

    • compress/uncompress: .Z
    • gzip/gunzip: .gz
    • bzip2/bunzip2: .bz2
    • xz/unxz: .xz
    • zip/unzip
    • tar
    • cpio

compress和uncompress

  • 此工具来自于ncompress包

  • 格式:

    compress [-dfvcVr] [-b maxbits] [file …]
    
  • 常用选项

    -d: 解压缩,相当于uncompress
    -c: 结果输出至标准输出,不删除原文件
    -v: 显示详情
    
  • uncompress 解压缩

  • zcat file.Z 不显式解压缩的前提下查看文本文件内容

    zcat file.Z >file
    

gzip/gunzip

  • 格式:

    gzip [OPTION]… FILE …
    
  • 常用选项

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

    unzip sysconfig.zip
    cat /var/log/messages | zip messages -
    unzip -p message > message
    
  • 范例:

    #解压缩
    gunzip file.gz
    #不显式解压缩的前提下查看文本文件内容
    zcat file.gz
    
  • 通用的解压参数 -d

  • 范例:

    #不显式解压缩的前提下查看文本文件内容
    gzip -c messages >messages.gz
    #不显式解压缩的前提下查看文本文件内容
    gzip -c -d messages.gz > messages
    #不显式解压缩的前提下查看文本文件内容
    zcat messages.gz > messages
    #不显式解压缩的前提下查看文本文件内容
    cat messages | gzip > m.gz
    

bzip2/bunzip2/bzcat

  • 来自于 bzip2 包

  • 格式:

    bzip2 [OPTION]… FILE …
    
  • 常用选项:

    -k keep, 保留原文件
    -d 解压缩
    -c 结果输出至标准输出,保留原文件不改变
    -# 1-9,压缩比,默认为9
    
  • bzcat:不显式解压缩的前提下查看文本文件内容

  • 范例:

    bunzip2 file.bz2 解压缩
    bzcat file.bz2 不显式解压缩的前提下查看文本文件内容
    

xz/unxz/xzcat

  • 来自于 xz包

  • 格式:

    xz [OPTION]… FILE …
    
  • 常用选项:

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

    unxz file.xz 解压缩
    xzcat file.xz 不显式解压缩的前提下查看文本文件内容
    
  • xzcat: 不显式解压缩的前提下查看文本文件内容

zip/unzip

  • 介绍:

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

  • 打包压缩

    zip –r /testdir/sysconfig /etc/sysconfig/
    
  • 范例:

    #打包并压缩
    zip –r /backup/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
    
  • 解包解压缩

    • unzip sysconfig.zip
    • at /var/log/messages | zip messages -
    • unzip -p message > message
    • 范例
    zip -r 1.gz 1.file
    unzip 1.gz
    

总结

  • 通用的解压参数 -d
  • 压缩保留原文件
    • xz -k /var/log/message
    • xz -k /var/log/message
    • xz -d /var/log/message.xz
    • bzip2 -k /var/log/message
    • bzip2 -k /var/log/message
    • bzip -d /var/log/message
    • zip
    • zip -r message.zip /var/log/message
    • unzip /var/log/message

tar

  • 介绍:

    tar 即 Tape ARchive 磁带归档,可以对目录和多个文件打包一个文件,并且可以压缩,保留文件属性不
    丢失,常用于备份功能,推荐使用

  • 格式:

    tar [OPTION]...
    
  • 创建归档

    tar -cpvf /PATH/TO/SOMEFILE.tar FILE…
    
  • 追加文件至归档: 注:不支持对压缩文件追加

    tar -r -f /PATH/TO/SOMEFILE.tar FILE…
    
  • 查看归档文件中的文件列表

    tar -t -f /PATH/TO/SOMEFILE.tar
    
  • 展开归档

    tar -x -f /PATH/TO/SOMEFILE.tar
    tar -x -f /PATH/TO/SOMEFILE.tar -C /PATH/
    
  • 结合压缩工具实现:归档并压缩

    -j: bzip2, 相当于bzip2压缩工具
    -z: gzip, 相当于gzip压缩工具
    -J: xz,相当于xz压缩工具
    
  • 范例:

    tar -zcvf etc.tar.gz /etc
    tar -jcvf etc.tar.bz2 /etc
    tar -Jcvf etc.tar.xz /etc
    
  • 只打包目录内的文件,不所括目录本身

    [root@centos8 ~]#cd /etc
    [root@centos8 etc]#tar zcvf /root/etc.tar.gz *
    
  • 利用 tar 进行文件复制

    [root@centos8 ~]#tar c /data/ | tar x -C /backup
    tar: Removing leading `/' from member names
    
  • -exclude 排除文件

    • 范例:

      tar zcvf /root/a3.tgz --exclude=/app/host1 --exclude=/app/host2 /app
      
    • -T 选项指定输入文件 -X 选项指定包含要排除的文件列表

      tar zcvf mybackup.tgz -T /root/includefilelist -X /root/excludefilelist
      
  • splist: 分割一个文件为多个文件

    • 分割大的 tar 文件为多份小文件

      • split –b Size –d tar-file-name prefix-name

      • split -b 1M –d mybackup.tgz mybackup-parts

    • 切换成的多个小分文件使用数字后缀

      • split -b 1M mybackup.tgz mybackup-parts
    • 合并:将多个切割的小文件合并成一个大文件

      • cat mybackup-parts* > mybackup.tar.gz

cpio

  • 功能:复制文件从或到归档

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

  • 格式:

    - cpio [选项] > 文件名或者设备名
    - cpio [选项] < 文件名或者设备名
    
  • 常见选项

    -o 将文件拷贝打包成文件或者将文件输出到设备上
    -O filename 输出到指定的归档文件名
    -A 向已存在的归档文件中追加文件
    -i 解包,将打包文件解压或将设备上的备份还原到系统
    -I filename 对指定的归档文件名解压
    -t 预览,查看文件内容或者输出到设备上的文件内容
    -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
    
posted @ 2020-04-07 22:36  Coolcoc  阅读(328)  评论(0)    收藏  举报