首页 > 技术文章 > 快速文件信息命令(find)

xgg123 2022-01-08 15:59 原文

1)精确查找

find 找寻的路径范围 -type 类型信息 -name "文件名称"

 [root@hgg ~]# find /etc -type f -name "ifcfg-eth0"
 
 /etc/sysconfig/network-scripts/ifcfg-eth0

2)忽略大小写查找

忽略大小写在name前加 i

find /etc -type f -iname "ifcfg-eth0"

3)模糊查找

一个文件名称没记全,如何查看文件路径可以尝试用

*代替进行模糊搜索

 [root@hgg ~]# find /etc -type f -name "ifc*"
 
 /etc/sysconfig/network-scripts/ifcfg-lo
 /etc/sysconfig/network-scripts/ifcfg-eth0
 
 [root@hgg ~]# find /etc -type f -name "*th0"
 /etc/sysconfig/network-scripts/ifcfg-eth0

4)根据文件大小进行查找(-size)

find 找寻的路径范围 -size 大小

 [root@hgg ~]# find /hgg -size  +10k
  ---找出大于10k的文件
 
 [root@hgg ~]# find /hgg -size -10k
  ---找出小于10k的文件
 
 [root@hgg ~]# find /hgg -size -1M
  ---找出小于1M的文件

常用大小查找

c ---代表字节

k ---代表kb

M ---代表MB

5)根据目录指定层级查找目录(-maxdepth)

find 找寻的路径范围 -maxdepth 1(看几层) -type 类型信息 -name "文件名称"

 [root@hgg hgg1]# find /hgg -type f  -name "hgg.txt"
 /hgg/hgg.txt
 /hgg/hgg1/hgg.txt
 
 [root@hgg hgg1]# find /hgg -maxdepth 1 -type f -name "hgg.txt"
 /hgg/hgg.txt

 

6)批量删除

如何找出/hgg目录中.txt结尾的文件并且删除

方法一:

find /hgg -type f -name "*.txt" -delete

 [root@hgg ~]# ls /hgg
 hgg hggboy.txt hggdog.txt
 [root@hgg ~]# find /hgg -type f -name "*.txt" -delete
 [root@hgg ~]# ls /hgg
 hgg

方法二:

find /hgg -type f -name "*.txt" -exec rm -rf {} \;

方法三:

find /hgg -type f -name "*.txt" |xargs rm -f

xargs:表示把内容转为一行

7)批量复制与移动

批量移动:

01、find /hgg -type f -name "*.txt" -exec mv {} /tmp \;

02、find /hgg -type f -name "*.txt" |xargs -i mv {} /hggdog

 [root@hgg ~]# ls /hgg
 hgg hggboy.txt hggdog.txt
 
 [root@hgg ~]# find /hgg -type f -name "*.txt" -exec mv {} /tmp \;
 [root@hgg ~]# ls /tmp/hggboy.txt
 /tmp/hggboy.txt
 

批量复制:

01、 find /hgg -type f -name "*.txt" -exec cp {} /tmp \;

 

02、 find /hgg -type f -name "*.txt" |xargs -i cp {} /hggdog

推荐阅读