Find 命令
-
查找指定文件名的文件(不区分大小写):
find -iname "MyPro.c" -
对找到的文件执行某个命令
find -iname "mypro.C -exec md5sum {} " -
查找home目录下的空文件
find ~ -empty -
再/usr 目录下找出超过10MB的文件
find /usr -type f -size +10240kdu 命令也可做到
du -a /var | sort -n -r | head -n 10
du -hsx * | sort -rh | head -10 -
在 /var 目录下找出 90 天之内未被访问过的文件?
find /var ! -atime -90 -
如何在 /home 目录下找出 120 天之前被修改过的文件?
find /home -mtime +120 -
在整个目录树下查找文件 “core” ,如发现则无需提示直接删除它们?
*find / -name core rm {} * -
基于正则表达式匹配文件路径
find . -regex ".*(.txt|.pdf)$" -
向下最大深度限制为3
find . -maxdepth 3 -type f -
找出当前目录下所有root的文件,并把所有权更改为用户tom
find .-type f -user root -exec chown tom {} ;
上例中,{} 用于与-exec选项结合使用来匹配所有文件,然后会被替换为相应的文件名。
- 找出自己家目录下所有的.txt文件并删除
find $HOME/. -name “*.txt” -ok rm {} ;
上例中,-ok和-exec行为一样,不过它会给出提示,是否执行相应的操作。
-
查找当前目录下所有.txt文件并把他们拼接起来写入到all.txt文件中
find . -type f -name “*.txt” -exec cat {} ;> all.txt -
将30天前的.log文件移动到old目录中
find . -type f -mtime +30 -name “*.log” -exec cp {} old ; -
找出当前目录下所有.txt文件并以“File:文件名”的形式打印出来
find . -type f -name “*.txt” -exec printf “File: %s\n” {} ;
因为单行命令中-exec参数中无法使用多个命令,以下方法可以实现在-exec之后接受多条命令
-exec ./text.sh {} ; -
搜索但跳出指定的目录
查找当前目录或者子目录下所有.txt文件,但是跳过子目录sk
find . -path “./sk” -prune -o -name “*.txt” -print