首页 > 代码库 > find命令

find命令

--本文原创

命令格式

find  /path/to/somewhere/  - options [- print -exec -ok ...]

1   /path/to/somewhere/   是find命令所查找的路径,例如"."表示当前路径,可使用绝对或者相对路径

2   -print 表示将匹配到的文件输出到标准输出中,默认是当前屏幕,可重定向输出流

3  -exec 表示对find命令查找匹配到的文件执行exec给出的shell 命令,相应的命令形式为 ‘command‘ {} \; 注意{}和\;之间的空格

4  -ok 作用和-exec相同,只不过是一种更安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,会给出提示,让用户来确定是否执行

查找模式

1 -name 按文件名查找

2 -perm 按照文件权限查找

3 -prune 不在当前指定的目录查找,如果同时使用-dept那么-prune将被忽略

4 -user 按文件属主查找

5 -group 按文件属组查找

6 -mtime -n +n 按文件更改时间查找,-n 表示从此刻算起,文件的更改时间是在n天以内,+n表示文件的更改时间在n天之前

7 -ctime 文件改变时间change time 选项-n,+n意义如上

8 -nogroup 查找无有效属组的文件,即该文件所属的组在/etc/groups中不存在

9 -nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在

10 -newer file1 file2 查找更改时间比file1新但比文件file2旧的文件,即file1<查找目标<file2

11 -type 按文件类型查找  b , d , c , p , l , f, s

12 -size n 表示查找大小为n的文件 带c表示大小以字节计算

13 -depth 表示查找文件时,首先查找当前目录中的文件,然后在其子目录中查找

14 -fstype 表示查找位于某一类文件系统中的文件,这些文件系统通常可以在配置文件/etc/fstab中找到

15 -mount 表示查找文件时不跨越文件系统mount点

16  -follow 表示如果find命令遇到符号链接文件,就跟踪至链接所指向的文件

17 -cpio 表示对匹配的文件使用cpio命令,将这些文件备份到磁带设备中

 

一般用-exec或者-ok来执行shell 命令,任何形式的命令都可以在-exec中使用

实例

列出当前目录所有普通文件

[root@localhost ~]# find . -type f -exec ls -l {} \;
-rw-r--r--. 1 root root 18 12月 29 2013 ./.bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 ./.bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 ./.bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 ./.cshrc
-rw-r--r--. 1 root root 129 12月 29 2013 ./.tcshrc
-rw-------. 1 root root 1382 11月 13 20:14 ./anaconda-ks.cfg
-rw-------. 1 root root 2 11月 13 12:15 ./.cache/dconf/user
-rw-------. 1 root root 11 5月  13 13:03 ./.cache/abrt/lastnotification
-rw-r--r--. 1 root root 463 11月 13 12:15 ./.dbus/session-bus/9ef6301b72a14a6e8a38e7e00cf69136-9
-rw-r--r--. 1 root root 1433 11月 13 12:15 ./initial-setup-ks.cfg
-rw-------. 1 root root 3212 5月  13 12:40 ./.bash_history
-rw-------. 1 root root 66 11月 13 14:24 ./.xauth3HNIPU
-rw-r--r--. 1 root root 397 5月  10 16:58 ./.ssh/known_hosts
-rw-------. 1 root root 408 5月  10 17:02 ./.ssh/authorized_keys
-rw-r--r--. 1 root root 408 5月  10 17:19 ./.ssh/my_rsa.pub
-rw-------. 1 root root 1675 5月  10 17:19 ./.ssh/id_rsa
-rw-------. 1 root root 4123 5月  13 12:16 ./.viminfo
-rw-r--r--. 1 root root 16 5月  13 12:46 ./test

查找passwd文件中某个用户是否存在

[root@localhost ~]# find /etc/ -name passwd -exec grep gandefeng {} \;
gandefeng:x:1002:1002::/home/gandefeng:/bin/bash

查找当前用户家目录中所有的文件,包含隐藏文件

 

[gandefeng@localhost ~]$ find ~ -print
/home/gandefeng
/home/gandefeng/.mozilla
/home/gandefeng/.mozilla/extensions
/home/gandefeng/.mozilla/plugins
/home/gandefeng/.bash_logout
/home/gandefeng/.bash_profile
/home/gandefeng/.bashrc
/home/gandefeng/.cache
/home/gandefeng/.cache/abrt
/home/gandefeng/.cache/abrt/lastnotification
/home/gandefeng/.config
/home/gandefeng/.config/abrt
/home/gandefeng/test
/home/gandefeng/.viminfo
/home/gandefeng/.bash_history

 

 

xargs对find的配合使用

    在find和-exec 处理匹配文件时,find命令将所匹配到的文件一起传递给exec执行,但是有的系统传递给exec的命令长度是有限制的,find命令运行几分钟后会溢出错误,通常是参数太长或参数列溢出,所以,find把匹配到的文件传递给xargs 而xargs每次只获取一部分而不是全部,分批次处理,有的系统在使用-exec会为每一个匹配到的文件发起一个相应的进程,并非将所匹配到的文件全部作为参数一次执行,这样会出现过多的进程,系统性能下降,而xargs只有一个进程,而且还可以根据参数来调整xargs接受的量

 

例:查找当前目录中每一个普通文件,然后测试属于的文件类型

[gandefeng@localhost ~]$ find . -type f |xargs file
./.bash_logout:                 ASCII text
./.bash_profile:                ASCII text
./.bashrc:                      ASCII text
./.cache/abrt/lastnotification: ASCII text
./test:                         ASCII text
./.viminfo:                     UTF-8 Unicode text
./.bash_history:                ASCII text


 

to be continued..

 

find命令