MSN: Phenzer@hotmail.com 欢迎加为好友讨论测试

find命令详解

上一篇 / 下一篇  2010-11-05 14:59:46 / 天气: 晴朗 / 心情: 高兴 / 精华(1) / 置顶(1) / 个人分类:Linux

find的作用是在系统中查找具有某一特征的文件 (例如文件权限、文件属主、文件长度、文件类型等等)
一、find命令的一般形式为:

find pathname -options [-print -exec -ok]

其中:
(1)pathname,find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
(2)-print,find命令将匹配的文件输出到标准输出。
(3)-exec,find命令对匹配的文件执行该参数所给出的 shell命令。相应命令的形式为 ' comm-{} \;,注意{ }和\;之间的空格。
(4)ok,和- exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的 shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行


二、find命令选项

find命令有很多选项或表达式,每一个选项前面跟随一个横杠-。主要选项:
-name   按照文件名查找文件。
-perm   按照文件权限来查找文件。
-prune  使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用了 -depth选项,那么-prune选项将被find命令忽略。
-user   按照文件属主来查找文件。
-group  按照文件所属的组来查找文件。
-mtime -n +n   按照文件的更改时间来查找文件,- n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。
find命令还有- atime和-ctime选项,但它们都和-mtime选项相似。
-nogroup   查找无有效所属组的文件,即该文件所属的组在 /etc/groups中不存在。
-nouser   查找无有效属主的文件,即该文件的属主在 /etc/passwd中不存在。
-newer file1 ! file2   查找更改时间比文件file1新但比文件file2旧的文件。
-type   查找某一类型的文件,诸如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
-size n[c]   查找文件长度为n块的文件,带有c时表示文件长度以字节计。
-depth   在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。

三、实例详解
1.-ame选项
文件名选项是最常用的选项,可以使用某种文件名模式匹配,此时要用引号(单双都可以)将文件名模式引起来。
例:在当前目录下查找以3个字母后接一个数字开头的文件
[root@RHEL5 shell]# find . -name "[a-zA-Z][a-z][a-z][0-9]*" -print  
./who2
./sql2.txt

2.-erm选项
perm是permission的简写。按照文件权限模式查找,权限最好使用八进制的权限表示法
例:在当前目录的上层目录查找权限为755,并以h开头的文件
[root@RHEL5 shell]# find ../script/ -perm 755 -name "h*" -print
../script/hello.t

3.-prune选项
不查找用-prune选项指定的目录,忽略此目录

4.-user和-nouser
  -user按文件属主查找
例:[root@RHEL5 shell]# find . -user test -print
    ./who2

  -nouser查找属主账号已经被删除的文件,使用此选项时不用指明用户名
[root@RHEL5 shell]# userdel test
[root@RHEL5 shell]# find . -nouser -print
./who2

5.-group和--nogroup
用法与.-user和-nouser相似

6.-mtime
|------------|--------------|
<--n日以前---n日<---n日以内  当前日期
-n:距今n日以内的文件;
+n:距今n日以前的文件;
例:在当前目录下查找2日以内的文件
[root@RHEL5 shell]# find . -mtime -2 -print
.
./hello.t
./who2
例:在当前目录下查找2日以前的文件
[root@RHEL5 shell]# find . -mtime +5 -print
./search
./grade.txt
./who
./output.txt
./printf
./data

还有-atime和-ctime选项,但它们和-mtime选项相似

7.查找比某个文件更新或更旧的文件
例:在当前目录下查找比文件quote.txt新但比heimei.txt旧的文件
[root@RHEL5 shell]# find . -newer quote.txt ! -newer heimei.txt -exec ls -l {} \; 
-rwxr--r-- 1 root root 53 Nov  3 16:01 ./append.sed
-rwxr--r-- 1 root root 55 Nov  3 16:10 ./insert.sed
-rw-r--r-- 1 root root 3029 Nov  4 16:16 ./heimei.txt
-rwxr--r-- 1 root root 71 Nov  3 16:22 ./change.sed
-rw-r--r-- 1 root root 25 Nov  3 17:28 ./sedread.txt

例:在当前目录下查找比文件plane.txt新的文件
[root@RHEL5 shell]# find . -newer plane.txt -print
.
./hello.t
./plane.txt2
./who2

8.-type选项
按文件的种类查找
例:在当前目录下查找所有文件类型为目录的文件---列出所有目录
[root@RHEL5 shell]# find . -type d -print
.
./script

例:在当前目录的上层目录中查找除目录以外的文件
[root@RHEL5 shell]# find .. ! -type d -print

9.-size选项
按照文件长度查找文件,长度可以用块来计量,也可以用字节来计量。
以块计量时:直接用数字表示;
以字节计量:用Nc表示,N是数字;

例:查找当前目录下字节数恰好等于3412字节的文件
[root@RHEL5 shell]# find . -size 3412c -print
./Makefile.PL

例:查找当前目录下字节数大于3412字节的文件
[root@RHEL5 shell]# find . -size +3412c -print 
.
./script

例:查找当前目录下字节数小于3412字节的文件
[root@RHEL5 shell]# find . -size -3412c -print

例:查找当前目录下块数大于1块的文件(一块=512字节)
[root@RHEL5 shell]# find . -size +1 -print    
.
./script
./heimei.txt
./Makefile.PL


10.-depth选项
先匹配所有的文件,然后再在子目录中查找
例:查找sql2.txt文件
[root@RHEL5 shell]# find . -name "sql2.txt"  -print

11.-exec和-ok
当匹配到一些文件以后,或许对其进行下一步操作,这是可以使用-exec选项。
exec选项后面跟随着所有执行的命令,然后分别是{}、一个空格、一个\,最后是一个分号
例:在当前目录中查找更改时间在5日以前的文件并删除它们
[root@RHEL5 shell]# find . -type f -mtime +5 -exec rm {} \;


TAG: find 详解 Linux linux

 

评分:0

我来说两句

Open Toolbar