侧边栏壁纸
  • 累计撰写 7 篇文章
  • 累计创建 0 个标签
  • 累计收到 0 条评论

linux中find命令的使用例子

林凡晨
2022-03-10 / 0 评论 / 0 点赞 / 586 阅读 / 1,136 字
温馨提示:
本文最后更新于 2022-03-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1. 在当前目录中使用名称查找文件

查找名称为test.txt的所有文件

find . -name test.txt

2.使用名称和忽略大小写查找文件

查找名称为test.txt的所有文件,并包含大写和小写字母

find /home -iname test.txt

3.查找目录中的所有 PHP 文件

找到所有php

find . -type f -name "*.php"

4.查找具有 777 权限的文件

find . -type f -perm 0777 -print

5.查找没有 777 权限的文件

find / -type f ! -perm 777

6.查找可执行文件

find / -perm /a=x

7.查找权限为 777的文件 并 chmod 设置为 644

find / -type f -perm 0777 -print -exec chmod 644 {} \;

8.查找和删除单个文件

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

9.查找某个路径下的所有空文件。

find /tmp -type f -empty

10.查找所有隐藏文件

find /tmp -type f -name ".*"

11.根据用户查找单个文件

find / -user root -name test.txt

12.根据组查找所有文件

find /home -group root

13.删除10天前对应的sql文件

find /opt -mtime +10 -name "*.sql" -exec rm -rf {} \;
-mmin +10 是十分钟前 -10十分钟内

14. 查找过去 1 小时内更改过的文件

find / -cmin -60
find / -mmin -60 #最近一小时
find / -amin -60 #过去小时内访问的文件

15. 查找 50MB – 100MB 之间的大小

find / -size +50M -size -100M

16.查找和删除 100MB 文件

find / -type f -size +100M -exec rm -f {} \;

17.查找特定文件并且根据大小删除

find / -type f -name *.mp3 -size +10M -exec rm {} \;

18.使用find查找大文件

find  /root/ -mount -type f  -size +100M 2>/dev/null  |xargs -n1 -i  du -sh {} | sort -hr #按GB排序

find  /root -mount -type f -size +100M 2>/dev/null  |xargs -n1 -i  du -sm {} | sort -rn #按MB排序

19.删除特殊字符的文件

一些无法使用rm删除的文件,可以使用此方法

1) 使用 ls -i 查出该文件的 inode 号,假设为654321
2) 使用find命令删除 
find ./ -inum 654321 -exec rm '{}' \;		
0

评论区