`

OS + Linux Shell Command find

    博客分类:
  • OS
 
阅读更多

 

Linux中find常见用法示例

http://www.iteye.com/topic/587714

http://icarusli.iteye.com/blog/646424

http://taylorqt.iteye.com/blog/150717

http://ericyou.iteye.com/blog/1715255

http://huaonline.iteye.com/blog/1752312

http://www.cnblogs.com/wanqieddy/archive/2011/06/09/2076785.html

 

 

find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;

 

这里总结一些常用到的关于find的命令的操作:
Find命令的一般形式为:

Shell代码  收藏代码
  1. # find pathname -options [-print -exec -ok]  

让我们来看看该命令的参数:
pathname find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
-print find命令将匹配的文件输出到标准输出。
-exec  find命令对匹配的文件执行该参数所给出的shell命令。
-ok 和- exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行.
find命令选项
1. -name   按照文件名查找文件
Java代码  收藏代码
  1. find -name test.txt  
  2. # find ~ -name "xxx.txt" -print// ~ 表示$HOME目录  
  3. # find . -name "xxx.txt" -print// . 表示当前目录  
  4. # find /etc -name "host*" -print// / 在指定目录中查找  

2.-perm   按照文件权限来查找文件
Java代码  收藏代码
  1. find -perm 755  
  2. # find ~ -perm 755 -print // 意义同name属性  
  3. # find . -perm 755 -print  
  4. # find /home -perm 755 -print  

3.-mtime -n +n   按照文件的更改时间来查找文件,-n表示文件更改时间距现在n天以内,+n表示文件更改时间距现在n天以前。
Java代码  收藏代码
  1. find -mtime -2  

如果想使用find命令的这一选项来查找更改时间在两个小时以内的文件,除非有一个现成
的文件其更改时间恰好在两个小时以前,否则就没有可用来比较更改时间的文件,为了解决
这一问题,可以首先创建一个文件并将其日期和时间戳设置为所需要的时间。这可以用touch
命令来实现,假设现在的时间是21:40,希望查找更改时间在两个小时以内的文件,可以首先创建这样
Java代码  收藏代码
  1. # touch -t 02032140 dstamp  
  2. # ls -l dstamp  
  3. -rw-r--r--    1 root     root            0  2月  3 21:40 dstamp  
  4. # find . -newer dstamp  

一个文件
4.-newer file1  查找更改时间比文件file1新的文件。
Java代码  收藏代码
  1. # find -newer test.txt  

5.-type   查找某一类型的文件
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
Java代码  收藏代码
  1. # find -type d  
  2. # find . ! -type d //查找当前目录下不是目录文件的其他文件  
  3. # find ~ -type d//意义同name  
  4. # find /etc -type d//意义同name  

6.使用size选项
可以按照文件长度来查找文件,这里所指的文件长度既可以用块(block)来计量,也可以用字节来计量。以字节计量文件长度的表达形式为 Nc;以块计量文件长度只用数字表示即可。
Java代码  收藏代码
  1. # find . -size -1000000c//当前目录下查找文件长度小于1M字节的文件  
  2. # find . -size +100c  

7 find 和 xargs
在使用find命令的-exec选项处理匹配到的文件时,find命令将所有匹配到的文件一起传递
给exee执行
Java代码  收藏代码
  1. # find . -name "core" -print | xargs echo "" >/tmp/core.log//整个系统中查找内存信息转储文件然后把结果保存到/tmp/core.log 文件中  
  2. # find /apps/audit -perm -7 -print | xargs chmod o-w//在/apps/audit目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限  
  3. # find / -type f -print | xargs grep "device"//用grep命令在所有的普通文件中搜索device这个词  

 

Linux中find常见用法示例

·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;

find命令的参数;

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

#-print 将查找到的文件输出到标准输出
#-exec   command   {} \;      —–将查到的文件执行command操作,{} 和 \;之间有空格
#-ok 和-exec相同,只不过在操作前要询用户
例:find . -name .svn | xargs rm -rf

====================================================

-name   filename             #查找名为filename的文件
-perm                        #按执行权限来查找
-user    username             #按文件属主来查找
-group groupname            #按组来查找
-mtime   -n +n                #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime    -n +n               #按文件访问时间来查GIN: 0px">

-ctime    -n +n              #按文件创建时间来查找文件,-n指n天以内,+n指n天以前

-nogroup                     #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser                     #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-newer   f1 !f2              找文件,-n指n天以内,+n指n天以前 
-ctime    -n +n               #按文件创建时间来查找文件,-n指n天以内,+n指n天以前 
-nogroup                     #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser                      #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-newer   f1 !f2               #查更改时间比f1新但比f2旧的文件
-type    b/d/c/p/l/f         #查是块设备、目录、字符设备、管道、符号链接、普通文件
-size      n[c]               #查长度为n块[或n字节]的文件
-depth                       #使查找在进入子目录前先行查找完本目录
-fstype                     #查更改时间比f1新但比f2旧的文件
-type    b/d/c/p/l/f         #查是块设备、目录、字符设备、管道、符号链接、普通文件
-size      n[c]               #查长度为n块[或n字节]的文件
-depth                       #使查找在进入子目录前先行查找完本目录
-fstype                      #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
-mount                       #查文件时不跨越文件系统mount点
-follow                      #如果遇到符号链接文件,就跟踪链接所指的文件
-cpio                %;      #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
-mount                       #查文件时不跨越文件系统mount点
-follow                      #如果遇到符号链接文件,就跟踪链接所指的文件
-cpio                        #对匹配的文件使用cpio命令,将他们备份到磁带设备中
-prune                       #忽略某个目录

=====================================================
$find   ~   -name   "*.txt"   -print    #在$HOME中查.txt文件并显示
$find   .    -name   "*.txt"   -print
$find   .    -name   "[A-Z]*"   -print   #查以大写字母开头的文件
$find   /etc   -name   "host*"   -print #查以host开头的文件
$find   .   -name   "[a-z][a-z][0–9][0–9].txt"    -print   #查以两个小写字母和两个数字开头的txt文件
$find .   -perm   755   -print
$find   .   -perm -007   -exec ls -l {} \;   #查所有用户都可读写执行的文件同-perm 777
$find   . -type d   -print
$find   .   !   -type   d   -print 
$find   .   -type l   -print

$find   .   -size   +1000000c   -print        #查长度大于1Mb的文件
$find   .   -size   100c         -print       # 查长度为100c的文件
$find   .   -size   +10   -print              #查长度超过期作废10块的文件(1块=512字节)

$cd /
$find   etc   home   apps    -depth   -print   | cpio   -ivcdC65536   -o   /dev/rmt0
$find   /etc -name "passwd*"   -exec grep   "cnscn"   {}   \;   #看是否存在cnscn用户
$find . -name "yao*"   | xargs file
$find   . -name "yao*"   |   xargs   echo    "" > /tmp/core.log
$find   . -name "yao*"   | xargs   chmod   o-w

======================================================

find   -name april*                     在当前目录下查找以april开始的文件
find   -name   april*   fprint file        在当前目录下查找以april开始的文件,并把结果输出到file中
find   -name ap* -o -name may*   查找以ap或may开头的文件
find   /mnt   -name tom.txt   -ftype vfat   在/mnt下查找名称为tom.txt且文件系统类型vfat的文件
find   /mnt   -name t.txt ! -ftype vfat   在/mnt下查找名称为tom.txt且文件系统类型不为vfat的文件
find   /tmp   -name wa* -type l            在/tmp下查找名为wa开头且类型为符号链接的文件
find   /home   -mtime   -2                 在/home下查最近两天内改动过的文件
find /home    -atime -1                  查1天之内被存取过的文件
find /home -mmin    +60                  在/home下查60分钟前改动过的文件
find /home   -amin   +30                  查最近30分钟前被存取过的文件
find /home   -newer   tmp.txt             在/home下查更新时间比tmp.txt近的文件或目录
find /home   -anewer   tmp.txt            在/home下查存取时间比tmp.txt近的文件或目录
find   /home   -used   -2                  列出文件或目录被改动过之后,在2日内被存取过的文件或目录
find   /home   -user cnscn                列出/home目录内属于用户cnscn的文件或目录
find   /home   -uid   +501                  列出/home目录内用户的识别码大于501的文件或目录
find   /home   -group   cnscn              列出/home内组为cnscn的文件或目录
find   /home   -gid 501                   列出/home内组id为501的文件或目录
find   /home   -nouser                    列出/home内不属于本地用户的文件或目录
find   /home   -nogroup                   列出/home内不属于本地组的文件或目录
find   /home    -name tmp.txt    -maxdepth   4   列出/home内的tmp.txt 查时深度最多为3层
find   /home   -name tmp.txt   -mindepth   3   从第2层开始查
find   /home   -empty                     查找大小为0的文件或空目录
find   /home   -size   +512k                查大于512k的文件
find   /home   -size   -512k               查小于512k的文件
find   /home   -links   +2                查硬连接数大于2的文件或目录
find   /home   -perm   0700                查权限为700的文件或目录
find   /tmp   -name tmp.txt   -exec cat {} \;
find   /tmp   -name   tmp.txt   -ok   rm {} \;

find    /   -amin    -10     # 查找在系统中最后10分钟访问的文件
find    /   -atime   -2        # 查找在系统中最后48小时访问的文件
find    /   -empty             # 查找在系统中为空的文件或者文件夹
find    /   -group   cat        # 查找在系统中属于 groupcat的文件
find    /   -mmin   -5         # 查找在系统中最后5分钟里修改过的文件
find    /   -mtime   -1       #查找在系统中最后24小时里修改过的文件
find    /   -nouser           #查找在系统中属于作废用户的文件
find    /   -user    fred     #查找在系统中属于FRED这个用户的文件

查当前目录下的所有普通文件
# find . -type f -exec ls -l {} \; 
-rw-r–r–    1 root      root         34928 2003-02-25   ./conf/httpd.conf 
-rw-r–r–    1 root      root         12959 2003-02-25   ./conf/magic 
-rw-r–r–    1 root      root          180 2003-02-25   ./conf.d/README 
查当前目录下的所有普通文件,并在- e x e c选项中使用ls -l命令将它们列出

=================================================
在/ l o g s目录中查找更改时间在5日以前的文件并删除它们:
$ find logs -type f -mtime +5 -exec   -ok   rm {} \;

=================================================
查询当天修改过的文件
[root@book class]# find   ./   -mtime   -1   -type f   -exec   ls -l   {} \;

=================================================
查询文件并询问是否要显示
[root@book class]# find   ./   -mtime   -1   -type f   -ok   ls -l   {} \;  
< ls … ./classDB.inc.php > ? y
-rw-r–r–    1 cnscn    cnscn       13709   1月 12 12:22 ./classDB.inc.php
[root@book class]# find   ./   -mtime   -1   -type f   -ok   ls -l   {} \;  
< ls … ./classDB.inc.php > ? n
[root@book class]#

=================================================
查询并交给awk去处理
[root@book class]# who   |   awk   ’{print $1"\t"$2}’
cnscn    pts/0

=================================================
awk—grep—sed

[root@book class]# df   -k |   awk ‘{print $1}’ |   grep   -v   ’none’ |   sed   s"/\/dev\///g"
文件系统
sda2
sda1
[root@book class]# df   -k |   awk ‘{print $1}’ |   grep   -v   ’none’
文件系统
/dev/sda2
/dev/sda1

1)在/tmp中查找所有的*.h,并在这些文件中查找“SYSCALL_VECTOR",最后打印出所有包含"SYSCALL_VECTOR"的文件名

A) find   /tmp   -name   "*.h"   | xargs   -n50   grep SYSCALL_VECTOR
B) grep   SYSCALL_VECTOR   /tmp/*.h | cut    -d’:'   -f1| uniq > filename
C) find   /tmp   -name "*.h"   -exec grep "SYSCALL_VECTOR"   {}   \; -print

2)find / -name filename -exec rm -rf {} \;
    find / -name filename -ok rm -rf {} \;

3)比如要查找磁盘中大于3M的文件:
find . -size +3000k -exec ls -ld {} ;

4)将find出来的东西拷到另一个地方
find *.c -exec cp ‘{}’ /tmp ‘;’

如果有特殊文件,可以用cpio,也可以用这样的语法:
find dir -name filename -print | cpio -pdv newdir

6)查找2004-11-30 16:36:37时更改过的文件
# A=`find ./ -name "*php"` |   ls -l –full-time $A 2>/dev/null | grep "2004-11-30 16:36:37"

Linux-allLinux | No Comments »

find 实例

四月 18th, 2006

  要在/usr/linux中查找所有的*.h,并在这些文件中查找“SYSCALL_VECTOR",最后打印出所有包含"SYSCALL_VECTOR"的文件名,有以下几种方法实现
find /usr/linux -name "*.h" | xargs -n50 grep SYSCALL_VECTOR
grep SYSCALL_VECTOR /usr/linux/*.h | cut -d’:’ -f1 | uniq > filename
find /usr/linux -name "*.h" -exec grep "SYSCALL_VECTOR" {} \; -print

  我用find / -name filename| rm -rf,不成功,请问为什么不成功?
find / -name filename -exec rm -rf {} \;
find . -name filename |rm -rf试一下{} 表示你找出来的结果。
\; 则相当于“宪法”,没什么说头,就是这么规定的,在 -exec 后面需要一个表示该命令终结的的符号。可以在 man find 中找到答案。
要让rm识别find的结果,如下:
find / -name filename |xargs rm -rf
之所以find . -name filename |rm -rf不通过,是因为rm命令不接受从标准输入传过来的指令
查找含特定字符串的文件
例如查找当前目录下含有"the string you want find…"字符串的文件:
$find . -type f -exec grep “the string you want find…” {} ; -print

  从根目录开始查tmpfile,一旦查到马上删除
find / -name "tmpfile" -exec rm {} \;

  find 的perm问题
请问一下以下命令什么意思?关键是那个数字前的-,其他都还知道
find -name ".*" -perm -007
我知道
find -name ".*" -perm 755
这个是用来查找权限位为755的隐藏文件
噢,对了还有,我上边的命令都省略了find的pathname参数 find默认是查找当前工作目录的吗?
如果我用 -ok 替代 -exec, 那么还需要加上 {} \; 吗?
这个已经清楚,仍然需要,因为 -ok 只是 -exec 的提示模式,它只是多了一个确认操作的步骤,刚才没有读懂那几句E文的意思 呵呵 不好意思
-007是指查找所有用户都可读、写、执行的文件,要小心呀~~~
解释解释?
find -name ".*" -perm -007 和 find -name ".*" -perm 777 有区别吗?
-007是怎么来得呢?
不过有一个问题
我用 find . -perm -100 会列出当前目录 . , 这是为什么呢?

下面引用由explover在 2002/10/01 06:15am 发表的内容:
-007是指查找所有用户都可读、写、执行的文件,要小心呀~~~
-007是查找含其它用户(不同组,非属主)可读,写,执行的文件.并不一定要同组可读写,-是指最少权限为007.
下面引用由一颗小白菜在 2002/10/01 10:16am 发表的内容:
OK了, 呵呵
不过有一个问题
我用 find . -perm -100 会列出当前目录 . , 这是为什么呢?
这种方法不会准确的找出目录的. -100是指权限至少是属主可运行.
在unix系统下,你可以拥有对目录文件的执行权你才可以进入一个目录.这便是目录文件被列出的原因.
find . -perm -001 -print找到往往是目录文件.
我的意思当然不是使用这种方法来找目录,只不过不明白其中的 -100 意义了
那以此类推,是不是 -010是指权限至少是owner同组可执行的吗?也就是说其实这里的010和-是分开的,-表示一个至少的意思,而且010才是真正用来描述权限位的?
这样子就明白了 谢谢你噢

  将find出来的东西拷到另一个地方?
find *.c -exec cp ‘{}’ /tmp ‘;’
如果有特殊文件,可以用cpio,也可以用这样的语法:
find dir -name filename -print | cpio -pdv newdir

  找出磁盘中某个大小范围内的文件
比如要查找磁盘中大于3M的文件:
find . -size +3000k -exec ls -ld {} ;

  如何用find查找某一天更改的文件?
可以使用这一行命令来实现:
A=`find ~ -print` | ls -l –full-time $A 2>/dev/null | grep "Jun 27" | grep 1998

  使用find 命令查找某个时间段的shell怎么写。比如11点到12点的。thanks
创建一个脚本judgetime,内容如下:
ls -l $*|awk ‘{split($8,hour,":");if((hour[1]>23 || hour[1] < 1)&&hour[1]<24)print}’
到要查找的目录下,运行
find ./ -name "*" -exec judgetime {} \;
注意时间格式为24小时制。
thank you ,如果我要精确到分钟呢
touch -t 04241112 starttemp #精确到12分钟
touch -t 04241220 endtemp #截止到12点20
find [dir] -newer starttemp -a ! -newer endtemp -exec ls -l {} \;
newer?
那昨天12:10文件如何呢?
每天执行的时候,用当天的日期和时间戳替换一下不就行了吗?
我不知道他是不是把所有的11:00~12:00的都找出来,是不是只执行一次还是每天都执行?
这种情况俺猜想是自己的东西放在哪忘了,只记得当时是深夜了。
有道理!
不愧是斑竹!
不光知道怎么解决问题,还知道在什么情况下出现这类问题,佩服佩服!
问题又出现了。创建这个文件的时候。本来应该是时间的一栏现在写上了2002,而不是12:00.
等到12:00过了吧!

  删除指定日期的文件
find ./ -name 文件名 -exec rm -f {} \;
例:删除当前30天内没用过的文件,用如下命令:
find / -atime +30 -exec rm -f {} \;
我自己试着写了一小段SHELL,也用ll ,grep, rm 几个命令,用起来还差强人意。
对过滤出来的文件名列表中用了一个FOR语句,再执行rm 。现在我想把这段SHELL 扩展一下让它每天定时运行将 n 天前的文件删掉,有没有人能给我一些提示,谢谢!
还有个问题,对于前面那位朋友提到的"find / -atime +30 -exec rm -f {} \;
"方法,我很早就试过几次,不过好像都不太对,参数 -atime n 是查找n天前被访问过的文件,我不明白的是这里的时间参照点是什么,以及这个n天是怎么计算的。
问 题二、对于"ll |cut -f 1" 这个命令我是不是用错了,我只想取出 ll 中列出的文件名,但用cut -f 命令做不到 ,我只好换用 ll |cut -c 59- 这种方式得到我要的文件名,but it’s a pool idear !我也试过用awk ,好像也不对,看看大家可不可以给我一些小小的提醒,TKS SO MUCH
问题三、如何改变 I结点 的日期格式 我现在的系统显示的格式是:
-rw-r—– 1 msahz01 users 2253 2002年2月 2日 poheader.i
我想把这换成
-rw-rw-rw- 1 house users 2193 Apr 19 2001 hkdisp.p
如何才能做到这点?
awk 应该可以
ll | awk ‘{print $9}’
删除多少天之前的文件
find /yourpath -mtime +31 -exec rm {} \;
find /yourpath -mtime +366 -exec rm {} \;

find中, -ctime, -mtime及其-atime有何区别

请问 -ctime 和 -mtime 有什么关系 ?
如果父目录的 ctime 改变, 那它下面的文件的 ctime 就会自动都改了吗 ?
-ctime 和 -mtime ,-atime 这些信息是存在哪儿呢 ?

我用 -mtime -1 找到了新建或改的文件.
但怎样才能找到一天内 mv 来的文件呢( 它们的时间是原有的时间,早于一天 ) ?

用-newer选项啊。
你可以先touch一个你想要的时间的文件如下:
$ touch -t 08190800 test
$ ls -l test
-rw-r–r– 1 dba other 0 Aug 19 08:00 test
然后
$ find . -newer test -print
.
./.sh_history
$ ls -l .sh_history
-rw——- 1 dba other 154 Aug 20 17:39 .sh_history

用touch可以写出你想要的任何时间的文件,然后用-newer ,! -newer选项即可成功。

1.ctime含inode信息修改的时间.mtime只指文件内容建立或修改的时间.
2 不会.
3.这些信息应该是存在文件系统的超级块里.

我查了书 -ctime 是指 inode 的改变(或称文件的状态改变).
请问 inode 存了哪些信息 ?
做了些小测试,-mtime 改, -ctime 一定也改.
改文件名, -ctime 也会改.
谁能回答 i-node 存了哪些东西 ?

vi /usr/include/sys/inode.h

班主,我不能 access /usr/include/sys/inode.h .
摘书如下:
Directories contain directory entries. Each entry contains a file or subdirectory name and an index node reference number (i-node number). To increase speed and enhance use of disk space, the data in a file is stored at various locations in the computer’s memory. The i-node contains the addresses used to locate all the scattered blocks of data associated with a file. The i-node also records other information about the file including time of modification and access, access modes, number of links, file owner, and file type.
可我发现 -atime 改了, -ctime 还没改. why ?
( 我先 cat 一个 ASCII 文件,再用 -atime -1 有它用 -ctime -1 居然没有它.)
着岂不跟 inode 信息改变, ctime 就改矛盾吗?

我不同意你贴出来的那段文章,正如我提到的那样,atime,ctime,mtime是放到超级块里,在sco unix下是一种叫stat的结构.(stat_32),不同的系统文件系统可能不同.
sco 下inode的结构如下:

typedef struct inode
{
struct inode *i_forw; /* inode hash chain */
struct inode *i_back; /* ‘’ */
struct inode *av_forw; /* freelist chain */
struct inode *av_back; /* ‘’ */
int *i_fsptr; /* "typeless" pointer to fs dependent */
ino32_t i_number; /* i number, 1-to-1 with dev address */
ushort i_ftype; /* file type = IFDIR, IFREG, etc. */
short i_fstyp; /* File system type */
off_t i_size; /* size of file */
ushort i_uid; /* owner */
ushort i_gid; /* group of owner */
ushort i_flag;
ushort i_want; /* i_flag extension to avoid MP races */
ushort i_count; /* reference count */
short i_nlink; /* directory entries */
dev_t i_rdev; /* Raw device number */
#define i_namtype i_rdev /* i_ftype==IFNAM subtype */
dev_t i_dev; /* device where inode resides */
struct mount *i_mton;/* ptr to mount table entry that */
/* this directory is mounted on */
struct region *i_rp; /* ptr to shared region if any */
struct stdata *i_sp; /* ptr to associated stream */
struct iisem *isem; /* ptr to XENIX semaphores */
struct iisd *isd; /* ptr to XENIX shared data */
} i_un;
#define i_mnton i_un.i_mton /* i_ftype==IFDIR IMOUNT */
#define i_rptr i_un.i_rp /* i_ftype==IFREG || i_ftype==IFBLK */
#define i_sptr i_un.i_sp /* i_ftype==IFCHR || i_ftype==IFIFO */
#define i_sem i_un.isem /* i_ftype==IFNAM && i_namtype==IFSEM */
#define i_sd i_un.isd /* i_ftype==IFNAM && i_namtype==IFSHD */

struct fstypsw *i_fstypp; /* ptr to file system switch FSPTR */
long *i_filocks; /* pointer to filock (structure) list */
unsigned long i_mappages; /* number of pages currently cached */
unsigned long i_vcode; /* read-ahead block save (NFS) */
short i_wcnt; /* write open count or ITEXT count */
struct lockb i_cilock; /* tas to synchronize i_flag changes */
ushort i_rdlocks; /* count of non-exclusive lockers */
} inode_t;

所以,访问一个文件不能改变inode信息.
使用chown, chgrp, chmod命令可以很好的比较mtime和ctime
chown改变一个文件的属主,用ctime可以找到,用mtime便找不到.
试试看.

多谢斑竹! 我是在 Solaris 上面试的.我是对 -ctime 不明白.
试的结果如下:
修改文件,-mtime 改了, -ctime 也会改.
访问文件,-atime 改了, -ctime 没变.
chown, chgrp, chmod,mv, 都会使 -ctime 改变,但不影响 -atime 和 -mtime.
touch 可以改 -mtime and/or -atime,但 touch -a 只改访问时间时,-ctime也改了.
touch -m 改修改时间时,-ctime当然也改了.
好象还有别的很多东西可以令 -ctime 改变, 搞不清楚.
有什么方法可以显示 -mtime,atime,ctime 吗?
可以用 -ctime 来实现对目录的增量文件进行备份或 transfer 吗 ?
多谢!

没有什么工具显示,(可能是俺不知道)
把下面程序里的st_mtime换成st_ctime,或st_atime便可以得到你要的了.
#include
int
main (int argc, char **argv)
{
struct stat buf;
char date[80];
char fname[80];
printf("Enter filename (with full path) to check mtime : ");
scanf("%s",fname);
stat(fname, &buf);
printf ("mtime (in sec) of %s = %ld\n", fname, buf.st_mtime);
strcpy(date, ctime((time_t *)&(buf.st_mtime)));
printf ("mtime (in date) of %s = %s\n", fname, date);
}

至于文件备份,有什么不可以的么?

mtime ls -l 最近修改文件内容的时间
atime ls -lu 最近访问文件的时间
ctime ls -li 最近文件有所改变的状态 ,如文件修改,属性\属主 改变 ,节点 ,链接变化等 ,应该是不拘泥只是时间前后的改变

俺看了ls的帮助,以为只是按ctime或atime排序,显示的时间还是mtime.

仔细比较了一下,ayhan说的是对的.谢谢ayhan.

多谢 ahyan 提示 ! 我在 Solaris 上试过如下:
mtime 用 ls -l 看到
atime 用 ls -lu 看到
ctime 用 ls -lc 看到. (ls -li 只有 inode number)
摘书如下:
-c Uses time of last modification of the i-node (file
created, mode changed, and so forth) for sorting (-t)
or printing (-l or -n).
-u Uses time of last access instead of last modification
for sorting (with the -t option) or printing (with the
-l option).
-i For each file, prints the i-node number in the first
column of the report.

 

Linux Find 命令精通指南
http://huaonline.iteye.com/blog/1752312
作者:Sheryl Calish

简单介绍这一无处不在的命令的强大的方面以及混乱的方面。

2008 年 7 月发布

Linux find 命令是所有 Linux 命令中最有用的一个,同时也是最混乱的一个。它很难,因为它的语法与其他 Linux 命令的标准语法不同。但是,它很强大,因为它允许您按文件名、文件类型、用户甚至是时间戳查找文件。使用 find 命令,您不但可以找到具这些属性任意组合的文件,还可以对它找到的文件执行操作。

本文的目的是,通过概述 find 命令的用途和潜能,简化该命令的学习和使用。同时,它将针对 find 命令的某些最强大但最混乱的方面提供一个基本的指南和参考。

[注意:本文使用的 find 版本是 GNU 版本,因此,某些细节可能与其他版本的 find 有所不同。]
基本格式

开始之前,我们先来看一下 find 命令的基本结构:

find   start_directory  test  options   criteria_to_match
action_to_perform_on_results
                          

在以下命令中,find 将开始在当前目录(用“.”表示)中查找任何扩展名为“java”的文件:

find . -name  "*.java"   

下面是该命令所找到的命令的缩略清单:

find . -name  "*.java"
./REGEXPvalidate/src/oracle/otnsamples/plsql/ConnectionManager.java
./REGEXPvalidate/src/oracle/otnsamples/plsql/DBManager.java
..

[注意:如果您从本文剪切并粘贴来运行该 find 命令,您可能需要使用自己的键盘替换双引号 (“”) 才能得出正确的结果。]

以下命令将执行相同的操作。在这两种情况下,您都需要对通配符进行转义以确保它传递到 find 命令并且不由 shell 解释。因此,请将您的搜索字符串放到引号里,或者在它前面加上反斜线:

find . -name  \*.java

尽管 find 的所有参数均为可选,但是如果您未指定从哪里开始搜索,搜索默认将在当前目录中开始。如果您不指定要匹配的测试连接、选项或值,您的结果将不完整或者无区别。

运行以下三个 find 命令将得出同样的结果 — 当前目录和所有子目录中的所有文件(包括隐藏文件)的完整清单:

find 
find .
find . -print

这类似于运行一个带 -la 选项的 ls 命令。如果您希望上述命令的输出包含完整的路径名(或许是为了备份),您将需要指定起始目录的完整路径:

find /home/bluher -name \*.java
/home/bluher/plsql/REGEXPvalidate/src/oracle/otnsamples/plsql/ConnectionManager.java
/home/bluher/plsql/REGEXPvalidate/src/oracle/otnsamples/plsql/DBManager.java/
...

您还可以在搜索字符串中指定多个起始目录。如果以具有相应权限的用户身份运行,以下命令将下到 /usr、/home /tmp 目录查找所有 jar 文件:

find /usr /home  /tmp -name "*.jar"

但是,如果您没有相应的权限,您在开始浏览许多系统目录时将生成错误消息。以下是一个示例:

find:  /tmp/orbit-root: Permission denied

您可以通过附加您的搜索字符串来避免混乱的输出,如下所示:

find /usr /home  /tmp -name "*.jar" 2>/dev/null

这会将所有错误消息发送到空文件,因此提供清理器输出。

默认情况下,find 是区分大小写的。对于不区分大小写的 find,将 -iname 测试替换为 -name 测试。

find downloads  -iname "*.gif"
downloads/.xvpics/Calendar05_enlarged.gif
downloads/lcmgcfexsmall.GIF

除文件名外,您还可以按类型搜索文件。例如,您可以使用以下命令查找一个目录中的所有子目录:

find . -type d          

您可以使用以下命令查找您的/usr 目录中的所有符号链接:

find /usr -type l

这可能会列出 3,000 多个链接。以下的任何一个命令使用根权限运行都将列出 /usr 目录中的链接以及它所指向的文件:

# find /usr/bin  -type l  -name "z*" -exec ls  -l {} \;
lrwxrwxrwx 1 root  root 8 Dec 12 23:17 /usr/bin/zsh -> /bin/zsh
lrwxrwxrwx 1 root  root 5 Dec 12 23:17 /usr/bin/zless -> zmore
lrwxrwxrwx 1 root  root 9 Dec 12 23:17 /usr/bin/zcat -> /bin/zcat

find /usr/bin -type  l  -name "z*" -ls

但是,第二个更短的命令将列出更多的文件,以及目录和 inode 信息:在本文后面的部分中,我们将讨论 -exec 和 -ls 操作的用法。

其他 find 可以找到的文件类型包括:

• b — 块(缓存)特殊
• c — 字符(未缓存)特殊
• p — 命名管道 (FIFO)
• s — 套接字

使用根作为 find 命令的起点会极大地降低系统的速度。如果您必须运行这样一个命令,您可以在非高峰时段或晚上运行它。您可以使用以下语法将输出重定向到一个文件:

find  /   -print > masterfilelist.out

如果您错误地输入一个 find 命令,生成大量不必要的输出,只需按 CTRL-C 中断该命令,这将停止最近执行的命令。

在 具多个文件系统的企业网络上,限制 find 查找的文件也是一个特别好用的方法。尽可能多地使用选项和测试以减少系统上的负载。用于此目的的两个最有用的选项是 -xdev 和 -mount。它们通过阻止 find 下到其他文件系统(如 MS-DOS、CD-ROM 或 AFS)上的目录中缩短了搜索范围。这将搜索限制为同一类型的文件系统作为起始目录。

如果运行 mount 命令,双引导系统上的用户可以使用这些选项。假设涉及 Windows 分区,您可以使用类似以下的命令安装它:

mount -t vfat  /dev/sda1 /mnt/msdos

您使用的实际命令取决于您的系统设置。您可以通过运行 df 或执行以下命令验证该分区已安装:

find /mnt/msdos  -name "*.txt" 2> /dev/null

您应该看到 MS Windows 分区上列出了很多的文件。现在,运行以下带 -mount 或 -xdev 选项的命令:

find / -name  "*.txt" -mount 2> /dev/null

或者

find / -name  "*.txt" -xdev 2> /dev/null

还可以使用 -fstype 测试明确告知 find 在哪个文件系统中查找,如以下示例中所示:

find / -name  "*.txt" -fstype vfat 2> /dev/null

查找时间

find 命令有几个用于根据您系统的时间戳搜索文件的选项。这些时间戳包括

• mtime — 文件内容上次修改时间
• atime — 文件被读取或访问的时间
• ctime — 文件状态变化时间

mtime 和 atime 的含义都是很容易理解的,而 ctime 则需要更多的解释。由于 inode 维护着每个文件上的元数据,因此,如果与文件有关的元数据发生变化,则 inode 数据也将变化。这可能是由一系列操作引起的,包括创建到文件的符号链接、更改文件权限或移动了文件等。由于在这些情况下,文件内容不会被读取或修改,因此 mtime 和 atime 不会改变,但 ctime 将发生变化。

这些时间选项都需要与一个值 n 结合使用,指定为 -n、n 或 +n。

• -n 返回项小于 n
• +n 返回项大于 n
• n 返回项正好与 n 相等

下面,我们来看几个例子,以便于理解。以下命令将查找在最近 1 小时内修改的所有文件:

find . -mtime -1
./plsql/FORALLSample
./plsql/RegExpDNASample
/plsql/RegExpSample

用 1 取代 -1 运行同一命令将查找恰好在 1 小时以前修改的所有文件:

find . -mtime 1 

上述命令不会生成任何结果,因为它要求完全吻合。以下命令查找 1 个多小时以前修改的所有文件:

find . -mtime +1 

默认情况下,-mtime、-atime 和 -ctime 指的是最近 24 小时。但是,如果它们前面加上了开始时间选项,则 24 小时的周期将从当日的开始时间算起。您还可以使用 mmin、amin 和 cmin 查找在不到 1 小时的时间内变化了的时间戳。

如果您在登录到您的帐户后立即运行以下命令,您将找到在不到 1 分钟以前读取的所有文件:

find . -amin -1
./.bashrc
/.bash_history
./.xauthj5FCx1

应该注意的是,使用 find 命令查找文件本身将更改该文件的访问时间作为其元数据的一部分。

您还可以使用 -newer、-anewer 和 –cnewer 选项查找已修改或访问过的文件与特定的文件比较。这类似于 -mtime、-atime 和 –ctime。

• -newer 指内容最近被修改的文件
• -anewer 指最近被读取过的文件
• -cnewer 指状态最近发生变化的文件

要查找您的主目录中自上一个 tar 文件以来以某种方式编辑过的所有文件,使用以下命令:

find . -newer  backup.tar.gz

按大小查找文件

-size 选项查找满足指定的大小条件的文件。要查找所有大于 5MB 的用户文件,使用

find / -size  +5000000c 2> /dev/null
/var/log/lastlog
/var/log/cups/access_log.4
/var/spool/mail/bluher

结尾的“c”以字节为单位报告我们的结果。默认情况下,find 以 512 字节块的数量报告大小。如果我们将“c”替换为“k”,我们还会看到以千字节的数量报告的结果,如果使用“w”,则会看到以两字节字的数量报告的结果。

-size 选项经常用于搜索所有零字节文件并将它们移至 /tmp/zerobyte 文件夹。以下命令恰好可以完成这一任务:

find test -type f  -size 0 -exec mv {} /tmp/zerobyte \;

-exec 操作允许 find 在它遇到的文件上执行任何 shell 命令。在本文的后面部分,您将看到其用法的更多示例。大括号允许移动每个空文件。

选项 -empty 还可用于查找空文件:

find test -empty        
test/foo
test/test

按权限和所有者查找

要监视您的系统安全离不开 find 命令。您可以使用符号或八进制表示法查找面向广大用户开放的文件,如下所示:

find . -type f  -perm a=rwx -exec ls -l {} \; 

或者

find . -type f  -perm 777 -exec ls -l {} \;
-rwxrwxrwx 1 bluher  users 0 May 24 14:14 ./test.txt

在这一部分中,在上面和下面的命令中,我们使用了 -exec ls -l 操作,因此,您可以看到返回的文件的实际权限。以下命令将查找可由“other”和组写入的文件:

find plsql -type f  -perm -ug=rw -exec ls -l {} \; 2>/dev/null

或者

find plsql -type f  -perm -220 -exec ls -l {} \; 2>/dev/null 
-rw-rw-rw- 1 bluher users 4303  Jun  7   2004 plsql/FORALLSample/doc/otn_new.css
-rw-rw-rw- 1 bluher users 10286 Jan  12  2005  plsql/FORALLSample/doc/readme.html
-rw-rw-rw- 1 bluher users 22647 Jan  12  2005  plsql/FORALLSample/src/config.sql
..

下一个命令将查找由用户、组或二者共同写入的文件:  

find plsql -type f  -perm /ug=rw -exec ls -l {} \; 2>/dev/null, or,
find plsql -type f  -perm /220 -exec ls -l {} \; 2>/dev/null 
-rw-r--r-- 1 bluher users 21473  May  3 16:02 plsql/regexpvalidate.zip
-rw-rw-rw- 1 bluher users 4303  Jun  7   2004 plsql/FORALLSample/doc/otn_new.css
-rw-rw-rw- 1 bluher users 10286 Jan  12  2005  plsql/FORALLSample/doc/readme.html
-rw-rw-rw- 1 bluher users 22647 Jan  12  2005  plsql/FORALLSample/src/config.sql

您可能会看到以下命令在 Web 和较早的手册中引用过:

find . -perm +220  -exec ls -l {} \; 2> /dev/null 

+ 符号的作用与 / 符号相同,但是现在新版 GNU findutils 中不支持使用该符号。

要查找您的系统上所有人都可以写入的所有文件,使用以下命令:

find / -wholename  '/proc' -prune  -o  -type f -perm -0002 -exec ls -l {} \;
-rw-rw-rw- 1 bluher users 4303  Jun  7   2004/home/bluher/plsql/FORALLSample/doc/otn_new.css
-rw-rw-rw- 1 bluher users 10286 Jan  12  2005  /home/bluher/plsql/FORALLSample/doc/readme.html
...

第 4 个权限将在稍后进行讨论,但最后一个字段中的“2”是文件权限中的“other”字段,也称为写入位。我们在权限模式 0002 前面使用了破折号,以指明我们希望看到为 other 设置了写权限的文件,无论其他权限设置为什么。

上 述命令还引入了三个新概念。针对文件模式“/proc”使用 -wholename 测试,如果该模式已找到,-prune 可防止 find 下到该目录中。布尔类型“-o”使 find 可以针对其他目录处理该命令的其余部分。由于每个表达式之间有一个假设的隐式 and 运算符 (-a),因此,如果左侧的表达式计算结果为 false,and 之后的表达式将不进行计算;因此需要 -o 运算符。Find 还支持布尔类型 -not、!,就像使用括号强行优先一样。

系统管理员经常使用 find 通过用户或组的名称或 ID 搜索特定用户或组的常规文件:

[root] $  find / -type f -user bluher -exec ls -ls {}  \;

下面是这样一个命令的高度精简的输出示例:

4 -rw-r--r-- 1 bluher users 48  May  1 03:09  /home/bluher/public_html/.directory
4 -rw-r--r-- 1 bluher users 925  May  1 03:09 /home/bluher/.profile

您还可以使用 find 按组查找文件:

[root] $ find /  -type f -group users

find / -type d -gid  100

该命令将列出由 ID 为 100 的组拥有的目录。要找到相应的 uid 或 gid,您可以针对 /etc/passwd 或 /etc/group 文件运行 more 或 cat 命令。

除了查找特定已知用户和组的文件外,您还会发现它对于查找没有这些信息的文件也很有用。下一个命令将识别未列在 /etc/passwd 或 /etc/group 文件中的文件:

find / -nouser -o  -nogroup

上述命令可能不会在您的系统上生成实际的结果。但是,它可用于识别或许在经常移动后没有用户或组的文件。

好了,现在我们可以解决本部分开始时提到的格外重要的权限了。

SGID 和 SUID 是特殊访问权限标志,可以分配给基于 UNIX 的操作系统上的文件和目录。设置它们是为了允许访问计算机系统的普通用户使用临时提升的权限执行二进制可执行文件。

find /  \( -perm -2000 -o -perm -4000 \) -ls
167901   12 -rwsr-xr-x   1 root     root         9340 Jun 16  2006 /usr/bin/rsh
167334   12 -rwxr-sr-x   1 root     tty         10532 May  4  2007 /usr/bin/wall

在上述命令中,您可以看到转义括号的使用。您还可以看到权限的不同。第一个文件设置了 SGID 权限,第二个文件设置了 SUID 权限。上述命令中的最后的操作与带 -exec ls -dils 操作的 find 效果类似。
控制 find

与 Linux 中的许多命令不同,find 不需要 -r 或 -R 选项即可下到子目录中。它默认情况下就这样操作。但是,有时您可能希望限制这一行为。因此,选项 -depth、-maxdepth 和 -mindepth 以及操作 -prune 就派上用场了。

我们已经看到了 -prune 是多么有用,下面让我们来看看 -depth、-maxdepth 和 -mindepth 选项。

-maxdepth 和 -mindepth 选项允许您指定您希望 find 搜索深入到目录树的哪一级别。如果您希望 find 只在目录的一个级别中查找,您可以使用 maxdepth 选项。

通过运行以下命令在目录树的前三个级别中查找日志文件,您可以看到 -maxdepth 的效果。使用该选项较之不使用该选项所生成的输出要少得多。

find / -maxdepth 3  -name "*log"

您还可以让 find 在至少下至目录树三个级别的目录中查找:

find / -mindepth 3  -name "*log"

-depth 选项确保先在一个目录中进行查找,然后才在其子目录中进行查找。以下命令提供了一个示例:

find -name "*test*" -depth
./test/test
./test
./localbin/test
./localbin/test_shell_var
./localbin/test.txt
./test2/test/test
./test2/test
./test2

find 世界

我 们已经看过了 find 命令的一些更加有用以及有点难懂的功能,但是 find 还可以执行更多的任务。例如,有多个选项可以使 find 与较低的 UNIX 版本和其他操作系统相兼容并允许您执行打印输出到多个文件等操作。阅读本文后,您现在已经有了理解 find 参考指南的背景,我鼓励您深入研究这一强大、有用的工具。

 

 

 

 

 

Linux文件查找命令find,xargs详述

总结:zhy2111314
来自:LinuxSir.Org
整理:北南南北
摘要: 本文是find 命令的详细说明,可贵的是针对参数举了很多的实例,大量的例证,让初学者更为容易理解;本文是zhyfly兄贴在论坛中;我对本文进行了再次整理,为方便大家阅读;

目录

版权声明
前言:关于find命令

一、find 命令格式

      1、find命令的一般形式为;
      2、find命令的参数;
      3、find命令选项;
      4、使用exec或ok来执行shell命令; 

二、find命令的例子;

      1、查找当前用户主目录下的所有文件;
      2、为了在当前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件;
      3、为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径;
      4、查找/var/logs目录中更改时间在7日以前的普通文件,并在删除之前询问它们;
      5、为了查找系统中所有属于root组的文件;
      6、find命令将删除当目录中访问时间在7日以来、含有数字后缀的admin.log文件
      7、为了查找当前文件系统中的所有目录并排序;
      8、为了查找系统中所有的rmt磁带设备; 

三、xargs
四、find 命令的参数;

      1、使用name选项
      2、用perm选项
      3、忽略某个目录
      4、使用find查找文件的时候怎么避开某个文件目录
      5、使用user和nouser选项
      6、使用group和nogroup选项
      7、按照更改时间或访问时间等查找文件
      8、查找比某个文件新或旧的文件
      9、使用type选项
      10、使用size选项
      11、使用depth选项
      12、使用mount选项 

五、关于本文
六、相关文档


+++++++++++++++++++++++++++++++++++++++++++++++++
正文
+++++++++++++++++++++++++++++++++++++++++++++++++


版权声明

本文是zhyfly兄贴在LinuxSir.Org 的一个帖子而整理出来的,如果您对版权有疑问,请在本帖后面跟帖。谢谢;本文的HTML版本由北南南北整理;修改了整篇文档的全角及说明文字中的单词中每个字母空格的问题;为标题加了编号,方便大家阅读;

前言:关于find命令

由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统中含有网络文件系统( NFS),find命令在该文件系统中**,只你具有相应的权限。

在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。


一、find 命令格式


1、find命令的一般形式为;

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


2、find命令的参数;

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


3、find命令选项

-name

按照文件名查找文件。

-perm
按照文件权限来查找文件。

-prune
使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。

-user
按照文件属主来查找文件。

-group
按照文件所属的组来查找文件。

-mtime -n +n
按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。

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

-nouser
查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2

查找更改时间比文件file1新但比文件file2旧的文件。
-type

查找某一类型的文件,诸如:

b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。

-size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。

-mount:在查找文件时不跨越文件系统mount点。
-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。

另外,下面三个的区别:
   -amin n
  查找系统中最后N分钟访问的文件

  -atime n
  查找系统中最后n*24小时访问的文件

  -cmin n
  查找系统中最后N分钟被改变文件状态的文件

  -ctime n
  查找系统中最后n*24小时被改变文件状态的文件

    -mmin n
  查找系统中最后N分钟被改变文件数据的文件

  -mtime n
  查找系统中最后n*24小时被改变文件数据的文件


4、使用exec或ok来执行shell命令

使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的

在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。

exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。

例如:为了用ls -l命令列出所匹配到的文件,可以把ls -l命令放在find命令的-exec选项中

# find . -type f -exec ls -l {  } \;
-rw-r--r--    1 root     root        34928 2003-02-25  ./conf/httpd.conf
-rw-r--r--    1 root     root        12959 2003-02-25  ./conf/magic
-rw-r--r--    1 root     root          180 2003-02-25 begin_of_the_skype_highlighting              180 2003-02-25      end_of_the_skype_highlighting  ./conf.d/README

上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。
在/logs目录中查找更改时间在5日以前的文件并删除它们:

$ find logs -type f -mtime +5 -exec rm {  } \;

记住:在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。

在下面的例子中, find命令在当前目录中查找所有文件名以.LOG结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。

$ find . -name "*.conf"  -mtime +5 -ok rm {  } \;
< rm ... ./conf/httpd.conf > ? n

按y键删除文件,按n键不删除。

任何形式的命令都可以在-exec选项中使用。

在下面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个sam用户。

# find /etc -name "passwd*" -exec grep "sam" {  } \;
sam:501:501::/usr/sam:/bin/bash


二、find命令的例子;


1、查找当前用户主目录下的所有文件:

下面两种方法都可以使用

$ find $HOME -print
$ find ~ -print


2、让当前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件;

$ find . -type f -perm 644 -exec ls -l {  } \;


3、为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径;

$ find / -type f -size 0 -exec ls -l {  } \;


4、查找/var/logs目录中更改时间在7日以前的普通文件,并在删除之前询问它们;

$ find /var/logs -type f -mtime +7 -ok rm {  } \;


5、为了查找系统中所有属于root组的文件;

$find . -group root -exec ls -l {  } \;
-rw-r--r--    1 root     root          595 10月 31 01:09 ./fie1


6、find命令将删除当目录中访问时间在7日以来、含有数字后缀的admin.log文件。

该命令只检查三位数字,所以相应文件的后缀不要超过999。先建几个admin.log*的文件 ,才能使用下面这个命令

$ find . -name "admin.log[0-9][0-9][0-9]" -atime -7  -ok
rm {  } \;
< rm ... ./admin.log001 > ? n
< rm ... ./admin.log002 > ? n
< rm ... ./admin.log042 > ? n
< rm ... ./admin.log942 > ? n


7、为了查找当前文件系统中的所有目录并排序;

$ find . -type d | sort


8、为了查找系统中所有的rmt磁带设备;

$ find /dev/rmt -print


三、xargs

xargs - build and execute command lines from standard input

在 使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现 溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。

find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。

在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;

而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。

来看看xargs命令是如何同find命令一起使用的,并给出一些例子。

下面的例子查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件

#find . -type f -print | xargs file
./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text
./.kde/Autostart/.directory:      ISO-8859 text\
......

在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中:

$ find / -name "core" -print | xargs echo "" >/tmp/core.log

上面这个执行太慢,我改成在当前目录下查找

#find . -name "file*" -print | xargs echo "" > /temp/core.log
# cat /temp/core.log
./file6

在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限:

# ls -l
drwxrwxrwx    2 sam      adm          4096 10月 30 20:14 file6
-rwxrwxrwx    2 sam      adm             0 10月 31 01:01 http3.conf
-rwxrwxrwx    2 sam      adm             0 10月 31 01:01 httpd.conf

# find . -perm -7 -print | xargs chmod o-w
# ls -l
drwxrwxr-x    2 sam      adm          4096 10月 30 20:14 file6
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 http3.conf
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 httpd.conf

用grep命令在所有的普通文件中搜索hostname这个词:

# find . -type f -print | xargs grep "hostname"
./httpd1.conf:#     different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

用grep命令在当前目录下的所有普通文件中搜索hostnames这个词:

# find . -name \* -type f -print | xargs grep "hostnames"
./httpd1.conf:#     different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

注意,在上面的例子中, \用来取消find命令中的*在shell中的特殊含义。

find命令配合使用exec和xargs可以使用户对所匹配到的文件执行几乎所有的命令。


四、find 命令的参数

下面是find一些常用参数的例子,有用到的时候查查就行了,像上面前几个贴子,都用到了其中的的一些参数,也可以用man或查看论坛里其它贴子有find的命令手册


1、使用name选项

文件名选项是find命令最常用的选项,要么单独使用该选项,要么和其他选项一起使用。

可以使用某种文件名模式来匹配文件,记住要用引号将文件名模式引起来。

不管当前路径是什么,如果想要在自己的根目录$HOME中查找文件名符合*.txt的文件,使用~作为 'pathname'参数,波浪号~代表了你的$HOME目录。

$ find ~ -name "*.txt" -print

想要在当前目录及子目录中查找所有的‘ *.txt’文件,可以用:

$ find . -name "*.txt" -print

想要的当前目录及子目录中查找文件名以一个大写字母开头的文件,可以用:

$ find . -name "[A-Z]*" -print

想要在/etc目录中查找文件名以host开头的文件,可以用:

$ find /etc -name "host*" -print

想要查找$HOME目录中的文件,可以用:

$ find ~ -name "*" -print 或find . -print

要想让系统高负荷运行,就从根目录开始查找所有的文件。

$ find / -name "*" -print

如果想在当前目录查找文件名以两个小写字母开头,跟着是两个数字,最后是.txt的文件,下面的命令就能够返回名为ax37.txt的文件:

$find . -name "[a-z][a-z][0--9][0--9].txt" -print


2、用perm选项

按照文件权限模式用-perm选项,按文件权限模式来查找文件的话。最好使用八进制的权限表示法。

如在当前目录下查找文件权限位为755的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件,可以用:

$ find . -perm 755 -print

还有一种表达方法:在八进制数字前面要加一个横杠-,表示都匹配,如-007就相当于777,-006相当于666

# ls -l
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 http3.conf
-rw-rw-rw-    1 sam      adm         34890 10月 31 00:57 httpd1.conf
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 httpd.conf
drw-rw-rw-    2 gem      group        4096 10月 26 19:48 sam
-rw-rw-rw-    1 root     root         2792 10月 31 20:19 temp

# find . -perm 006
# find . -perm -006
./sam
./httpd1.conf
./temp

-perm mode:文件许可正好符合mode

-perm +mode:文件许可部分符合mode

-perm -mode: 文件许可完全符合mode


3、忽略某个目录

如果在查找文件时希望忽略某个目录,因为你知道那个目录中没有你所要查找的文件,那么可以使用-prune选项来指出需要忽略的目录。在使用-prune选项时要当心,因为如果你同时使用了-depth选项,那么-prune选项就会被find命令忽略。

如果希望在/apps目录下查找文件,但不希望在/apps/bin目录下查找,可以用:

$ find /apps -path "/apps/bin" -prune -o -print


4、使用find查找文件的时候怎么避开某个文件目录

比如要在/usr/sam目录下查找不在dir1子目录之内的所有文件

find /usr/sam -path "/usr/sam/dir1" -prune -o -print

find [-path ..] [expression] 在路径列表的后面的是表达式

-path "/usr/sam" -prune -o -print 是 -path "/usr/sam" -a -prune -o
-print 的简写表达式按顺序求值, -a 和 -o 都是短路求值,与 shell 的 && 和 || 类似如果 -path "/usr/sam" 为真,则求值 -prune , -prune 返回真,与逻辑表达式为真;否则不求值 -prune,与逻辑表达式为假。如果 -path "/usr/sam" -a -prune 为假,则求值 -print ,-print返回真,或逻辑表达式为真;否则不求值 -print,或逻辑表达式为真。

这个表达式组合特例可以用伪码写为

if -path "/usr/sam"  then
          -prune
else
          -print

避开多个文件夹

find /usr/sam \( -path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -print

圆括号表示表达式的结合。
\ 表示引用,即指示 shell 不对后面的字符作特殊解释,而留给 find 命令去解释其意义。

查找某一确定文件,-name等选项加在-o 之后

#find /usr/sam  \(-path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -name "temp" -print


5、使用user和nouser选项

按文件属主查找文件,如在$HOME目录中查找文件属主为sam的文件,可以用:

$ find ~ -user sam -print

在/etc目录下查找文件属主为uucp的文件:

$ find /etc -user uucp -print

为了查找属主帐户已经被删除的文件,可以使用-nouser选项。这样就能够找到那些属主在/etc/passwd文件中没有有效帐户的文件。在使用-nouser选项时,不必给出用户名; find命令能够为你完成相应的工作。

例如,希望在/home目录下查找所有的这类文件,可以用:
$ find /home -nouser -print


6、使用group和nogroup选项

就像user和nouser选项一样,针对文件所属于的用户组, find命令也具有同样的选项,为了在/apps目录下查找属于gem用户组的文件,可以用:

$ find /apps -group gem -print

要查找没有有效所属用户组的所有文件,可以使用nogroup选项。下面的find命令从文件系统的根目录处查找这样的文件

$ find / -nogroup-print


7、按照更改时间或访问时间等查找文件

如果希望按照更改时间来查找文件,可以使用mtime,atime或ctime选项。如果系统突然没有可用空间了,很有可能某一个文件的长度在此期间增长迅速,这时就可以用mtime选项来查找这样的文件。

用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件。

希望在系统根目录下查找更改时间在5日以内的文件,可以用:

$ find / -mtime -5 -print

为了在/var/adm目录下查找更改时间在3日以前的文件,可以用:

$ find /var/adm -mtime +3 -print


8、查找比某个文件新或旧的文件

如果希望查找更改时间比某个文件新但比另一个文件旧的所有文件,可以使用-newer选项。它的一般形式为:

newest_file_name ! oldest_file_name

其中,!是逻辑非符号。

查找更改时间比文件sam新但比文件temp旧的文件:

例:有两个文件

-rw-r--r--    1 sam      adm             0 10月 31 01:07 fiel
-rw-rw-rw-    1 sam      adm         34890 10月 31 00:57 httpd1.conf
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 httpd.conf
drw-rw-rw-    2 gem      group        4096 10月 26 19:48 sam
-rw-rw-rw-    1 root     root         2792 10月 31 20:19 temp

# find -newer httpd1.conf  ! -newer temp -ls
1077669    0 -rwxrwxr-x   2 sam      adm             0 10月 31 01:01 ./httpd.conf
1077671    4 -rw-rw-rw-   1 root     root         2792 10月 31 20:19 ./temp
1077673    0 -rw-r--r--   1 sam      adm             0 10月 31 01:07 ./fiel

查找更改时间在比temp文件新的文件:

$ find . -newer temp -print


9、使用type选项

在/etc目录下查找所有的目录,可以用:

$ find /etc -type d -print

在当前目录下查找除目录以外的所有类型的文件,可以用:
$ find . ! -type d -print

在/etc目录下查找所有的符号链接文件,可以用
$ find /etc -type l -print


10、使用size选项

可以按照文件长度来查找文件,这里所指的文件长度既可以用块(block)来计量,也可以用字节来计量。以字节计量文件长度的表达形式为N c;以块计量文件长度只用数字表示即可。

在按照文件长度查找文件时,一般使用这种以字节表示的文件长度,在查看文件系统的大小,因为这时使用块来计量更容易转换。
在当前目录下查找文件长度大于1 M字节的文件:
$ find . -size +1000000c -print

在/home/apache目录下查找文件长度恰好为100字节的文件:

$ find /home/apache -size 100c -print

在当前目录下查找长度超过10块的文件(一块等于512字节):

$ find . -size +10 -print


11、使用depth选项

在使用find命令时,可能希望先匹配所有的文件,再在子目录中查找。使用depth选项就可以使find命令这样做。这样做的一个原因就是,当在使用find命令向磁带上备份文件系统时,希望首先备份所有的文件,其次再备份子目录中的文件。

在下面的例子中, find命令从文件系统的根目录开始,查找一个名为CON.FILE的文件。

它将首先匹配所有的文件然后再进入子目录中查找。

$ find / -name "CON.FILE" -depth -print


12、使用mount选项

在当前的文件系统中查找文件(不进入其他文件系统),可以使用find命令的mount选项。

从当前目录开始查找位于本文件系统中文件名以XC结尾的文件:

$ find . -name "*.XC" -mount -print



五、关于本文

本文是find 命令的详细说明,可贵的是针对参数举了很多的实例,大量的例证,让初学者更为容易理解;本文是zhy2111314兄贴在论坛中;我对本文进行了再次整理,为方便大家阅读; ── 北南南北

六、相关文档

    * 由 北南南北 在 2005/12/20 - 10:24 发表
    * Linux
    * 基础知识
    * 命令/SHELL/PERL
    * 要发表评论,请先登录 或 注册

-exec 和 管道符

用 -exec来执行shell命令,和通过管道符|来执行有什么不同?

    * 由 li_qinshan 在 2008/10/15 - 18:05 发表
    * 要发表评论,请先登录 或 注册

可不可以忽略 Permission denied的

-bash-3.1$ find /home/f/a -type f -name enen -exec ls -l {} \;
find: /home/f/a/liuyunge: Permission denied
find: /home/f/a/ll1012: Permission denied
find: /home/f/a/hebaidu: Permission denied
find: /home/f/a/ET_SUN: Permission denied
find: /home/f/a/Rory.Lu: Permission denied
-rw-r--r-- 1 lufeng 27108 60 Nov 1 14:24 /home/f/a/lufeng/enen
find: /home/f/a/shenglee2007: Permission denied
find: /home/f/a/zlh750620: Permission denied
find: /home/f/a/mlh720goodboy: Permission denied

    * 由 LinuxSir 在 2007/11/03 - 10:20 发表
    * 要发表评论,请先登录 或 注册

-mtime +n

-mtime +n -n

"用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件"

疑问:用加号+来限定更改时间在距今 (n+1) 日以前的文件

 

 

 

find

http://taylorqt.iteye.com/blog/150717
find is one of the most useful Linux/Unix tools around, but most people use only a fraction of its power. Many Linux/Unix questions seen online can be solved using the find command alone; it's simply a matter of becoming familiar with its options.

The power of find lets you do anything from finding all your .jpg files to seeing "all of Michael's files that have the execute bit set and have been modified since yesterday." When combined with xargs, a properly wielded find command can make many common tasks ten times easier.


Basics
Let's start simple and get progressively more advanced. To begin with we'll look at finding things by name. Remember that the first argument you give find is where to look.

Find all files with something in their name:
find . -name "*.jpg"


...
./Pictures/iPhoto Library/Data/2006/Roll 20/00697_bluewaters_1440x900.jpg
./Pictures/iPhoto Library/Data/2006/Roll 20/00705_cloudyday_1440x900.jpg
./Pictures/iPhoto Library/Data/2006/Roll 20/00710_fragile_1600x1200.jpg
./Pictures/iPhoto Library/Data/2006/Roll 20/00713_coolemoticon_1440x900.jpg
./Pictures/iPhoto Library/Data/2006/Roll 20/00714_cloudyday_1440x900.jpg
...


Note that by default when you give a location to start from (in our case "."), the find command starts there and drills all the way down during its search. So in this case I started from my home directory and it found the files all the way down in "~/Pictures/iPhoto Library/Data/2006/Roll 20" as well.

** Placing quotes around the search criteria avoids issues with wildcard characters and is probably a good habit to get into. You can also use -iname instead of -name; it's the same but it's case insensitive

Find all files that belong to a certain user:
find . -user daniel


...
./Music/iTunes/iTunes Music/Tool/Undertow/01 Intolerance.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/02 Prison Sex.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/03 Sober.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/04 Bottom.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/05 Crawl Away.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/06 Swamp Song.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/07 Undertow.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/08 4 Degrees.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/09 Flood.m4a
./Music/iTunes/iTunes Music/Tool/Undertow/69 Disgustipated.m4a
...


** Also works for groups (-group)

Find only directories, regular files, links, or sockets:
find . -type d


...
./Development/envelope
./Development/mhp
./Development/mservers
./Development/mservers/fortune100
./Development/mst
./Development/mst/nmap
./Development/mst/services
...


Those are all directories, and to look for the others (files, links, or sockets), just substitute f, l, s for the d in the command above.

Find files that are over a gigabyte in size
find ~/Movies -size +1024M


...
/Movies/Comedy/Funny.mpg
/Movies/Drama/Sad.mpg
...




Combining Arguments
You can also combine arguments using and, or, and not. By default if you use two different arguments you're and'ing them. If you want to use or you give the -o option, and if you want to get everything except something, you use the ! option.

Find only regular files, owned by daniel, that are also jpg images
find . -user daniel -type f -name *.jpg


...
./Pictures/iPhoto Library/autumn_woods.jpg
./Pictures/iPhoto Library/blue_forest.jpg
./Pictures/iPhoto Library/brothers.jpg
...


Now do the same, but exclude anything named autumn
find . -user daniel -type f -name *.jpg ! -name autumn*


...
./Pictures/iPhoto Library/blue_forest.jpg
./Pictures/iPhoto Library/brothers.jpg
...




Forensics
find also has a number of options that help one answer forensics-oriented questions such as when a file was last changed or what files have had their permissions modified recently.

Find all files in /etc owned by root that have been modified within the last day
find /etc -user root -mtime -1


...
/etc/passwd
...


The checks you can use here are:

-atime: when the file was last accessed
-ctime: when the file's permissions were last changed
-mtime: when the file's data was last modified
These searches are done in 24 hour increments and followed by a number n. If you want to match the exact 24 hour period you use n by itself. More frequently, however, you'll want to say everything since yesterday, or everything "more than 3 days ago." This is accomplished using the -n and +n options respectively.

There are also minute versions of the atime, ctime, and mtime arguments:

-amin: when (in minutes) the file was last accessed
-cmin: when (in minutes) the file's permissions were last changed
-mmin: when (in minutes) the file's data was last modified
Show me all files in /etc owned by root that have been accessed within the last two minutes
find /etc -user root -amin -2


...
/etc/hosts
/etc/resolv.conf
...


A list of a few other forensics-oriented options:

-nouser: shows output that's not associated with an existing userid
-nogroup: shows output not associated with an existing groupid
-links n: file has n links
-newer file: file was modified more recently than file.
-perm mode: file has mode permissions.
Show me all files in ~ with wide open permissions
find ~ -perm 777


...
~/testfile.txt
~/lab.rtf
...




Combining find With xargs
This is the piece that we've been leading up to -- performing an action on the stuff that we find with find. So while it's interesting to say, "Show me this stuff", it's far more useful to say, "Take every text file owned by Jason that's hasn't been accessed in 60 days and move it to the backup folder."


Cookbook Examples Of find in action** Be sure to test these on your system before using them for important files

Find all files on your system that are world writable. The 0002 denotes a 2 in the "other" field in the file permissions, which is the write bit
find / -perm -0002


Collect files that are not owned by valid users and delete them
find / -nouser -print0 | xargs -0 rm


Clean the images off of your *nix desktop
find ~/Desktop -name "*.jpg" -o -name "*.gif" -o -name "*.png" -print0 | xargs -0 mv --target-directory ~/Pictures

** The -print0 option terminates results with a null character instead of the default newline, making it cleaner and less likely to balk in many cases

Correct the permissions on your web directory
find /your/webdir/ -type d -print0 | xargs -0 chmod 755
find /your/webdir -type f | xargs chmod 644


Show a list of files in /etc that have been modified since last month
find /etc -mtime -30


A Final Thought
There is a bit of a debate in some circles about using xargs vs. the -exec option that's built into find itself. To me, however, it's not much of a debate; -exec isn't nearly as good as xargs for what I use find for. I tend to use it to do big jobs involving many files. "Move all these files there", "copy all those directories there", "Delete these links.", etc.

This is where -exec breaks down and xargs stands up. Whe you use -exec you run a seperate instance of the called program for each input. With xargs, you build up the input into bundles and run them through the called command as few times as possible, which is usually just once. When dealing with hundreds or thousands of elements this is a big win for xargs.
Don't believe me? Well, let's run some numbers. Below is a listing of 5,310 .jpg files on my OS X system using both -exec and xargs:

time find . -name "*.jpg" -exec ls {} \;


real    0m23.548s
user    0m3.913s
sys     0m15.167s


Hmm, that's not bad. 23 seconds for over five thousand files, right? Let's try it with xargs.

time find . -name "*.jpg" -print0 | xargs -0 ls


real    0m1.526s
user    0m0.667s
sys     0m0.864s


That's 2 seconds vs 24 seconds. Seriously; find and xargs is the combination you want to use.





References
The find man page:
http://www.netadmintools.com/html/find.man.html

The xargs man page:
http://www.research.att.com/~gsf/man/man1/xargs.html

 

 

Linux文件查找命令find,xargs详述

http://icarusli.iteye.com/blog/646424

<!-- begin content -->

来自:cn

摘要: 本文是find 命令的详细说明,可贵的是针对参数举了很多的实例,大量的例证,让初学者更为容易理解;本文是zhyfly兄贴在论坛中;我对本文进行了再次整理,为方便大家阅读;

目录

版权声明
前言:关于find命令

一、find 命令格式
1、find命令的一般形式为;
2、find命令的参数;
3、find命令选项;
4、使用exec或ok来执行shell命令;

二、find命令的例子;

1、查找当前用户主目录下的所有文件;
2、为了在当前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件;
3、为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径;
4、查找/var/logs目录中更改时间在7日以前的普通文件,并在删除之前询问它们;
5、为了查找系统中所有属于root组的文件;
6、find命令将删除当目录中访问时间在7日以来、含有数字后缀的admin.log文件
7、为了查找当前文件系统中的所有目录并排序;
8、为了查找系统中所有的rmt磁带设备;

三、xargs
四、find 命令的参数;

1、使用name选项
2、用perm选项
3、忽略某个目录
4、使用find查找文件的时候怎么避开某个文件目录
5、使用user和nouser选项
6、使用group和nogroup选项
7、按照更改时间或访问时间等查找文件
8、查找比某个文件新或旧的文件
9、使用type选项
10、使用size选项
11、使用depth选项
12、使用mount选项

五、关于本文
六、相关文档


+++++++++++++++++++++++++++++++++++++++++++++++++
正文
+++++++++++++++++++++++++++++++++++++++++++++++++


版权声明

本文是zhyfly兄贴在LinuxSir.Org 的一个帖子而整理出来的,如果您对版权有疑问,请在本帖后面跟帖。谢谢;本文的HTML版本由北南南北整理;修改了整篇文档的全角及说明文字中的单词中每个字母空格的问题;为标题加了编号,方便大家阅读;

前言:关于find命令

由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限。

在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。


一、find 命令格式


1、find命令的一般形式为;

 

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

 


2、find命令的参数;

 

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

 


3、find命令选项

-name

按照文件名查找文件。

-perm
按照文件权限来查找文件。

-prune
使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。

-user
按照文件属主来查找文件。

-group
按照文件所属的组来查找文件。

-mtime -n +n
按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。

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

-nouser
查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2

查找更改时间比文件file1新但比文件file2旧的文件。
-type

查找某一类型的文件,诸如:

b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。

-size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。

-mount:在查找文件时不跨越文件系统mount点。
-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。

 

另外,下面三个的区别:

   -amin n
  查找系统中最后N分钟访问的文件

  -atime n
  查找系统中最后n*24小时访问的文件

  -cmin n
  查找系统中最后N分钟被改变文件状态的文件

  -ctime n
  查找系统中最后n*24小时被改变文件状态的文件

    -mmin n
  查找系统中最后N分钟被改变文件数据的文件

  -mtime n
  查找系统中最后n*24小时被改变文件数据的文件

 


4、使用exec或ok来执行shell命令

使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的

在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。

exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。

例如:为了用ls -l命令列出所匹配到的文件,可以把ls -l命令放在find命令的-exec选项中

 

# find . -type f -exec ls -l {  } \;
-rw-r--r--    1 root     root        34928 2003-02-25  ./conf/httpd.conf
-rw-r--r--    1 root     root        12959 2003-02-25  ./conf/magic
-rw-r--r--    1 root     root          180 2003-02-25  ./conf.d/README

 

上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。
在/logs目录中查找更改时间在5日以前的文件并删除它们:

 

$ find logs -type f -mtime +5 -exec rm {  } \;

 

记住:在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。

在下面的例子中, find命令在当前目录中查找所有文件名以.LOG结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。

 

$ find . -name "*.conf"  -mtime +5 -ok rm {  } \;
< rm ... ./conf/httpd.conf > ? n

 

按y键删除文件,按n键不删除。

任何形式的命令都可以在-exec选项中使用。

在下面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个sam用户。

 

# find /etc -name "passwd*" -exec grep "sam" {  } \;
sam:x:501:501::/usr/sam:/bin/bash

 


二、find命令的例子;


1、查找当前用户主目录下的所有文件:

下面两种方法都可以使用

 

$ find $HOME -print
$ find ~ -print


2、让当前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件;

 

 

$ find . -type f -perm 644 -exec ls -l {  } \;

 


3、为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径;

 

$ find / -type f -size 0 -exec ls -l {  } \;

 


4、查找/var/logs目录中更改时间在7日以前的普通文件,并在删除之前询问它们;

 

$ find /var/logs -type f -mtime +7 -ok rm {  } \;

 


5、为了查找系统中所有属于root组的文件;

 

$find . -group root -exec ls -l {  } \;
-rw-r--r--    1 root     root          595 10月 31 01:09 ./fie1

 


6、find命令将删除当目录中访问时间在7日以来、含有数字后缀的admin.log文件。

该命令只检查三位数字,所以相应文件的后缀不要超过999。先建几个admin.log*的文件 ,才能使用下面这个命令

 

$ find . -name "admin.log[0-9][0-9][0-9]" -atime -7  -ok
rm {  } \;
< rm ... ./admin.log001 > ? n
< rm ... ./admin.log002 > ? n
< rm ... ./admin.log042 > ? n
< rm ... ./admin.log942 > ? n

 


7、为了查找当前文件系统中的所有目录并排序;

 

$ find . -type d | sort

 


8、为了查找系统中所有的rmt磁带设备;

 

$ find /dev/rmt -print

 


三、xargs

xargs - build and execute command lines from standard input

在使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现 溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。

find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。

在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;

而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。

来看看xargs命令是如何同find命令一起使用的,并给出一些例子。

下面的例子查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件

 

#find . -type f -print | xargs file
./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text
./.kde/Autostart/.directory:      ISO-8859 text\
......

 

在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中:

 

$ find / -name "core" -print | xargs echo "" >/tmp/core.log

 

上面这个执行太慢,我改成在当前目录下查找

 

#find . -name "file*" -print | xargs echo "" > /temp/core.log
# cat /temp/core.log
./file6

 

在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限:

 

# ls -l
drwxrwxrwx    2 sam      adm          4096 10月 30 20:14 file6
-rwxrwxrwx    2 sam      adm             0 10月 31 01:01 http3.conf
-rwxrwxrwx    2 sam      adm             0 10月 31 01:01 httpd.conf

# find . -perm -7 -print | xargs chmod o-w
# ls -l
drwxrwxr-x    2 sam      adm          4096 10月 30 20:14 file6
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 http3.conf
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 httpd.conf

 

用grep命令在所有的普通文件中搜索hostname这个词:

 

# find . -type f -print | xargs grep "hostname"
./httpd1.conf:#     different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

 

用grep命令在当前目录下的所有普通文件中搜索hostnames这个词:

 

# find . -name \* -type f -print | xargs grep "hostnames"
./httpd1.conf:#     different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

 

注意,在上面的例子中, \用来取消find命令中的*在shell中的特殊含义。

find命令配合使用exec和xargs可以使用户对所匹配到的文件执行几乎所有的命令。


四、find 命令的参数

下面是find一些常用参数的例子,有用到的时候查查就行了,像上面前几个贴子,都用到了其中的的一些参数,也可以用man或查看论坛里其它贴子有find的命令手册


1、使用name选项

文件名选项是find命令最常用的选项,要么单独使用该选项,要么和其他选项一起使用。

可以使用某种文件名模式来匹配文件,记住要用引号将文件名模式引起来。

不管当前路径是什么,如果想要在自己的根目录$HOME中查找文件名符合*.txt的文件,使用~作为 'pathname'参数,波浪号~代表了你的$HOME目录。

 

$ find ~ -name "*.txt" -print

 

想要在当前目录及子目录中查找所有的‘ *.txt’文件,可以用:

 

$ find . -name "*.txt" -print

 

想要的当前目录及子目录中查找文件名以一个大写字母开头的文件,可以用:

 

$ find . -name "[A-Z]*" -print

 

想要在/etc目录中查找文件名以host开头的文件,可以用:

 

$ find /etc -name "host*" -print

 

想要查找$HOME目录中的文件,可以用:

 

$ find ~ -name "*" -print 或find . -print

 

要想让系统高负荷运行,就从根目录开始查找所有的文件。

 

$ find / -name "*" -print

 

如果想在当前目录查找文件名以两个小写字母开头,跟着是两个数字,最后是.txt的文件,下面的命令就能够返回名为ax37.txt的文件:

 

$find . -name "[a-z][a-z][0--9][0--9].txt" -print

 


2、用perm选项

按照文件权限模式用-perm选项,按文件权限模式来查找文件的话。最好使用八进制的权限表示法。

如在当前目录下查找文件权限位为755的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件,可以用:

 

$ find . -perm 755 -print

 

还有一种表达方法:在八进制数字前面要加一个横杠-,表示都匹配,如-007就相当于777,-006相当于666

 

# ls -l
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 http3.conf
-rw-rw-rw-    1 sam      adm         34890 10月 31 00:57 httpd1.conf
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 httpd.conf
drw-rw-rw-    2 gem      group        4096 10月 26 19:48 sam
-rw-rw-rw-    1 root     root         2792 10月 31 20:19 temp

# find . -perm 006
# find . -perm -006
./sam
./httpd1.conf
./temp

 

-perm mode:文件许可正好符合mode

-perm +mode:文件许可部分符合mode

-perm -mode: 文件许可完全符合mode


3、忽略某个目录

如果在查找文件时希望忽略某个目录,因为你知道那个目录中没有你所要查找的文件,那么可以使用-prune选项来指出需要忽略的目录。在使用-prune选项时要当心,因为如果你同时使用了-depth选项,那么-prune选项就会被find命令忽略。

如果希望在/apps目录下查找文件,但不希望在/apps/bin目录下查找,可以用:

 

$ find /apps -path "/apps/bin" -prune -o -print

 


4、使用find查找文件的时候怎么避开某个文件目录

比如要在/usr/sam目录下查找不在dir1子目录之内的所有文件

 

find /usr/sam -path "/usr/sam/dir1" -prune -o -print

 

 

find [-path ..] [expression] 在路径列表的后面的是表达式

 

-path "/usr/sam" -prune -o -print 是 -path "/usr/sam" -a -prune -o
-print 的简写表达式按顺序求值, -a 和 -o 都是短路求值,与 shell 的 && 和 || 类似如果 -path "/usr/sam" 为真,则求值 -prune , -prune 返回真,与逻辑表达式为真;否则不求值 -prune,与逻辑表达式为假。如果 -path "/usr/sam" -a -prune 为假,则求值 -print ,-print返回真,或逻辑表达式为真;否则不求值 -print,或逻辑表达式为真。

这个表达式组合特例可以用伪码写为

 

if -path "/usr/sam"  then
          -prune
else
          -print

 

避开多个文件夹

 

find /usr/sam \( -path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -print

 

圆括号表示表达式的结合。

\ 表示引用,即指示 shell 不对后面的字符作特殊解释,而留给 find 命令去解释其意义。

 

查找某一确定文件,-name等选项加在-o 之后

 

#find /usr/sam  \(-path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -name "temp" -print

 


5、使用user和nouser选项

按文件属主查找文件,如在$HOME目录中查找文件属主为sam的文件,可以用:

 

$ find ~ -user sam -print

 

在/etc目录下查找文件属主为uucp的文件:

 

$ find /etc -user uucp -print

 

为了查找属主帐户已经被删除的文件,可以使用-nouser选项。这样就能够找到那些属主在/etc/passwd文件中没有有效帐户的文件。在使用-nouser选项时,不必给出用户名; find命令能够为你完成相应的工作。

例如,希望在/home目录下查找所有的这类文件,可以用:

$ find /home -nouser -print

 


6、使用group和nogroup选项

就像user和nouser选项一样,针对文件所属于的用户组, find命令也具有同样的选项,为了在/apps目录下查找属于gem用户组的文件,可以用:

 

$ find /apps -group gem -print

 

要查找没有有效所属用户组的所有文件,可以使用nogroup选项。下面的find命令从文件系统的根目录处查找这样的文件

 

$ find / -nogroup-print

 


7、按照更改时间或访问时间等查找文件

如果希望按照更改时间来查找文件,可以使用mtime,atime或ctime选项。如果系统突然没有可用空间了,很有可能某一个文件的长度在此期间增长迅速,这时就可以用mtime选项来查找这样的文件。

用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件。

希望在系统根目录下查找更改时间在5日以内的文件,可以用:

 

$ find / -mtime -5 -print

 

为了在/var/adm目录下查找更改时间在3日以前的文件,可以用:

 

$ find /var/adm -mtime +3 -print

 


8、查找比某个文件新或旧的文件

如果希望查找更改时间比某个文件新但比另一个文件旧的所有文件,可以使用-newer选项。它的一般形式为:

 

newest_file_name ! oldest_file_name

 

其中,!是逻辑非符号。

查找更改时间比文件sam新但比文件temp旧的文件:

例:有两个文件

 

-rw-r--r--    1 sam      adm             0 10月 31 01:07 fiel
-rw-rw-rw-    1 sam      adm         34890 10月 31 00:57 httpd1.conf
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 httpd.conf
drw-rw-rw-    2 gem      group        4096 10月 26 19:48 sam
-rw-rw-rw-    1 root     root         2792 10月 31 20:19 temp

# find -newer httpd1.conf  ! -newer temp -ls
1077669    0 -rwxrwxr-x   2 sam      adm             0 10月 31 01:01 ./httpd.conf
1077671    4 -rw-rw-rw-   1 root     root         2792 10月 31 20:19 ./temp
1077673    0 -rw-r--r--   1 sam      adm             0 10月 31 01:07 ./fiel

 

查找更改时间在比temp文件新的文件:

 

$ find . -newer temp -print


9、使用type选项

 

在/etc目录下查找所有的目录,可以用:

 

$ find /etc -type d -print

 

在当前目录下查找除目录以外的所有类型的文件,可以用:

$ find . ! -type d -print

 

在/etc目录下查找所有的符号链接文件,可以用

$ find /etc -type l -print

 


10、使用size选项

可以按照文件长度来查找文件,这里所指的文件长度既可以用块(block)来计量,也可以用字节来计量。以字节计量文件长度的表达形式为N c;以块计量文件长度只用数字表示即可。

在按照文件长度查找文件时,一般使用这种以字节表示的文件长度,在查看文件系统的大小,因为这时使用块来计量更容易转换。
在当前目录下查找文件长度大于1 M字节的文件:

$ find . -size +1000000c -print

 

在/home/apache目录下查找文件长度恰好为100字节的文件:

 

$ find /home/apache -size 100c -print

 

在当前目录下查找长度超过10块的文件(一块等于512字节):

 

$ find . -size +10 -print

 


11、使用depth选项

在使用find命令时,可能希望先匹配所有的文件,再在子目录中查找。使用depth选项就可以使find命令这样做。这样做的一个原因就是,当在使用find命令向磁带上备份文件系统时,希望首先备份所有的文件,其次再备份子目录中的文件。

在下面的例子中, find命令从文件系统的根目录开始,查找一个名为CON.FILE的文件。

它将首先匹配所有的文件然后再进入子目录中查找。

 

$ find / -name "CON.FILE" -depth -print

 


12、使用mount选项

在当前的文件系统中查找文件(不进入其他文件系统),可以使用find命令的mount选项。

从当前目录开始查找位于本文件系统中文件名以XC结尾的文件:

 

$ find . -name "*.XC" -mount -print

 



五、关于本文

本文是find 命令的详细说明,可贵的是针对参数举了很多的实例,大量的例证,让初学者更为容易理解;本文是zhy2111314兄贴在论坛中;我对本文进行了再次整理,为方便大家阅读; ── 北南南北

六、相关文档

-exec 和 管道符

用 -exec来执行shell命令,和通过管道符|来执行有什么不同?

可不可以忽略 Permission denied的

-bash-3.1$ find /home/f/a -type f -name enen -exec ls -l {} \;
find: /home/f/a/liuyunge: Permission denied
find: /home/f/a/ll1012: Permission denied
find: /home/f/a/hebaidu: Permission denied
find: /home/f/a/ET_SUN: Permission denied
find: /home/f/a/Rory.Lu: Permission denied
-rw-r--r-- 1 lufeng 27108 60 Nov 1 14:24 /home/f/a/lufeng/enen
find: /home/f/a/shenglee2007: Permission denied
find: /home/f/a/zlh750620: Permission denied
find: /home/f/a/mlh720goodboy: Permission denied

-mtime +n

-mtime +n -n

"用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件"

疑问:用加号+来限定更改时间在距今 (n+1) 日以前的文件

very good.Thanks

very good.Thanks

很好的文章

谢谢,又学到一些东西

非常感谢,很实用的文档

非常感谢,很实用的文档,对俺很有帮助哟。

如-007就相当于777,-006相当于666 有误

-007应该是至少007的意思:如:407,447 etc

man,find有详细的例子,-007是的确至少007的意思

我查看了man,find有详细的例子,-007是的确至少007的意思。
而且 -perm +mode 是老的用法,新用法是 find -perm /mode.手册上说他们作用是一样的。

-exec后边的{}有问题

{}之间没有空格吧.页面上的命令有空格,复制粘贴后是错误的.

这里的确有问题

debian:~# find /usr/bin -type f -size -50c -exec 'ls -l' {} ;
find: missing argument to `-exec'

debian:~# find /usr/bin -type f -size -50c -exec ls -l '{}' ';'
-rwxr-xr-x 1 root root 30 2003-11-08 21:31 /usr/bin/rgrep
-rwxr-xr-x 1 root root 29 2004-11-22 14:09 /usr/bin/bison.yacc
-rwxr-xr-x 1 root root 48 2005-09-04 15:20 /usr/bin/pydoc2.3
-rwxr-xr-x 1 root root 32 2005-05-16 19:02 /usr/bin/gnome-sudo

debian:~# find /usr/bin -type f -size -50c -exec ls -l '{}' \;
-rwxr-xr-x 1 root root 30 2003-11-08 21:31 /usr/bin/rgrep
-rwxr-xr-x 1 root root 29 2004-11-22 14:09 /usr/bin/bison.yacc
-rwxr-xr-x 1 root root 48 2005-09-04 15:20 /usr/bin/pydoc2.3
-rwxr-xr-x 1 root root 32 2005-05-16 19:02 /usr/bin/gnome-sudo

debian:~# find /usr/bin -type f -size -50c -exec ls -l \{\} \;
-rwxr-xr-x 1 root root 30 2003-11-08 21:31 /usr/bin/rgrep
-rwxr-xr-x 1 root root 29 2004-11-22 14:09 /usr/bin/bison.yacc
-rwxr-xr-x 1 root root 48 2005-09-04 15:20 /usr/bin/pydoc2.3
-rwxr-xr-x 1 root root 32 2005-05-16 19:02 /usr/bin/gnome-sudo

在用-exec的时候,command不能用'',而{}和;必须用'',或者\转义。

不错的文章

这样的实用实干的文章越多越好!

非常奇怪的问题!!!!

在RHEL4下有文件:/lib/modules/2.6.9ELsmp/source/include/linux/version.h
但是为何用命令find / -name version.h
或者到目录:/lib/modules/2.6.9ELsmp/下
find . -name version.h
都无法找到上述文件。
但是在:/lib/modules/2.6.9ELsmp/source下
find . -name version.h
却可以找到这个文件。
不知是什么原因????

排版有误?

这么多&line; 应该是|吧

谢谢,已经修复

谢谢,已经修复;

改过了么? 怎么还有line

7、为了查找当前文件系统中的所有目录并排序;

$ find . -type d &line;sort

非常感谢,全部修正;

非常感谢,全部修正;

在solaris下

在solaris下,不用也不能转义 直接使用 ...... -exec ls -l {} \; 即可.

 

 

end

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics