Linux基础-文件查找find与打包压缩tar详解

find详解

语法:

find    路径    条件    跟条件相关的操作符         [-exec   动作]

路径:1.默认不写路径事务,查找的是当前路径。2.加路径

条件:指定的名称、文件类型、权限、时间

1.按文件名

从根目录查找文件

[root@b-2 ~]# find / -name "file" 
/usr/bin/file
/usr/share/file
从/etc目录查找文件

[root@b-2 ~]# find /etc/ -name "ifcfg-ens33"
/etc/sysconfig/network-scripts/ifcfg-ens33
 

忽略大小写查找          -i忽略大小写

[root@b-2 ~]# find /etc/ -iname "ifcfg-ens33"
/etc/sysconfig/network-scripts/ifcfg-ens33
 

熟用*通配符

查看以 .txt 结尾的文件

[root@b-2 ~]# find /etc/ -iname "*.txt"
/etc/pki/nssdb/pkcs11.txt

2.按文件大小

[root@b-2 ~]# find /etc -size +5M                #大于5M
/etc/udev/hwdb.bin

[root@b-2 ~]# find /etc -size 5M                #等于5M


[root@b-2 ~]# find /etc -size -5M                #小于5M
/etc
/etc/fstab
/etc/crypttab
/etc/mtab

限制查找

 [root@b-2 ~]# find / -size +3M -a -size -5M                #查找大于3M而且小于5M的文件
/boot/System.map-3.10.0-693.el7.x86_6

[root@b-2 ~]# find / -size -3M -o -size +5M                #查找大于3M或者小于5M的文件

[root@b-2 ~]# find / -size -3M -a -name "*.txt"                #查找小于3M而且名字是 .txt结尾的文件

3.按时间查找

按时间找(atime,mtime,ctime)

-atime=访问时间

-mtime=改变时间  内容修改时间会改变

-ctime=修改时间  属性修改时间会改变

-amin   访问时间  分钟

-mmin   修改时间  分钟

[root@b-2 ~]# find /opt -mtime +5                修改时间5天之前

[root@b-2 ~]# find /opt -atime +1                访问时间1天之前

                        find / -mtime -2                     修改时间2天之内

find . -amin +1                 访问时间在1分钟之前

find . -amin  -4                  访问时间在4分钟之内

find . -mmin -2                 修改时间在2分钟之内

4.按文件类型

[root@qfedu.com ~]# find /dev -type f    #f普通文件
[root@qfedu.com ~]# find / -type f -size -1M -o -name "*.txt"

[root@qfedu.com ~]# find /dev -type d    #d目录
[root@qfedu.com ~]# find /etc/ -type d -name "*.conf.d"

[root@qfedu.com ~]# find /etc -type l    #l链接

[root@qfedu.com ~]# find /dev -type b    #b块设备
[root@qfedu.com ~]# find /dev/ -type b -name "sd*"

5.按文件权限

[root@qfedu.com ~]# find . -perm 644            #.是当前目录    精确查找644  
[root@qfedu.com ~]# find /usr/bin  -perm -4000  #包含set uid
[root@qfedu.com ~]# find /usr/bin  -perm -2000  #包含set gid
[root@qfedu.com ~]# find /usr/bin  -perm -1000  #包含sticky

6.找到后的处理动作 

[root@qfedu.com ~]# find /etc -name "ifcfg*" -exec cp -rf {} /tmp \; #exec命令对之前查找出来的文件做进一步操作-----  查找带ifcfg开头的文件复制到tmp下
[root@qfedu.com ~]# touch /home/test{1..20}.txt
[root@qfedu.com ~]# find /home/ -name test* -exec rm -rf {} \; #{}为前面查找到的内容,\; 格式

7.结合xargs

[root@qfedu.com ~]# touch /home/test{1..20}.txt
[root@qfedu.com ~]# # find /home/ -name "test*" | xargs -i cp {} /tmp/ #找到之后删除处理xargs 参数传递

8.-exec和xargs的区别

-exec:参数是一个一个传递的,传递一个参数执行一次命令。
xargs:将前一个命令的标准输出传递给下一个命令,作为它的参数转换成下一个命令的参数列表。
===============
1、exec 每处理一个文件或者目录,它都需要启动一次命令,效率不好; 
2、exec 格式麻烦,必须用 {} 做文件的代位符,必须用 \来转义; 作为命令的结束符,书写不便。 
3、xargs不能操作文件名有空格的文件;

综上,如果要使用的命令支持一次处理多个文件,并且也知道这些文件里没有带空格的文件,
那么使用 xargs比较方便; 否则,就要用 exec了。

打包压缩

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值