这是 Linux find 命令备忘清单的快速参考列表,包含常用选项和示例。
入门
用法
1 | find [path...] [options] [expression] |
通配符
1 | find . -name "*.txt" |
- Find 备忘清单 (gist.github.com)
参数示例
参数 | 示例 | 描述 |
---|---|---|
-type |
find . -type d | 仅查找目录 |
-name |
find . -type f -name “*.txt” | 按名称查找文件 |
-iname |
find . -type f -iname “hello” | 按名称查找文件(不区分大小写) |
-size |
find . -size +1G | 查找大于 1G 的文件 |
-user |
find . -type d -user jack | 查找杰克的文件 |
-regex |
find /var -regex ‘.*/tmp/.*[0-9]*.file’ | 将正则表达式与查找一起使用 |
-maxdepth |
find . -maxdepth 1 -name “a.txt” | 在当前目录和子目录中 |
-mindepth |
find / -mindepth 3 -maxdepth 5 -name pass | 在子目录级别 2 和 4 之间 |
类型
-type d |
目录 |
-type f |
文件 |
-type l |
符号链接 |
-type b |
缓冲块 |
-type c |
无缓冲字符 |
-type p |
命名管道 |
-type s |
插座 |
大小
-size b |
512 字节块(默认) |
-size c |
字节 |
-size k |
千字节 |
-size M |
兆字节 |
-size G |
千兆字节 |
-size T |
太字节_(仅限 BSD)_ |
-size P |
PB (仅 BSD) |
大小 +/-
查找所有大于 10MB 的文件
1 | find / -size +10M |
查找所有小于 10MB 的文件
1 | find / -size -10M |
查找 100MB 和 1GB 之间的大小
1 | find / -size +100M -size -1G |
像往常一样,+
和 -
前缀表示大于和小于。
名称
在当前目录中使用名称查找文件
1 | find . -name tecmint.txt |
查找主目录下的文件
1 | find /home -name tecmint.txt |
使用名称查找文件并忽略大小写
1 | find /home -iname tecmint.txt |
使用名称查找目录
1 | find / -type d -name tecmint |
使用名称查找php文件
1 | find . -type f -name tecmint.php |
查找目录下的所有php文件
1 | find . -type f -name "*.php" |
权限
查找权限为 777 的文件。
1 | find . -type f -perm 0777 -print |
查找未经许可的文件 777.
1 | find / -type f ! -perm 777 |
查找 SUID 集文件。
1 | find / -perm /u=s |
查找 SGID 集文件。
1 | find / -perm /g=s |
查找只读文件。
1 | find / -perm /u=r |
查找可执行文件。
1 | find / -perm /a=x |
所有者和组
根据用户查找单个文件
1 | find / -user root -name tecmint.txt |
根据用户查找所有文件
1 | find /home -user tecmint |
根据组查找所有文件
1 | find /home -group developer |
查找用户的特定文件
1 | find /home -user tecmint -iname "*.txt" |
多个文件名
1 | find . -type f \( -name "*.sh" -or -name "*.txt" \) |
查找带有 .sh
或 .txt
扩展名的文件
多个目录
1 | find /opt /usr /var -name foo.scala -type f |
查找具有多个目录的文件
空的
1 | find . -type d -empty |
删除目录中的所有空文件
1 | find . -type f -empty -delete |
查找日期和时间
方法
Option | Description |
---|---|
atime |
访问时间(上次文件 |
mtime |
修改时间(上次文件 |
ctime |
更改时间(上次文件 |
示例
Option | Description |
---|---|
-mtime +0 |
24 小时前修改 |
-mtime 0 |
从现在到 1 天前修改 |
-mtime -1 |
不到 1 天前修改(与 -mtime 0 相同) |
-mtime 1 |
24 至 48 小时前修改 |
-mtime +1 |
超过 48 小时前修改 |
-mtime +1w |
上次修改时间超过 1 周前 |
-atime 0 |
从现在到 24 小时前最后一次访问 |
-atime +0 |
访问时间超过 24 小时 |
-atime 1 |
在 24 至 48 小时前访问 |
-atime +1 |
访问时间超过 48 小时 |
-atime -1 |
不到 24 小时前访问过(与 -atime 0 相同) |
-ctime -6h30m |
文件状态在过去 6 小时 30 分钟内发生变化 |
更多示例
查找最近 50 天修改的文件
1 | find / -mtime 50 |
查找最近 50 天访问的文件
1 | find / -atime 50 |
查找最近 50-100 天修改的文件
1 | find / -mtime +50 –mtime -100 |
查找最近 1 小时内更改的文件
1 | find / -cmin -60 |
查找最近 1 小时内修改过的文件
1 | find / -mmin -60 |
查找最近 1 小时内访问过的文件
1 | find / -amin -60 |
查找并
查找和删除
查找并删除多个文件
1 | find . -type f -name "*.mp3" -exec rm -f {} \; |
查找和删除单个文件
1 | find . -type f -name "tecmint.txt" -exec rm -f {} \; |
查找和删除 100mb 文件
1 | find / -type f -size +100m -exec rm -f {} \; |
查找特定文件并删除
1 | find / -type f -name *.mp3 -size +10m -exec rm {} \; |
查找和替换
1 | find ./ -type f -exec sed -i 's/find/replace/g' {} \; |
参见:sed 命令
查找和重命名
1 | find . -type f -name 'file*' -exec mv {} {}_renamed \; |
查找和移动
1 | find . -name '*.mp3' -exec mv {} /tmp/music \; |
查找并将其移动到特定目录
查找和复制
1 | find . -name '*2020*.xml' -exec cp -r "{}" /tmp/backup \; |
查找并将其复制到特定目录
查找并连接
1 | find download -type f -iname '*.csv' | xargs cat > merged.csv |
查找和排序
1 | find . -printf "%T+\t%p\n" | sort |
查找和 chmod
查找文件并将权限设置为 644。
1 | find / -type f -perm 0777 -print -exec chmod 644 {} \; |
查找目录并将权限设置为 755。
1 | find / -type d -perm 777 -print -exec chmod 755 {} \; |
查找并 tar
1 | find . -type f -name "*.java" | xargs tar cvf myfile.tar |
查找并排除目录
查找当前目录及子目录中的所有js
文件,但是排除掉node_modules
目录中的
1 | find . -type f -name "*.js" -not -path "./node_modules/*" |
评论