find linux 目录深度_linux find常用命令示例用法汇总找大小文件、目录

列出当前目录和子目录下的所有文件

这个命令会列出当前目录以及子目录下的所有文件。

[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 命令中的模式串。在下面的查找中,我们用另一种语法来代替数字。

[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命令结合,从而找出最大或最小的文件。

下面的命令使用了 sort 命令的 -r 选项,也就是从大到小降序排列。经过 head 命令的过滤之后,会显示当前目录和子目录下最大的5个文件。命令的执行过程需要一段时间,查找的速度取决于文件的总数。

[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日以前的文件并删除它们,在删除之前先给出提示

[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模糊查找一个文件

find [查找范围] | grep [查找的关键字]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值