linux find 详细说明,以及实例

张映 发表于 2010-06-15

分类目录: linux

标签:, , ,

一,遇到find之前经常用的查找命令

which,whereis来查找安装的程序文件

[zhangy@BlackGhost ~]$ whereis php
php: /usr/bin/php /etc/php /usr/lib/php /usr/include/php /usr/local/php /usr/share/man/man1/php.1.gz
[zhangy@BlackGhost ~]$ which php
/usr/bin/php

locate来查找文件

[zhangy@BlackGhost ~]$ locate Test.php
/home/zhangy/phpMyAdmin/test/FailTest.php
/home/zhangy/phpbak2010/php/lib/php/PEAR/RunTest.php

说明:用which和whereis来找安装程序还是挺方便的,但是用locate来找文件就不方便了,感觉不能得心应手,不能满足我的所有要求,感觉不像是用来查找文件用的。

二,find命令介绍

在介绍命令前,我先说一下,我学linux命令的方法,当我发现一个冒似很有用的命令时,我会去查看一下linux的命令手册,这一点我觉得很重要,因为这里是最权威的了。不过都是英文的。命令如下:

[zhangy@BlackGhost ~]$ man find
你会看到很多内容来告诉你什么是find,怎么用,带什么参数,还有很多例子给你参考,只要你根着例子一步一步学的就可以了,有人会问英语不行怎么办,我英语也一般,网上有太多的在线翻译,google翻译就不错,当然这种翻译很傻的,不过根据翻译和自己猜测,还是能知道是什么意思,在结合自己的实践,我相信这个命令你很快能掌握。
find命令的一般形式为:

find 路径 -命令参数 [输出形式]
参数说明:
路径:告诉find在哪儿去找你要的东西,
命令参数:参数很多下面会说到
输出形式:输出形式很多,-print,-printf,-print0,-exec,-ok,-ls反正很多自己看手册吧。
说一下exec,
-exec find命令对匹配的文件执行该参数所给出的其他linux命令。相应命令的形式为' 命令 - and' {} \;,注意{ }和\;之间的空格。
-ok 和- exec的作用相同,只不过和会人交互而已,OK执行前会向你确认是不是要执行。

find命令主要参数:

-name 按照文件名查找文件。
-perm 按照文件权限来查找文件。
-prune 使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用了- depth选项,那么-prune选项将被find命令忽略。
-user 按照文件属主来查找文件。
-group 按照文件所属的组来查找文件。
-mtime -n +n 按照文件的更改时间来查找文件, -n表示文件更改时间距现在n天以内,+n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime选项,但它们都和-mtime选项
相似,所以我们在这里只介绍-mtime选项。
-nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
-nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2 查找更改时间比文件file1新但比文件file2旧的文件。
-type 查找某一类型的文件,诸如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
s - socket文件
-size n[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
-depth 在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-fstype 查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件
/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
-mount 在查找文件时不跨越文件系统mount点。
-follow 如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio 对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。

三,find命令实例

1,通过名字来查找

#~代表的是$home目录,查找名子为memcache.pid的文件
[zhangy@BlackGhost ~]$ find ~ -name memcached.pid -print
/home/zhangy/memcached/memcached.pid

#.代表当前目录,查找所有以pid结尾的文件
[zhangy@BlackGhost ~]$ find . -name "*.pid" -print
./memcached/memcached.pid
./.tencent/qq/95219454.pid

#查找以数字开头的所有pid文件,在这里要说[0-9]*不能匹配23,它根一般的语言类正则不太一样,shell里面的*可以代表一切字符(单个,多个都行),如果想匹配2345只能这样写[0-9][0-9][0-9][0-9]
[zhangy@BlackGhost ~]$ find ~ -name "[0-9]*.pid" -print
/home/zhangy/.tencent/qq/95219454.pid

#在.tencent文件下面找pid文件
[zhangy@BlackGhost ~]$ find /home/zhangy/.tencent/ -name "[0-9]*.pid" -print
/home/zhangy/.tencent/qq/95219454.pid

2,通过文件权限来查找

#~代表的是$home目录,查找权限为755的文件
[zhangy@BlackGhost css]$ find ~ -perm 755 -print |more
/home/zhangy/www/css2/c_textshadow.html
/home/zhangy/www/css2/c_textautospace.html

#.代表当前目录,查找所有以产u_开头的,并且权限为700的文件
[zhangy@BlackGhost css]$ find . -perm 700 -name "u_*" -print |more
./css2/u_length_cm.html
./css2/u_length_px.html

3,prune来忽略目录来查找

#查找在前目录中,不在以aaa结尾的目录中的,不以gz结尾的文件

[zhangy@BlackGhost download]$ find . -name "*.gz"  -prune -o \( \! -name aaa \)  -print
.
./eaccelerator-0.9.5.3.tar
./fix-crash-in-excerpts.patch
./AddFeed_Widget_WordPress_Plugin.zip
./jQuery china-addthis plugin 1.07.rar

#下面的例子,是find手册里面的

cd /source-dir
find . -name .snapshot -prune -o \( \! -name *~ -print0 \)|
cpio -pmd0 /dest-dir

This command copies the contents of /source-dir to /dest-dir, but omits f
iles and directories  named  .snapshot  (and  anything  in
them).  It also omits files or directories whose name ends in ~, but not their contents.  The construct -prune -o \( ... -print0 \)
is quite common.  The idea here is that the expression before -prune matches things which are to be pruned.   However,  the  -prune
action  itself  returns  true,  so  the following -o ensures that the right hand side is evaluated only for those directories which
didn't get pruned (the contents of the pruned directories are not even visited, so their contents are irrelevant).  The  expression
on  the  right  hand side of the -o is in parentheses only for clarity.  It emphasises that the -print0 action takes place only for
things that didn't have -prune applied to them.  Because the default `and' condition between tests binds more tightly than -o, this
is the default anyway, but the parentheses help to show what is going on.

4,根据文件类型来查找文件

#查找当前目录下面的目录
[zhangy@BlackGhost download]$ find . -type d -print
.
./ddd

#查找当前目录下面的非目录文件,请注意空格
[zhangy@BlackGhost download]$ find . !-type d -print
bash: !-type: event not found

#查找当前目录下面的非目录文件
[zhangy@BlackGhost download]$ find . ! -type d -print
./eaccelerator-0.9.5.3.tar
./haproxy-1.3.15.7.tar.gz
./fix-crash-in-excerpts.patch

5,根据文件所属用户和用户组来找文件

#查找当前目录中,没有归属的文件
[zhangy@BlackGhost download]$ find . -nouser -print

#查找/home/zhangy/download目录中,用户组为users,所属用户为zhangy的文件
[zhangy@BlackGhost download]$ find /home/zhangy/download -user zhangy -group users -print
/home/zhangy/download
/home/zhangy/download/eaccelerator-0.9.5.3.tar
/home/zhangy/download/haproxy-1.3.15.7.tar.gz
/home/zhangy/download/fix-crash-in-excerpts.patch
/home/zhangy/download/memcached-1.4.0.tar.gz
/home/zhangy/download/mmseg-0.7.3.tar.gz
/home/zhangy/download/mysql-proxy-0.6.0-linux-rhas4-x86.tar.gz
/home/zhangy/download/memcachephp.zip
/home/zhangy/download/sphinx-0.9.8-rc2.tar.gz

6,根文件大小来查找

#查找/home/zhangy/download目录中,文件大小大于1000000字符的文件,注意+号表示大于
[zhangy@BlackGhost download]$ find /home/zhangy/download -size +1000000c -print
/home/zhangy/download/eaccelerator-0.9.5.3.tar
/home/zhangy/download/mmseg-0.7.3.tar.gz
/home/zhangy/download/pcre-7.9.tar.gz
/home/zhangy/download/squid-3.1.4.tar.gz
/home/zhangy/download/squid-3.0.STABLE7.tar.gz

#查找/home/zhangy/download目录中,文件大小小于10块的文件,注意-号表示小于,一块等于512b
[zhangy@BlackGhost download]$ find /home/zhangy/download -size -10 -print
/home/zhangy/download
/home/zhangy/download/fix-crash-in-excerpts.patch
/home/zhangy/download/test.sql.zip

7,根文件的修改时间来查找

#查找/home/zhangy下,5小内修改过的文件,-表示以内
[zhangy@BlackGhost download]$ find /home/zhangy -mtime -5 -print
/home/zhangy/www/css2/c_textshadow.html
/home/zhangy/www/css2/c_textautospace.html

#查找/home/zhangy下,5小前修改过的文件,+表示以前
[zhangy@BlackGhost download]$ find /home/zhangy -mtime +5 -print
/home/zhangy/www/test.php

8,exec解释

#显示当前目录下面所有大于1000000的文件,exec后面执行了一个命令,{}这个代表文件名

[zhangy@BlackGhost download]$ find . -type f -size +1000000c -exec ls -al {} \;
-rw-r--r-- 1 zhangy users 3624960 2009-03-08 ./eaccelerator-0.9.5.3.tar
-rw-r--r-- 1 zhangy users 3091711 12-18 13:48 ./mmseg-0.7.3.tar.gz
-rw-r--r-- 1 zhangy users 1191330 2009-06-26 ./pcre-7.9.tar.gz
-rw-r--r-- 1 zhangy users 3263040 06-08 20:28 ./squid-3.1.4.tar.gz
-rw-r--r-- 1 zhangy users 2431967 2008-06-22 ./squid-3.0.STABLE7.tar.gz
-rw-r--r-- 1 root root 4398222 03-16 10:00 ./blog.tar.gz

总结:上面的几种情况,能满足绝大部分的需求,上面的提到的参数可以排列组合的。例如:-type f -size +1000000c -name "*.gz"



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/linux/859.html