linux find命令的用法

   对find的功能不介绍了,以下列出一些我常用的find参数。

一、列出当前目录以及子目录下所有的文件和文件夹

   find base_path \\base_path可以是任何目录 -print 参数指明打印出匹配文件的文件名(路径)。使用-print时,'\n'作为用于分隔文件的分隔文件的定界符。

[root@wujc1 server]# find shell/ -print
shell/
shell/test6
shell/test8
shell/test7
shell/test5
shell/test3
shell/test2
shell/test1
shell/test9
shell/test4
shell/test10
[root@wujc1 server]#

##-print0 指明使用'\0'作为定界符来打印每一个匹配的文件名。

[root@wujc1 server]# find shell/ -print0
shell/shell/test6shell/test8shell/test7shell/test5shell/test3shell/test2shell/test1shell/test9shell/test4shell/test10[root@wujc1 server]#
[root@wujc1 server]#

1.1、根据文件名或正则表达式匹配搜索

find base_path -name "*.txt"  -print

[root@wujc1 shell]# ls
example.txt  EXAMPLE.txt  file.txt
[root@wujc1 shell]# find . -name "*.txt"
./EXAMPLE.txt
./example.txt
./file.txt
[root@wujc1 shell]#

find有一个参数-iname(忽略字母大小写),该选项作用和-name一样,但是匹配的时候会忽略大小写。

[root@wujc1 shell]# find . -iname "example*" -print
./EXAMPLE.txt
./example.txt
[root@wujc1 shell]#

如果想匹配多个条件中的一个,可以采用OR条件操作:

[root@wujc1 shell]# find . \( -name "*.txt" -o -name "*.pdf" \) -print
./EXAMPLE.txt
./new.txt
./example.txt
./file.txt
./text.pdf
[root@wujc1 shell]#

-path参数可以使用通配符来匹配文件路径或者文件。

[root@wujc1 shell]# ll
total 12
-rw-r--r--. 1 root root    0 Dec  9 01:51 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rw-r--r--. 1 root root    0 Dec  9 01:51 file.txt
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# pwd
/server/shell
[root@wujc1 shell]# find /server/shell -path  "*list1*" -print
/server/shell/file/list1
/server/shell/list/list1
[root@wujc1 shell]#

-regex选项和-path类似,不过-regex是基于正则表达式来匹配文件路径

我们用-regex选项来匹配.py或.sh文件

[root@wujc1 shell]# ll
total 12
-rw-r--r--. 1 root root    0 Dec  9 01:51 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rw-r--r--. 1 root root    0 Dec  9 01:51 file.txt
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# find . -regex ".*\(\.py\|\.sh\)$"
./new.sh
./raw_input.py
[root@wujc1 shell]#

类似的,-iregex 用于忽略正则表达式的大小写

[root@wujc1 shell]# ll
total 12
-rw-r--r--. 1 root root    0 Dec  9 01:51 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rw-r--r--. 1 root root    0 Dec  9 01:51 file.txt
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-r--r--. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# find . -iregex ".*\(\.py\|\.sh\)$"
./new.sh
./NEW.SH
./raw_input.py
[root@wujc1 shell]#

否定参数 "!"。

匹配任何不以.txt结尾的的文件名

[root@wujc1 shell]# ll
total 12
-rw-r--r--. 1 root root    0 Dec  9 01:51 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rw-r--r--. 1 root root    0 Dec  9 01:51 file.txt
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-r--r--. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# find . ! -name "*.txt" -print
.
./test
./test/test1
./new.sh
./some.jpg
./file
./file/file1
./file/list1
./list
./list/list1
./NEW.SH
./text.pdf
./raw_input.py
[root@wujc1 shell]#

基于目录深处的搜索

-maxdepth && -mindepth

限制命令遍历的深度,-maxdepth 1 只允许在当前目录,2即为向下2级,其他类推

[root@wujc1 server]# ll
total 4
drwxr-xr-x. 5 root root 4096 Dec  9 02:06 shell
[root@wujc1 server]# pwd;
/server
[root@wujc1 server]# find . -maxdepth 1 -type f -print
[root@wujc1 server]# find . -maxdepth 2 -type f -print
./shell/EXAMPLE.txt
./shell/new.sh
./shell/some.jpg
./shell/new.txt
./shell/example.txt
./shell/file.txt
./shell/NEW.SH
./shell/text.pdf
./shell/raw_input.py
[root@wujc1 server]#

反之-mindepth为最小遍历多少个目录

    ####提示,-maxdepth和-mindepth应该作为find的第3个参数出现,如果作为第4个或者之后的参数,就可能影响到find的效率,因为它不得不进行一些不必要的检查。

根据文件类型搜索

[root@wujc1 shell]# ll
total 12
-rw-r--r--. 1 root root    0 Dec  9 01:51 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rw-r--r--. 1 root root    0 Dec  9 01:51 file.txt
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-r--r--. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# find . -type d -print
.
./test
./file
./list
[root@wujc1 shell]# find . -type f
./EXAMPLE.txt
./test/test1
./new.sh
./some.jpg
./new.txt
./example.txt
./file.txt
./file/file1
./file/list1
./list/list1
./NEW.SH
./text.pdf
./raw_input.py
[root@wujc1 shell]#
文件类型类型参数
普通文件

f

符号链接

l

目录d
字符设备c
块设备b
套接字s
Fifo
p

根据文件时间进行搜索

linux/unix文件系统中每一个文件都有三种时间戳,如下所示。

访问时间(-atime):用户最近一次访问文件的时间。

修改时间(-mtime):文件内容最后一次被修改的时间 。  

变化时间(-ctime):文件元数据最后一次改变的时间。

---打印出最近七天内被访问过的所有文件:

[root@wujc1 shell]#  find . -type f -atime  -7 -print
./EXAMPLE.txt
./test/test1
./new.sh
./some.jpg
./new.txt
./example.txt
./file.txt
./file/file1
./file/list1
./list/list1
./NEW.SH
./text.pdf
./raw_input.py
[root@wujc1 shell]# ll
total 12
-rw-r--r--. 1 root root    0 Dec  9 01:51 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rw-r--r--. 1 root root    0 Dec  9 01:51 file.txt
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-r--r--. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]#

其他类似 ####这些作为find的时间参数而言,他们一般给出的是整数值,单位是天、这些数值通常还带有-或+:-代表小雨,+代表大于。 (即大于多少天,小于多少天)

另外:-amin -mmin -cmin也是可以作为find的参数,类似于-atime -mtime -ctime ,不过这些参数的单位是分钟。

小技巧:find有一个-newer参数。使用这个参数我们可以指定一个用于比较时间戳的参考文件,例如:

找出file.txt时间更新的所有文件。

[root@wujc1 shell]# ll
total 16
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rw-r--r--. 1 root root    0 Dec  9 01:51 file.txt
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-r--r--. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# find . -type f -newer file.txt -print
./test/test1
./new.sh
./some.jpg
./new.txt
./example.txt
./file/file1
./file/list1
./list/list1
./NEW.SH
./text.pdf
./raw_input.py
[root@wujc1 shell]#

有兴趣的可以看以下看下第8列的时间戳

基于文件大小的搜索:

   打印出小于2k的文件

[root@wujc1 shell]# ll
total 16
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rw-r--r--. 1 root root    0 Dec  9 01:51 file.txt
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-r--r--. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# du -sh *
4.0K    example.txt
0   EXAMPLE.txt
4.0K    file
0   file.txt
4.0K    list
0   new.sh
0   NEW.SH
0   new.txt
0   raw_input.py
0   some.jpg
4.0K    test
0   text.pdf
[root@wujc1 shell]# find . -type f -size -2k
./EXAMPLE.txt
./test/test1
./new.sh
./some.jpg
./new.txt
./example.txt
./file.txt
./file/file1
./file/list1
./list/list1
./NEW.SH
./text.pdf
./raw_input.py
[root@wujc1 shell]#

以下是一些单位参考:

b

c

字节
w字(2字节)
k千字节
M兆字节
GG字节


删除匹配的文件:

   -delete可以用来删除find查找到匹配文件

删除当前目录下的file.txt文件:

   

[root@wujc1 shell]# ll
total 16
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rw-r--r--. 1 root root    0 Dec  9 01:51 file.txt
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-r--r--. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# find . -type f -name "file.txt" -delete
[root@wujc1 shell]# ll
total 16
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-r--r--. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]#

基于文件所有权和文件权限的匹配

例如,列出权限是644的文件:

   使用-perm参数

[root@wujc1 shell]# ll
total 16
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-r--r--. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# find . -type f  -perm 644 -print
./EXAMPLE.txt
./test/test1
./new.sh
./some.jpg
./new.txt
./example.txt
./file/file1
./file/list1
./list/list1
./NEW.SH
./text.pdf
./raw_input.py
[root@wujc1 shell]#

用!可以查找权限不是644的文件,这在查找web中php文件时很有用:

[root@wujc1 shell]# ll
total 16
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-------. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# find . -type f  ! -perm 644 -print
./new.txt
[root@wujc1 shell]#


查找属于某个用户的文件:

使用-user参数:

[root@wujc1 shell]# find . -type f -user root  -print
./EXAMPLE.txt
./test/test1
./new.sh
./some.jpg
./new.txt
./example.txt
./file/file1
./file/list1
./list/list1
./NEW.SH
./text.pdf
./raw_input.py
[root@wujc1 shell]# ll
total 16
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-------. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]#

-exec参数

例如,找到new.txt文件以后把所有权更改为wujc用户:

[root@wujc1 shell]# ll
total 16
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 root root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-------. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# find . -type f -name "new.sh" -exec chown wujc {} \;
[root@wujc1 shell]# ll
total 16
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 wujc root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-------. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]#

-exec参数使用”{}“ 来遍历所有被找到的文件,等同于for i in list

我们可以使用-exec参数来读取文件的内容并写入某个文件,统计时可以使用:

[root@wujc1 shell]# ll
total 16
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 wujc root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-------. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# find . -type f -name "example.txt" -exec cat {} \;> find.log
[root@wujc1 shell]# ll
total 20
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rw-r--r--. 1 root root  500 Dec  9 02:45 find.log
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 wujc root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-------. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]#

小技巧:-exec无法使用多个命令,但是可以执行脚本,所以我们可以把多个命令写入到脚本中,通过-exec来执行,例如:

   

[root@wujc1 shell]# ll
total 24
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rwxr-xr-x. 1 root root   34 Dec  9 02:48 file.sh
-rw-r--r--. 1 root root  500 Dec  9 02:45 find.log
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 wujc root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-------. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
-rw-r--r--. 1 root root    0 Dec  9 01:55 text.pdf
[root@wujc1 shell]# find . -type f -name "text.pdf" -exec ./file.sh {} \;
[root@wujc1 shell]# ll
total 24
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rwxr-xr-x. 1 root root   34 Dec  9 02:48 file.sh
-rw-r--r--. 1 root root  500 Dec  9 02:45 find.log
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 wujc root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-------. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
[root@wujc1 shell]# ll /tmp/
total 0
-rw-r--r--. 1 root root 0 Dec  9 02:49 text.pdf
-rw-------. 1 root root 0 Dec  2 22:04 yum.log
[root@wujc1 shell]# cat file.sh
#!/bin/bash
\cp $1 /tmp
\rm -f $1
[root@wujc1 shell]#

这里使用方法即是-exec ./command.sh {} \

让find跳过指定的目录:

   用find搜索的时候,为了提升效率,略过一些不必要的被搜索的目录。这种用法主要体现在,该目录文件碎文件很多的情况下.

例如:

   

[root@wujc1 shell]# ll
total 24
-rw-r--r--. 1 root root  500 Dec  9 02:22 example.txt
-rw-r--r--. 1 root root    0 Dec  9 01:51 EXAMPLE.txt
drwxr-xr-x. 2 root root 4096 Dec  9 02:00 file
-rwxr-xr-x. 1 root root   34 Dec  9 02:48 file.sh
-rw-r--r--. 1 root root  500 Dec  9 02:45 find.log
drwxr-xr-x. 2 root root 4096 Dec  9 01:58 list
-rw-r--r--. 1 wujc root    0 Dec  9 02:03 new.sh
-rw-r--r--. 1 root root    0 Dec  9 02:06 NEW.SH
-rw-------. 1 root root    0 Dec  9 01:55 new.txt
-rw-r--r--. 1 root root    0 Dec  9 02:03 raw_input.py
-rw-r--r--. 1 root root    0 Dec  9 01:55 some.jpg
drwxr-xr-x. 2 root root 4096 Dec  9 01:59 test
[root@wujc1 shell]# ll file/ list/ test/
file/:
total 0
-rw-r--r--. 1 root root 0 Dec  9 01:59 file1
-rw-r--r--. 1 root root 0 Dec  9 02:00 list1
list/:
total 0
-rw-r--r--. 1 root root 0 Dec  9 01:58 list1
test/:
total 0
-rw-r--r--. 1 root root 0 Dec  9 01:59 test1
[root@wujc1 shell]# find /server/shell/ \( -name "list" -prune \) -o \( -type f -print \)
/server/shell/EXAMPLE.txt
/server/shell/test/test1
/server/shell/new.sh
/server/shell/find.log
/server/shell/some.jpg
/server/shell/new.txt
/server/shell/example.txt
/server/shell/file/file1
/server/shell/file/list1
/server/shell/NEW.SH
/server/shell/file.sh
/server/shell/raw_input.py
[root@wujc1 shell]#

这里的方法是,第一个\( -name "list" -prune \)是排除掉要查找的目录,第二个\( -type f -print \) 这里是要进行的动作,这句话的意思是,打印出除了list目录下以外的所有文件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章