linux 查找小于100k,Linux find常用用法

简介

find用于搜索文件或目录,功能非常强大。在搜索文件时,如果还要对搜索的文件进行后续的处理,一般都会结合xargs来实现。

find搜索是从磁盘进行指定目录开始扫描,而不是从数据库搜索。

语法

find [path...] [expression_list]

1.基础打印

find命令默认接的命令是 -print,默认以\n将找到的文件分割,如果想把\n分割符换成其他分隔符,可使用tr。

[email protected] /tmp % mkdir /tmp/test/a/ [0]

[email protected] /tmp % touch /tmp/test/a/{1..5}.c [0]

[email protected] /tmp % find /tmp/test/a/ [0]

/tmp/test/a/

/tmp/test/a/5.c

/tmp/test/a/1.c

/tmp/test/a/2.c

/tmp/test/a/3.c

/tmp/test/a/4.c

2.文件名或文件路径搜索

这里用到的是 -name(文件名)和-path(文件路径)。

-name可以对文件的basename进行匹配,-path是可以对文件的dirname+basename。查找时,文件名最好使用引号保卫,通常会配合通配符使用。

[email protected] /tmp % touch /tmp/test/a/other.c [130]

[email protected] /tmp % ls /tmp/test/a/ [0]

1.c 2.c 3.c 4.c 5.c other.c

[email protected] /tmp % find /tmp/test/a/ -name "*.c" [0]

/tmp/test/a/other.c

/tmp/test/a/5.c

/tmp/test/a/1.c

/tmp/test/a/2.c

/tmp/test/a/3.c

/tmp/test/a/4.c

[email protected] /tmp % find /tmp/test/a/ -name "[1-5].c" [0]

/tmp/test/a/5.c

/tmp/test/a/1.c

/tmp/test/a/2.c

/tmp/test/a/3.c

/tmp/test/a/4.c

注意,不能再-name的模式中使用"/",除非文件名中到了字符"/",否则匹配不到任何东西,-name只对basename进行匹配。

[email protected] test % find /tmp/test -name "*a*/*.c" [0]

find: warning: Unix filenames usually don‘t contain slashes (though pathnames do). That means that ‘-name ‘*a*/*.c’‘ will probably evaluate to false all the time on this system. You might find the ‘-wholename‘ test more useful, or perhaps ‘-samefile‘. Alternatively, if you are using GNU grep, you could use ‘find ... -print0 | grep -FzZ ‘*a*/*.c’‘.

故,如果我们要在指定目录下搜索某个子目录中的文件,要使用-path而非-name。

[email protected] test % find /tmp/test/ -path "*a*/*.c" [0]

/tmp/test/a/other.c

/tmp/test/a/5.c

/tmp/test/a/1.c

/tmp/test/a/2.c

/tmp/test/a/3.c

/tmp/test/a/4.c

3.根据文件类型搜索:-type

通常,如果我们知道要搜索文件的文件类型,我们可以使用-type进行搜索,一般需要搜索的文件有普通文件(f),目录(d)或链接文件(l)等。

例如:

搜索目录文件文件,且目录名字为a。

[email protected] test % find /tmp/test/ -type d -name "a" [0]

/tmp/test/a

搜索普通文件,且名字后缀为.c。

[email protected] test % find /tmp/test/a/ -type f -name "*.c" [0]

/tmp/test/a/other.c

/tmp/test/a/5.c

/tmp/test/a/1.c

/tmp/test/a/2.c

/tmp/test/a/3.c

/tmp/test/a/4.c

4.根据文件的三个时间进行修改

ps:不理解文件的三个时间,可以看这个链接https://www.cnblogs.com/ydqblogs/p/14300625.html

1)根据mtime进行搜索 -mtine

2)根据ctime进行搜索 -ctime

3)根据atime进行搜索 -atime

例如

搜索/tmp目录下一天内修改过内容的.c文件

#注意 -1表示一天内,+1表示大于一天

[email protected] a % find /tmp/test/a/ -type f -mtime -1 -name "*.c" [0]

/tmp/test/a/other.c

/tmp/test/a/5.c

/tmp/test/a/1.c

/tmp/test/a/2.c

/tmp/test/a/3.c

/tmp/test/a/4.c

5.根据文件大小搜索:-size

-size n[cwbkMG]

例如

搜索/tmp/test/a目录下,文件大小小于100k的后缀为.c的文件

[email protected] a % find /tmp/test/a/ -type f -size -10c -name "*.c" [0]

/tmp/test/a/other.c

/tmp/test/a/5.c

/tmp/test/a/2.c

/tmp/test/a/3.c

/tmp/test/a/4.c

6.搜索空文件

这里的空文件指没有内容的普通文件或者目录。

例如:

[email protected] a % mkdir empty [0]

[email protected] a % find /tmp/test/a/ -type d -empty [0]

/tmp/test/a/empty

[email protected] a % find /tmp/test/a/ -type f -empty [0]

/tmp/test/a/other.c

/tmp/test/a/5.c

/tmp/test/a/2.c

/tmp/test/a/3.c

/tmp/test/a/4.c

7.根据权限搜索:-perm

-perm可以根据文件的所有者对文件的可读可写可执行权限进行搜索。

[email protected] a % chmod 775 1.c [0]

[email protected] a % find ./ -perm -0775 [0]

./1.c

[email protected] a % find ./ -perm -0775 -name "*.c" [0]

./1.c

8.搜索到文件后删除该文件:-exec

例如

搜索到/tmp/test/a/目录下的后缀为.c的空文件,并删除。

[email protected] /tmp % ls -lh /tmp/test/a/ [0]

total 8.0K

-rwxrwxr-x 1 ydqun root 83 Jan 20 10:33 1.c

-rw-r--r-- 1 ydqun root 0 Jan 20 11:10 2.c

-rw-r--r-- 1 ydqun root 0 Jan 20 11:10 3.c

-rw-r--r-- 1 ydqun root 0 Jan 20 11:10 4.c

-rw-r--r-- 1 ydqun root 0 Jan 20 11:10 5.c

drwxr-xr-x 2 ydqun root 4.0K Jan 20 10:56 empty

-rw-r--r-- 1 ydqun root 0 Jan 20 11:10 empty.c

[email protected] /tmp % find /tmp/test/a/ -type f -empty -exec rm -r ‘{}‘ \; [0]

[email protected] /tmp % ls -lh /tmp/test/a/ [0]

total 8.0K

-rwxrwxr-x 1 ydqun root 83 Jan 20 10:33 1.c

drwxr-xr-x 2 ydqun root 4.0K Jan 20 10:56 empty

[email protected] /tmp %

原文:https://www.cnblogs.com/ydqblogs/p/14301518.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值