文件查找及压缩

文件查找

1、命令文件

which ls     #从PATH环境变量 (echo $PATH)
whereis vim

[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/htop/bin/:/root/bin

任意文件

A. locate (查询的数据库: /var/lib/mlocate/mlocate.db)
计划任务:每天自动更新数据库 /etc/cron.daily/mlocate.cron
手动更新数据库:updatedb
locate ifcfg-eth0
locate ifcfg-enp0s25
B. find
find [options] [path…] [expression] [action]
find 选项 路径 类型 动作

按文件名:

[root@test ~]# find /etc -name “ifcfg-eth0”
[root@test ~]# find /etc -iname “ifcfg-eth0” #-i忽略大小写
[root@test ~]# find /etc -iname “ifcfg-eth*”

按文件大小:

[root@test ~]# find /etc -size +5M     #大于5M
[root@test ~]# find /etc -size 5M
[root@test ~]# find /etc -size -5M
[root@test ~]# find /etc -size +5M -ls #-ls找到的处理动作

指定查找的目录深度:

-maxdepth levels
-mindepth levels
[root@test ~]# find / -maxdepth 3 -a -name “ifcfg-eth0”

按时间找(atime,mtime,ctime):

[root@test ~]# find /etc -mtime +5  #修改时间超过5天
[root@test ~]# find /etc -mtime 5   #修改时间等于5天
[root@test ~]# find /etc -mtime -5  #修改时间5天以内

按文件属主、属组找:

[root@test ~]# find /home -user jack #属主是jack的文件
[root@test ~]# find /home -group hr  #属组是hr组的文件
[root@test ~]# find /home -user jack -group hr
[root@test ~]# find /home -user jack -a -group hr
[root@test ~]# find /home -user jack -o -group hr
[root@test ~]# find /home -nouser
[root@test ~]# find /home -nogroup
[root@test ~]# find /home -nouser -o -nogroup

按文件类型:

[root@test ~]# find /dev -type f   #f普通
[root@test ~]# find /dev -type d   #d目录
[root@test ~]# find /dev -type l   #l链接
[root@test ~]# find /dev -type b   #b块设备
[root@test ~]# find /dev -type c   #c字符设备
[root@test ~]# find /dev -type s   #s套接字
[root@test ~]# find /dev -type p   #p管道文件

按文件权限:

[root@test ~]# find . -perm 644 -ls
[root@test ~]# find . -perm -644 -ls
[root@test ~]# find . -perm -600 -ls
[root@test ~]# find . -perm -222 -ls //全局可写
[root@test ~]# find /usr/bin /usr/sbin -perm -4000 -ls //包含set uid
[root@test ~]# find /usr/bin /usr/sbin -perm -2000 -ls //包含set gid
[root@test ~]# find /usr/bin /usr/sbin -perm -1000 -ls //包含sticky

按正则表达式:

-regex pattern
[root@test ~]# find /etc -regex ‘.*ifcfg-eth[0-9]’
.* 任意多个字符
[0-9] 任意一个数字

[root@localhost ~]# find /etc -regex ‘.*ifcfg-enp0s25’
/etc/sysconfig/network-scripts/ifcfg-enp0s25

[root@localhost ~]# find /etc -regex ‘.*ifcfg-enp0s[0-9]+’
/etc/sysconfig/network-scripts/ifcfg-enp0s25

找到后处理的动作 ACTIONS: (默认动作-print)

-print
-ls
-delete
-exec 后面跟自定义的shell命令
-ok 后面跟自定义的shell命令
[root@test ~]# find /etc -name “ifcfg*”
[root@test ~]# find /etc -name “ifcfg*” -print
[root@test ~]# find /etc -name “ifcfg*” -ls
[root@test ~]# find /etc -name “ifcfg*” -exec cp -rvf {} /tmp \;
[root@test ~]# find /etc -name “ifcfg*” -ok cp -rvf {} /tmp \;

[root@test ~]# find /etc -name “ifcfg*” -exec rm -rf {} \;
[root@test ~]# find /etc -name “ifcfg*” -delete

find结合xargs

[root@test ~]# find . -name “yang*.txt” |xargs rm -rf
[root@test ~]# find /etc -name “ifcfg-eth0” |xargs -I {} cp -rf {} /var/tmp

文件打包

打包命令tar

tar [选项] 参数
-c:建立新的备份文件
-v:显示指令执行过程
-f:指定备份文件
-x:从打包文件中提取文件
-k:保留原有文件不被覆盖
-p:保留原来的文件权限和属性
-m:保留文件不被覆盖
-w:确认压缩文件的正确性
-j:通过bzip2指令压缩/解压缩文件 命名为*.tar.bz2
-z:通过gzip指令压缩/解压缩文件 命名为*.tar.gz
-o:将文件解开到标准输出
-C:将文件解压缩到指定目录中

Example:

打包

[root@test ~]# tar -czf etc1.tar.gz /etc    #-z 调用gzip
[root@test ~]# tar -cjf etc2.tar.bz2 /etc   #-j 调用bzip2
[root@test ~]# tar -cJf etc3.tar.xz /etc    #-J 调用xz
[root@test ~]# ll -h etc*
-rw-r–r–. 1 root root 8.7M 3月 12 00:08 etc1.tar.gz
-rw-r–r–. 1 root root 7.5M 3月 12 00:08 etc2.tar.bz2
-rw-r–r–. 1 root root 4.8M 3月 12 00:09 etc3.tar.xz

解压

[root@test ~]# tar -tf sys.tar.xz
[root@test ~]# tar -xzvf etc1.tar.gz
[root@test ~]# tar -xvf etc1.tar.gz          #无需指定解压工具,tar会自动判断
[root@test ~]# tar -xvf etc2.tar.bz2 -C /tmp #-C重定向到//tmp目录
[root@test ~]# tar xf etc3.tar.xz
[root@test ~]# unzip xxx.zip
posted @ 2021-08-27 15:14  Cai_HL  阅读(78)  评论(0编辑  收藏  举报
>