
这个命令会列出当前目录以及子目录下的所有文件。
[root@localhost ~]# find . ./abc.txt ./subdir ./subdir/how.php ./cool.php
查找特殊的目录或路径
下面的命令会查找当前目录下 test 文件夹中的文件,默认列出所有文件。
[root@localhost ~]# find ./test ./test ./test/abc.txt ./test/subdir ./test/subdir/how.php ./test/cool.php
下面的命令用于查找指定名称的文件。
[root@localhost ~]# find ./test -name "abc.txt" ./test/abc.txt
也可以使用通配符
[root@localhost ~]# find ./test -name "*.php" ./test/subdir/how.php ./test/cool.php
在查找文件名时,忽略大小写往往非常有用。要忽略大小写,只需要使用 iname 选项,而不是 name 选项。
[root@localhost ~]# find ./test -iname "*.Php" ./test/subdir/how.php ./test/cool.php
限制目录查找的深度
[root@localhost ~]# find ./test -maxdepth 2 -name "*.php" ./test/subdir/how.php ./test/cool.php [root@localhost ~]# find ./test -maxdepth 1 -name *.php ./test/cool.php
例中指定了 maxdepth 为1,表明最多只查找一层内的子目录,也就是只查找当前文件夹。
反向查找
[root@localhost ~]# find ./test -not -name "*.php" ./test ./test/abc.txt ./test/subdir
在上面的示例中我们找到了所有扩展名不是 php 的文件和文件夹。我们也可以使用感叹号 ! 来代替 -not。
[root@localhost ~]# find ./test ! -name "*.php"
结合多个查找条件
可以同时使用多个查找条件来指定文件名并排除某些文件。
[root@localhost ~]# find ./test -name 'abc*' ! -name '*.php' ./test/abc.txt ./test/abc

上面的命令查找所有以 abc 开头并且不含 .php 扩展名的文件。这个示例展现了 find 命令自带的查找表达式是多么的强大。
如果我们需要进行基于 OR 运算的查找时,可以加上 -o 开关。
[root@localhost ~]# find -name '*.php' -o -name '*.txt' ./abc.txt ./subdir/how.php ./abc.php ./cool.php
只查找文件或目录
[root@localhost ~]# find ./test -name abc* ./test/abc.txt ./test/abc
只查找文件
[root@localhost ~]# find ./test -type f -name "abc*" ./test/abc.txt
只查找目录
[root@localhost ~]# find ./test -type d -name "abc*" ./test/abc
同时在多个目录下查找
[root@localhost ~]# find ./test ./dir2 -type f -name "abc*" ./test/abc.txt ./dir2/abcdefg.txt
查找隐藏文件
[root@localhost ~]# find ~ -type f -name ".*"
查找指定权限的文件
查找了所有具有 0664 权限的文件
[root@localhost ~]# find . -type f -perm 0664 ./abc.txt ./subdir/how.php ./abc.php ./cool.php
可以结合 反向查找 来进行权限检查。
[root@localhost ~]# find . -type f ! -perm 0777 ./abc.txt ./subdir/how.php ./abc.php ./cool.php
查找具有 SGID/SUID 属性的文件
下面的命令查找所有具有 644 权限和 SGID 属性的文件。
[root@localhost ~]# find / -perm 2644
可以使用 1664 来查找设置了 粘滞位 (sticky bit)的文件。

[root@localhost ~]# find / -perm 1644
perm 选项除了接受数值型参数外,同样接受 chmod 命令中的模式串。说明:本调用将参数string传递给一个命令解释器(一般为sh)执行, 即string被解释为一条命令, 由sh执行该命令.若参数string为一个空指针则为检查命令解释器是否存在. 该命令可以同命令行命令相同形式, 但由于命令做为一个参数放在系统调用中, 应注意编译时对特殊意义字符的处理. 命令的查找是按path环境变量的定义的. 命令所生成的后果一般不会对父进程造成影响.。如果勾中”use as a prompt” 选项,则该参数接受传入的参数值,”default value expression” 中的值作为默认值使用linux find 时间,当没有传入该参数值的时候,即使用此值。
[root@localhost ~]# find / -maxdepth 2 -perm /u=s 2>/dev/null /bin/mount /bin/su /bin/ping6 /bin/fusermount /bin/ping /bin/umount /sbin/mount.ecryptfs_private
注意:由于权限不足,某些目录会拒接访问。命令中的 2>/dev/null 正是用于清除输出中的错误访问结果。
查找只读文件
[root@localhost ~]# find /etc -maxdepth 1 -perm /u=r /etc /etc/thunderbird /etc/brltty /etc/dkms /etc/phpmyadmin ... output truncated ...
查找可执行文件
[root@localhost ~]# find /bin -maxdepth 2 -perm /a=x /bin /bin/preseed_command /bin/mount /bin/zfgrep /bin/tempfile ... output truncated ...
查找属于特定用户的文件
查找当前目录下,属于 bob 的文件。
[root@localhost ~]# find . -user bob . ./abc.txt ./abc ./subdir ./subdir/how.php ./abc.php
在指定所属用户的同时,我们同样可以指定文件名。
[root@localhost ~]# find . -user bob -name '*.php'
查找属于特定用户组的文件
[root@localhost ~]# find /var/www -group developer
基于日期和时间的查找
查找过去的第 N 天被修改过的文件
[root@localhost ~]# find / -mtime 50
查找过去的 N 天内被访问过的文件
[root@localhost ~]# find / -atime -50
查找某段时间范围内被修改过内容的文件
[root@localhost ~]# find / -mtime +50 -mtime -100

查找过去的 N 分钟内状态发生改变的文件
[root@localhost ~]# find /home/bob -cmin -60
查找过去的 1 小时内被修改过内容的文件
[root@localhost ~]# find / -mmin -60
查找过去的 1 小时内被访问过的文件
[root@localhost ~]# find / -amin -60
查找指定大小的文件
[root@localhost ~]# find / -size 50M
查找大小在一定范围内的文件
[root@localhost ~]# find / -size +50M -size -100M
查找最大和最小的文件
我们可以将 find 命令与 ls 和 sort命令结合,从而找出最大或最小的文件。
查找目录并列出目录下的文件(为找到的每一个目录单独执行ls命令,没有选项-print时文件列表前一行不会显示目录名称)。-prune 使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。
[root@localhost ~]# find . -type f -exec ls -s {} \; | sort -n -r | head 5
同样,我们可以去掉 sort 命令的 -r 选项来进行升序排列,从而显示出最小的5个文件。
[root@localhost ~]# find . -type f -exec ls -s {} \; | sort -n | head 5
查找空文件和空目录
查找空文件:
[root@localhost ~]# find /tmp -type f -empty
查找空目录:
[root@localhost ~]# find ~/ -type d -empty
使用 ls 命令列出文件信息

[root@localhost ~]# find . -exec ls -ld {} \; drwxrwxr-x 4 enlightened enlightened 4096 Aug 11 19:01 . -rw-rw-r-- 1 enlightened enlightened 0 Aug 11 16:25 ./abc.txt drwxrwxr-x 2 enlightened enlightened 4096 Aug 11 16:48 ./abc drwxrwxr-x 2 enlightened enlightened 4096 Aug 11 16:26 ./subdir -rw-rw-r-- 1 enlightened enlightened 0 Aug 11 16:26 ./subdir/how.php -rw-rw-r-- 1 enlightened enlightened 29 Aug 11 19:13 ./abc.php -rw-rw-r-- 1 enlightened enlightened 0 Aug 11 16:25 ./cool.php
删除找到的文件
下面的命令会删除 tmp 目录下扩展名为 .txt 的文件。
[root@localhost ~]# find /tmp -type f -name "*.txt" -exec rm -f {} \;
我们同样可以删除目录,只要把 -type 后面的 f 改为 d ,并且在 rm 命令后面加上 -r 即可。
[root@localhost ~]# find /tmp -type d -name "dirToRemove" -exec rm -r -f {} \;
查找当前所有目录并排序
[root@localhost ~]# find . -type d | sort
在目录中查找更改时间在n日以前的文件并删除它们
[root@localhost ~]# find . -type f -mtime +14 -exec rm {} \;
在目录中查找更改时间在n日以前的文件并删除它们linux find 时间,在删除之前先给出提示
[root@localhost ~]# find . -name "*.log" -mtime +5 -ok rm {} \;
-exec中使用grep命令
[root@localhost ~]# find /etc -name "passwd*" -exec grep "root" {} \;
查找文件移动到指定目录
[root@localhost ~]# find . -name "*.log" -exec mv {} .. \;
用exec选项执行cp命令
[root@localhost ~]# find . -name "*.log" -exec cp {} test3 \;
将/usr/local/backups目录下所有10天前带"."的文件删除
[root@localhost ~]# find /usr/local/backups -mtime +10 -name "*.*" -exec rm -rf {} \;
-exec:固定写法
rm -rf:强制删除文件,包括目录
{} \; :固定写法,一对大括号+空格+\
find $1 -name "*.html" -mtime +1 -print0 |xargs -0 rm -v
linux模糊查找一个文件
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-118151-1.html
去投资损失的更多
他说的不是指人类
中国品牌严重低估