美文网首页
find 命令使用详解

find 命令使用详解

作者: 小屁孩云熙 | 来源:发表于2021-08-19 12:42 被阅读0次

1. 功能

查询文件所在路径

2. 语法及常用参数

2.1 语法格式

find 查找范围 -type 文件类型 -name 文件名  (精确查找)
find 查找范围 -type 文件类型 -name 文件名* (模糊查找)

2.2 参数

2.2.1 忽略大小写

-i      -- 忽略大小写
find 查找范围 -type 文件类型 -iname 文件名

2.2.2 按文件大小查找

-size       -- 按照文件大小查找
    `b'    for 512-byte blocks (this is the default if no suffix is used)
    `c'    for bytes (推荐)
    `w'    for two-byte words
    `k'    for Kilobytes (units of 1024 bytes) (推荐)
    `M'    for Megabytes (units of 1048576 bytes) (推荐)
    `G'    for Gigabytes (units of 1073741824 bytes)

# 查找根目录下 大于100k 的文件    
find / -type f -size +100
# 查找根目录下 小于100k 的文件 
find / -type f -size -100

# 查找根目录下 大于100M 的文件    
find / -type f -size +100M
# 查找根目录下 小于100M 的文件 
find / -type f -size -100M

2.2.3 按照目录的层数查找

-maxdepth       -- 按照目录的层数查找

注意:该参数放在第一位,另外,find 命令默认为递归查找

# 查找根目录下(2层目录结构内)名为 "hosts" 的文件
find / -maxdepth 2 -type f  -name "hosts"

2.2.4 根据文件的权限信息进行查找

-perm       -- 根据文件的权限查找
find /etc -type f -maxdepth 3 -perm 644 

2.2.5 根据文件的时间信息进行查找


-mtime +7   -- 7天以前
-mtime  7   -- 第7天
-mtime -7   -- 最近7天

find / -type f -mtime +7 -name "xxxx"

案例:将30天以前的文件进行删除
find / -type f -mtime +30 --delete
find / -type f -mtime +30 -exec rm -rf {} \;
find / -type f -mtime +30 | xargs rm -rf

2.2.6 根据文件的 inode 号查找文件

-inum inode号码
find / -type f -inum inode号码

3. 常用基本语法案例

3.1 忽略大小写查询

参数 -i

[root@yunxuanedu tmp]# find /tmp -type f -name "admin.txt"
/tmp/admin.txt
[root@yunxuanedu tmp]# find /tmp -type f -name "Admin.txt"
/tmp/Admin.txt
[root@yunxuanedu tmp]# find /tmp -type f -iname "Admin.txt"
/tmp/admin.txt
/tmp/Admin.txt

3.2 按照文件的大小查找

参数 -size

[root@yunxuanedu tmp]# find /tmp -type f -size +1M
/tmp/services
[root@yunxuanedu tmp]# ll -h
total 2.0M
-rw-r--r-- 1 root root 655K Aug 13 19:07 admin.txt
-rw-r--r-- 1 root root  262 Aug 13 19:07 Admin.txt
-rw-r--r-- 1 root root 1.3M Aug 13 19:03 services

3.3 按照目录的层数查找

参数 maxdepth

[root@yunxuanedu tmp]# find / -maxdepth 2 -type f  -name "hosts"
/etc/hosts

3.4 按照文件的权限查找

find /data -maxdepth 2 -type f -perm 644

[root@yunxuanedu ~]# find /data -maxdepth 3 -type f -perm 644
/data/admin.jpg
/data/server/yunxuan04.png
/data/server/yunxuan02.png
/data/server/file/my.cnf
/data/server/yunxuan05.png
/data/server/yunxuan01.png
/data/server/yunxuan03.png
/data/test/yunxuan04.txt
/data/test/yunxuan03.txt
/data/test/yunxuan01.txt
/data/test/yunxuan02.txt
/data/test/yunxuan05.txt

3.5 根据文件时间信息进行查找

# 查找30天以前的文件
[root@yunxuanedu data]# find /etc -type f -mtime +30 -name "r*.conf"
/etc/resolv.conf
/etc/rsyncd.conf
/etc/rsyslog.conf

# 查找最近30天的文件
[root@templates data]# find /etc -type f -mtime -30 -name "r*.conf"
/etc/resolv.conf

# 查找距今第30天的文件
[root@templates data]# find /etc -type f -mtime 30 -name "r*.conf"

3.6 根据文件的 inode 信息查找

[root@templates scripts]# ln /etc/hosts /tmp/hosts
[root@templates scripts]# ll -i /etc/hosts
20971702 -rw-r--r--. 2 root root 158 Jun  7  2013 /etc/hosts
[root@templates scripts]# find / -type f -inum 20971702
/etc/hosts
/tmp/hosts

4. 配合其他命令使用案例

4.1 将查找出的文件进行删除

4.1.1 将/data目录 下以 .txt 结尾的文件找出,并删除

  • 方法一
find /data -type f -name "*.txt" -delete
  • 方法二
find /data -type f -name "*.txt" -exec rm -rf {} \;
  • 方法三
find /data -type f -name "*.txt"|xargs rm -fr
  • 方法四
rm -fr `find /tmp -maxdepth 1 -type f -perm 644 -name "*.txt"`

4.2 将查找出的文件进行复制或移动到某目录

find /data -type f -name "*.txt" -exec cp -a {} /tmp \;
find /data -type f -name "*.txt"|xargs cp -t /tmp
find /data -type f -name "*.txt"|xargs -i cp {} /tmp

find /data -type f -name "*.txt" -exec mv {} /tmp \;
find /data -type f -name "*.txt"|xargs mv -t /tmp
find /data -type f -name "*.txt"|xargs -i mv {} /tmp

4.3 将查找到的文件统一压缩

find /data -type f -mtime +7 -name "*.txt" | xargs tar zcvPf /tmp/data.tar.gz

5. 配合 find 命令使用 exec 和 xargs 命令的工作原理

5.1 exec

find /data -type f -name "*.txt" -exec rm -rf {} \;
image-20210819121132197.png

5.2 xargs

find /data -type f -name "*.txt"|xargs rm -rf
image-20210819122536070.png

5.3 扩展

5.3.1 扩展一

find /data -type f -name "*.txt"|xargs cp -t /tmp
image-20210819123327146.png

5.3.2 扩展二

find /data -type f -name "*.txt"|xargs -i cp {} /tmp
image-20210819123945102.png

相关文章

网友评论

      本文标题:find 命令使用详解

      本文链接:https://www.haomeiwen.com/subject/mcnobltx.html