zoukankan      html  css  js  c++  java
  • find命令

    http://blog.chinaunix.net/uid-7340476-id-225283.html

    find命令主要用来在硬盘上搜索文件, find命令主要用于文件查找,列出当前目录及子目录下所有的文件和文件夹

    格式:find path -option  "keyword" [-print] [-exec -ok command] {} ;

    path: 查找路径 ;该命令用于在指定路径中查找符合条件的文件,搜索路径可以是多个目录,不同目录之间用空格分割

    -option: 选项

    "keyword": 关键字

    command: 需要执行的命令

    -exec command] {} ;将查到的文件执行command操作,注意{}和;之间有空格

    -ok和-exec的作用一样,只是-ok在执行前会询问用户是否执行操作

    find . -name "*" | xargs grep -i "passwd"(自己工作中用的最多的实例,-i的作用是不区分大小写

    find -name "t*" -perm 744    //查找当前目录下文件名以t开头的,且文件属性主具有读、写、执行权限的文件。。。。

    find还有-exec选项,对匹配文件执行该参数过给出的shell命令。

    例如:find /etc/ -type f -name "rc*" -exec ls -l {} ;       //注意{}和之间有空格。。。

    由此可见:可以接多个选项参数

    目录路径:表示以此目录作为根目录逐级往下搜索

    1.目录介绍:

    如果find不指定目录,则默认从当前所在的目录开始搜索

    $find

    $find .

    以上两个结果一样

     .  一个点表示当前目录,也可以使用 ./ 来表示;

     .. 两个点表示父目录,也可以 ../ 来代表。

    如果需要从根目录开始查找:/

     find .   遍历输出当前目录下的所有文件(夹)及子文件(夹)             

     find /   遍历输出根目录下的所有文件(夹)及子文件(夹)             

     find ./  遍历输出当前目录的下一级路径的所有文件(夹)及子文件(夹)

    也可以是/opt/qmfsun之类的路径

    find中的目录可以指定多个搜索目录

    $ind /usr /home /tmp -name "*.java";在/usr /home /tmp三个目录中查找以.java结尾的文件

    如果对某个目录没有访问权限的话,就会报错,提示: find: /tmp/qmfsun:Permission denide

    2需要搜索的关键字

    关键字可以使用正则表达式来模糊匹配该文件名,记住要用""将文件名模式引起来,不用双引号的话,需要转义
    find . -name *.txt
     

    3.主要选项参数:

    注意:

    a)每一个选项前面跟随一个横杠-

    $find /doc -name '*bak' -exec rm -rf {} ;     //从 /doc 目录开始往下找,找到凡是文件名结尾为 bak的文件,把它删除掉。

    注意:-exec 选项是执行的意思,rm -rf是删除命令,{ } 表示文件名,“;”是规定的命令结尾。  

    find命令默认情况下是区分大小写的,可以通过-iname或者在grep中添加-i参数来忽略大小写

    -name :指定按照文件名查找文件,查找时文件名大小写敏感只能搜索到文件名,如果需要搜索文件内容里包含的特定字符串,需要用grep(用的最常见)

    -iname: 查找时不区分文件名大小写

    $ find . -iname U*          
      users users2

    #如果执行find . -name U*将不会找到匹配的文件

    find -iname "MyCProgram.c";所有不区分大小写的文件名为“MyCProgram.c”的文件

     查找当前用户主目录下的所有文件:下面两种方法都可以使用

    $ find $HOME -print
    $ find ~ -print
     
    $ find . -name "*.log";从当前目录中查找扩展名为.log的文件。需要说明的是,缺省情况下,find会从指定的目录搜索,并递归的搜索其子目录
      ./install.log
     
    $find . -name "*.log"
    $find . -name *.log;在当前目录查找 以.log结尾的文件。 ". "代表当前目录,这两个命令执行结果一样
    说明:
     
     
     
     
    $find . -name "*";查找当前目录下的所有文件
    $find . -name "*qmf*";查找当前目录下文件名中包含qmf的文件
    $find ./ -name "[A-Z]*";想要当前目录及子目录中查找文件名以一个大写字母开头的文件
    $find /etc -name "host*";想要在/etc目录中查找文件名以host开头的文件
    $find ~ -name "*";想要查找$HOME目录中的文件
    $find . -name "[a-z][a-z][0--9][0--9].txt";如果想在当前目录查找文件名以两个小写字母开头,跟着是两个数字,最后是.txt的文件,返回名为ax37.txt的文件
     
    $find /etc -name inittab -o -size +2048000       --在etc目录下查找名称为inittab或者文件到校大于1000MB的文件
    $find /etc -size +163840 -a -size -204800        --在etc目录下查找大于80MB小于100MB的文件
    $find /etc -name inittab -exec ls -l {} ;       --在etc目录下查找inittab文件并显示其详细信息
    $find /etc -name init* -a -type f -exec ls -l {} ;   --在etc目录下查找以init开头的、文件类型为二进制文件,查找到以后并查看详细信息;

    grep差多个字符串
    ps -e|grep -E ‘grant_server|commsvr|tcpsvr|dainfo’ 查找多个字符串的匹配(grep -E相当于egrep)
    使用grep匹配“与”或者“或”模式
    grep命令加- E参数,这一扩展允许使用扩展模式匹配。例如,要抽取城市代码为2 1 9或2 1 6,方法如下:
    CODE: 
    [sam@chenwy sam]$ grep -E '219|216' data.f
    219 dec 2CC1999 CAD 23.00 PLV2C 68
    216 sept 3ZL1998 USP 86.00 KVM9E 234
    -perm :按照文件权限来查找文件。(用的很常见)
    按照文件权限模式用-perm选项,按文件权限模式来查找文件的话。最好使用八进制数字权限表示法,但是也可以用符号表示
    $find . -type f -perm a=rwx -exec ls -l {} ;
    或者
    $find . -type f -perm 777 -exec ls -l {} ;   //两个效果一样
    $find . -type f -perm -ug=rw -exec ls -l {} ;  //查找可以由“other”和组写入的文件
    或者
    $find . -type f -perm 220 -exec ls -l {} ;
    $find . -perm 755;在当前目录下查找文件权限为755的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件
    $find /opt/soft/test/ -perm 777;查找/opt/soft/test/目录下 权限为 777的文件
     
    /:只匹配一位权限即可
    -:文件权限全包含时才显示

    find -perm,根据文件的权限来查找文件,有三种形式,是位"与", + /是位"或"

    find -perm mode

    find -perm -mode

    find -perm /mode(+符号的作用与 / 符号相同,但是现在新版 GNU findutils 中不支持使用该符号)

    三者区别:

    在linux中文件或目录有三者权限r,w,x,代表的含义分别是读、写、可执行。而一个文件或目录的属性中又包括所属用户u、所属组g、其他o三个部分的属性,分别表示所属用户、所属组、其他用户对这个文件所拥有的权限。 
     所属用户   所属组    其他
       rwx       rwx      rwx
     -perm mode  :查找的档案属性『刚好等于』 mode的档案 
     -perm -mode :查找的档案属性『必须要全部囊括 mode 的属性』的档案,举例来说, 
                     我们要查找 -rwxr--r-- ,亦即744 的档案,使用 -perm -744, 
                     当一个档案的属性为 -rwxr-xr-x ,亦即 755时,也会被列出来, 
                     因为 -rwxr-xr-x 的属性已经囊括了 -rwxr--r-- 的属性了。 
                     所以:-mode其实就是查找比mode值大的档案
     -perm +mode :查找档案属性『包含任一 mode 的属性』的档案,举例来说,我们查找
                     -rwxr-xr-x ,亦即 -perm +755 时,但一个档案属性为 -rw------- 
                     也会被列出來,因為他有 -rw.... 的属性存在 
    [root@test test1]# ll
    总用量 0
    -rwxrwxrwx  1 root root 0  6月  9 20:30 1.txt   777
    -rwxrwxr-x  1 root root 0  6月  9 18:58 2.txt    775
    -rwxr-xrwx  1 root root 0  6月 10 00:10 3.txt   757
    -r-xrwxrwx  1 root root 0  6月 10 00:10 4.txt   577
    -r-xr-xrwx  1 root root 0  6月 10 00:10 5.txt    557
    -r-xr-xr-x  1 root root 0  6月 10 00:10 6.txt     555
    [root@test test1]# find .  -perm  700   //没有这个权限的文件
    [root@test test1]# find .  -perm  -700
    .
    ./3.txt
    ./1.txt
    ./2.txt
    [root@test test1]# find .  -perm  -712   //2.txt主要是-rwxrwxr-x中的其他用户没有w权限
    ./3.txt
    ./1.txt
    [root@test test1]# 
    find -perm 755  //也没有这个文件

    find -perm +222   //找u或者g或者o三个中,至少一个有可写权限的
    find -perm -222    //找u,g和o三个中都有可写的权限文件
    find -perm -002    //找权限至少o可以写的
    find -perm +222    u g o 有一个可写就匹配
    find -perm -222     u g o 全都可写才匹配
    find . -perm 777 查找当前及子目录下所有权限为777的文件

    -perm按文件权限查找。例如:-perm -777, -perm –a+x(user, group, other 都具有write属性)

    find plsql -type f -perm -ug=rw -exec ls -l {} ; 2>/dev/null     //将查找可由“other”和组写入的文件

    或者

    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 

    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-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

    find /etc -perm 640 精确匹配,其权限必须是640

    find /etc -perm /640三组权限中有任意一组匹配都行

    find /etc -perm -640含有该权限的都得匹配

    -perm -222 可查找出666,只要含有222权限的都可以

    -perm -400只要属主有读权限即可,其他任意权限

    -perm /400属主有读权限,其他没有任何权限;符合这三组都可

     
    find . -perm 700 是说恰好为 700, rwx------ 
    find . -perm -700 是说第一组满足 7 就可以了,后两组无所谓,因此 rwx------ 和 rwxrwxrwx 都会入选 

    find . -perm +700 是说第一组每一位有一个满足就可以了,因此 r-x------ 也会入选,范围又扩大很多  

    # find . -perm 111 -print  //表示在当前目录下搜寻所有者、同组成员及其他成员均为可执行权限的文件及文件夹
    # find . -perm -111 -print  //表示在当前目录下搜寻所有者、同组成员及其他成员均含有可执行权限的文件及文件夹
    #find . -perm /111 -print  //表示在当前目录下搜寻所有者、同组成员及其他成员中至少一个角色含有可执行权限的文件及文件夹

    + 针对 三个权限位中的任意 一位

    - 针对 三个权限位中的全部 三位

     MODE 也是针对三位的

    [root@localhost ~]# ls -lh test
    total 0
    -r--r--r-- 1 root root 0 Oct 10 20:50 test1
    -rwxr--r-- 1 root root 0 Oct 10 20:50 test2
    -rw-r--r-- 1 root root 0 Oct 10 20:50 test3
    -rw-r--r-- 1 root root 0 Oct 10 20:50 test4
    [root@localhost ~]# find test -perm 644 | xargs ls -hld
    -rw-r--r-- 1 root root 0 Oct 10 20:50 test/test3
    -rw-r--r-- 1 root root 0 Oct 10 20:50 test/test4
    [root@localhost ~]# find test -perm +644 |xargs ls -lhd
    drwxr-xr-x 2 root root 1.0K Oct 10 20:50 test    //找到这个,是因为find中没有指明查找的类型,即没有-type f的原因
    -r--r--r-- 1 root root    0 Oct 10 20:50 test/test1
    -rwxr--r-- 1 root root    0 Oct 10 20:50 test/test2
    -rw-r--r-- 1 root root    0 Oct 10 20:50 test/test3
    -rw-r--r-- 1 root root    0 Oct 10 20:50 test/test4
    [root@localhost ~]# find test -perm -644 | xargs ls -lhd
    drwxr-xr-x 2 root root 1.0K Oct 10 20:50 test
    -rwxr--r-- 1 root root    0 Oct 10 20:50 test/test2
    -rw-r--r-- 1 root root    0 Oct 10 20:50 test/test3
    -rw-r--r-- 1 root root    0 Oct 10 20:50 test/test4

    可以看到 a ab abc 的权限分别为600 640 666

    find -perm 640 是做精确匹配,只会匹配到640即 ab

    find -perm -640 做比640更充足的匹配,当然666是满足的,即 ab abc

    find -perm /640  是要任意的一组权限中1的位置上有一个符合即可,因此a ab abc 都会匹配出来

     
     
     
    -prune忽略某个目录, 使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。(使用find查找文件的时候怎么避开某个文件目录)
    eg:

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

    find /apps -path "/apps/bin" -prune -o -print;希望在/apps目录下查找文件,但不希望在/apps/bin目录下查找

    find /usr/sam -path "/usr/sam/dir1" -prune -o -print;比如要在/usr/sam目录下查找不在dir1子目录之内的所有文件


    find命令和and(多个参数选项连接符),or搭配使用

    连接符
    -a and 逻辑与 ;多参数条件查找
    -o or 逻辑或;
    $find /doc ( -name 'ja*' -o- -name 'ma*' )       //从 /doc 目录开始往下找,找寻文件名是 ja 开头或者 ma开头的文件。
    $find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" -or -name "*.h" -or -name "*.rss" ")" -print | xargs wc -l  就可以得到项目的总代码行数。
     组合测试需要用括号
    $find / ( -name "test*" -or -newer afile ) -type f -print
    $find . -name *.c -or -name *.cpp
    $find /etc -name init* -a -type f/l/d 二进制文件/软链接文件/目录;-a是默认的,一般不写 $find /etc -name inittab -o -size +204800

    b)这些选项可以多个一起用

    $find -name "t*" -perm 744;查找当前目录下文件名以t开头的,且文件属性主具有读、写、执行权限的文件。。。。

    $find /etc/ -type f -name "rc*" -exec ls -l {} ;      (find还有-exec选项,对匹配文件执行该参数过给出的shell命令。)

    $find /doc -user jacky -name 'j*'     //从 /doc 目录开始往下找,找属主为jacky 的、文件名开头是 j的文件。

    多条件查找:条件间的逻辑关系

    并关系:-a

    或关系:-o

    非关系:!或者-not

    例如:find /tmp -name "passwd" -user root(默认并关系)

    -a

    -o

    !

    避开多个文件夹

    find /usr/sam ( -path /usr/sam/dir1 -o -path /usr/sam/file1 ) -prune -o -print;圆括号表示表达式的结合。

      表示转义字符,即指示 shell 不对后面的字符作特殊解释,转义字符

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

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

    在linux find 进行查找的时候,有时候需要忽略某些目录不查找,可以使用 -prune 参数来进行过滤,但必须要注意要忽略的路径参数必须紧跟着搜索的路径之后,否则该参数无法起作用。

    以下是指定搜索/home/carryf目录下的所有文件,但是会忽略/home/carryf/astetc的路径:

    find /home/carryf -path "/home/carryf/astetc" -prune -o -type f -print

     如果按照文件名来搜索则为:

    find /home/carryf -path "/home/carryf/astetc" -prune -o -type f -name "cdr_*.conf" -print

    如果要忽略两个以上的路径如何处理?

    find /home/carryf /( -path "/home/carryf/astetc" -o -path "/home/carryf/etc" /) -prune -o -type f  -print

    find /home/carryf /( -path "/home/carryf/astetc" -o -path "/home/carryf/etc" /) -prune -o -type f  -name "cdr_*.conf" -print

    注意/( 和/) 前后都有空格。

     查找某个文件包含内容,下面这个语句可以解决目录带空格的问题:

    find ./ -name "mysql*" -print0  |xargs -0 grep "SELECT lead_id FROM vicidial_list where vendor_lead_code"

    如果目录不带空格,那么可以如下面的形式执行:

    find ./ -name "mysql*"  |xargs  grep "SELECT lead_id FROM vicidial_list where vendor_lead_code"

    -user 按照文件属主来查找文件。
    -nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在
    eg:
    $find ~ -user sam;在$HOME目录中查找文件属主为sam的文件
    $find / -type f -user qmfsun -exec ls -l {} ;     //在系统根目录下查找文件属主为qmfsun的文件
    $find /etc -user uucp;在/etc目录下查找文件属主为uucp的文件
     
    为了查找属主帐户已经被删除的文件,可以使用-nouser选项。这样就能够找到那些属主在/etc/passwd文件中没有有效帐户的文件。在使用-nouser选项时,不必给出用户名; find命令能够为你完成相应的工作。
     例如,希望在/home目录下查找所有的这类文件,可以用:
     find /home -nouser;
     
    -group 按照文件所属的组来查找文件。
    -nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
    eg: 
    $ find /apps -group gem -print;在/apps目录下查找属于gem用户组的文件
     
    要查找没有有效所属用户组的所有文件,可以使用nogroup选项。下面的find命令从文件系统的根目录处查找这样的文件
     $ find / -nogroup-print
     
    -mtime -n +n 按照文件的更改时间来查找文件,注意:单位都是以天计算的,可以使用mtime,atime或ctime选项
    -mtime:文件内容修改时间
    -atime:文件被读取或者访问时间
    -ctime:文件状态变化时间(权限等,文件的移动也是由这个时间决定)

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

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

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

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

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

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

    以上的这些时间都需要与一个值n结合使用,只需指定+n、n、-n
    - n表示文件更改时间距现在n天以内,
    + n表示文件更改时间距现在n天以前。
    n表示文件更改时间距现在恰好n天。一般不会查到结果,因为它要求完全吻合
    find命令还有-atime和-ctime 选项,但它们都和-m time选项。如果希望按照更改时间来查找文件。用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件。
    $ find / -mtime -5;在系统根目录下查找更改时间在5日以内的文件
    $ find /var/adm -mtime +3;在/var/adm目录下查找更改时间在3日以前的文件
    $ find  ./  -mtime  -1  -type f  -exec  ls -l  {} ;(查询当天修改过的文件)
    $find / -amin -10;查找在系统中最后10分钟访问的文件
    $find / -atime -2;查找在系统中最后48小时访问的文件
    $find / -empty;查找在系统中为空的文件或者文件夹
    $find / -mmin -5;查找在系统中最后5分钟里修改过的文件
    $find / -mtime -1;查找在系统中最后24小时里修改过的文件
     
     
     
       -amin n 查找系统中最后N分钟访问的文件
     
     -atime n 查找系统中最后n*24小时访问的文件
    $ find -atime -2;超找48小时内修改过的文件
     
     -cmin n 查找系统中最后N分钟被改变文件状态的文件
     -ctime n 查找系统中最后n*24小时被改变文件状态的文件
       -mmin n 查找系统中最后N分钟被改变文件数据的文件
     -mtime n 查找系统中最后n*24小时被改变文件数据的文件
     

    解释什么是atime ctime mtime

    atime (access time):最后一次访问文件的时间

    mtime(medify time):最后一次修改文件的时间

    ctime(change time):最后一次改变文件(改变的是原数据即属性)的时间

    如:记录该文件的inode节点被修改的时间。touch 命令除了-d -t选项外都会改变改时间,而且chmod,chown等命令也能改变该值

    三者之间的关系

    当修改mtime时,ctime必须随着改变,因为文件大小等属性;有人说atime 也一定会改变,要想修改文件必须先访问;其实是不对的,不必访问文件就能修改内容:如#echo "change it" >> /etc/inittab ,inittab文件内容会改变,但并没有访问文件,所以atime没有改变

    查看三者的命令

    stat filename  可以查看三者的时间值

    ls -l filename  查看文件修改时间

    ls -lc filename  查看文件状态改动时间

    ls -lu filename   查看文件访问时间

     
    -type 查找某一类型的文件,诸如:b - 块设备文件。d - 目录。c - 字符设备文件。p - 管道文件。l - 符号链接文件。f - 普通文件。
    诸如:b - 块设备文件。d - 目录。c - 字符设备文件。p - 管道文件。l - 符号链接文件。f - 普通文件
     
    $ find /etc -type d;在/etc目录下查找所有的目录 
    $ find . ! -type d;在当前目录下查找除目录以外的所有类型的文件
    $ find /etc -type l;在/etc目录下查找所有的符号链接文件
    $ find . -type f -name "*.log";查找当目录,以.log结尾的普通文件
     
    -depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
    在使用find命令时,可能希望先匹配所有的文件,再在子目录中查找。使用depth选项就可以使find命令这样做
     $ find / -name "CON.FILE" -depth;
    find命令从文件系统的根目录开始,查找一个名为CON.FILE的文件,它将首先匹配所有的文件然后再进入子目录中查找
     
     
     
     
     
     
     
     
     
     
    size n :查找满足指定大小的文件
    [c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计,带k表示千字节
    可以按照文件长度来查找文件,这里所指的文件长度既可以用块(block)来计量,也可以用字节来计量。以字节计量文件长度的表达形式为N c;以块计量文件长度只用数字表示即可。
     -size [+/-]100[c/k/M/G]: 表示文件的长度为等于[大于/小于]100块[字节/k/M/G]的文件。
        -empty: 查找空文件。
    -size 2(K|M|G)

    -size +2M大于2M的文件

    -size -1k小于1k的

    -size 2M介于2M正负1M范围内的文件

     
    在按照文件长度查找文件时,一般使用这种以字节表示的文件长度,在查看文件系统的大小,因为这时使用块来计量更容易转换。
    在当前目录下查找文件长度大于1 M字节的文件:
     
    $ find . -size +1000000c;在当前目录下查找文件长度大于1 M字节的文件
     
    $ find . -size +1000c;查找当前目录大于1K的文件
     
    $ find /home/apache -size 100c;在/home/apache目录下查找文件长度恰好为100字节的文件
     
    $ find . -size +10;在当前目录下查找长度超过10块的文件(一块等于512字节)
    $ find / -type f -name *.zip -size +100M -exec rm -i {} ;(删除大于100M的*.zip文件)
     
    find test -type f -size 0 -exec mv {} /tmp/zerobyte ;     //搜索所有零字节文件并将它们移至 /tmp/zerobyte 文件夹

    -exec 操作允许 find 在它遇到的文件上执行任何 shell 命令。大括号允许移动每个空文件

     
    4.find命令配合【-print -exec -ok】使用,完成更复杂的搜索
    这种情况在工作中用的最多
    • -print: find命令将匹配的文件输出到标准输出。
    • -exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为"command { } ; ",注意"{ }"和“;”之间的空格
    • -ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。

    作用:用户使用这一选项是为了查找到旧文件并查看,删除它或者做其他的操作

    exec和ok:

    选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个,最后是一个分号。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。任何形式的命令都可以在-exec选项中使用。

    eg:

    -prune剔除某个文件或者文件夹

    $find . –path “/DSF” –prune –o –print

    $find . ( -path “./DSF” –o “./file1” ) –prune –o –perm -444 -print

    注意:-o 是或者,以此类推也可以用-a,-a是并且的意思。可根据相应的情况用-o 或者-a

    $ 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命令将它们列出。
     
    $find . -type f -size 0 -exec mv {} /tmp/qmfsun ;        //将当前目录下所有0字节的文件都移动到/tmp/qmfsun目录下
    $find . -name "*.log" -exec mv {} .. ;
     
    $ find logs -type f -mtime +5 -exec rm { } ;(在/logs目录中查找更改时间在5日以前的文件并删除它们)
     记住:在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。
     
    在下面的例子中, find命令在当前目录中查找所有文件名以.LOG结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。
    $ find . -name "*.conf" -mtime +5 -ok rm {} ;
    < rm ... ./conf/httpd.conf > ? n
     按y键删除文件,按n键不删除。
     
    在下面的例子中我们使用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
     
    为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径
    $ find / -type f -size 0 -exec ls -l {} ;
     
    查找/var/logs目录中更改时间在7日以前的普通文件,并在删除之前询问它们;
    $ find /var/logs -type f -mtime +7 -ok rm { } ;
     
    为了查找系统中所有属于root组的文件;
    $find . -group root -exec ls -l { } ;
    -rw-r--r-- 1 root root 595 10月 31 01:09 ./fie1
     
    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
     
    5、find命令配合xargs:非常常用
    注意:xarg后面也是接需要执行的命令
    在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用
    find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。
    在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;
    而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。
    来看看xargs命令是如何同find命令一起使用的,并给出一些例子。
    下面的例子查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件
    eg:
    在当前目录中查找以file打头的文件 ,然后把结果保存到/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 777 -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  | 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 "*" | 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

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

    实例1:ls -l命令放在find命令的-exec选项中 

    命令:

    find . -type f -exec ls -l {} ;

    输出: 

    [root@localhost test]# find . -type f -exec ls -l {} ; 

    -rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log

    -rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log

    -rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log

    -rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log

    -rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log

    -rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log

    -rw-r--r-- 1 root root 25 10-28 17:02 ./log.log

    -rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt

    -rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log

    -rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log

    -rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log

    [root@localhost test]#

    说明: 

    上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。

    实例2:在目录中查找更改时间在n日以前的文件并删除它们

    命令:

    find . -type f -mtime +14 -exec rm {} ; 

    输出:

    [root@localhost test]# ll

    总计 328

    -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

    -rw-r--r-- 1 root root     33 10-28 16:54 log2013.log

    -rw-r--r-- 1 root root    127 10-28 16:51 log2014.log

    lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -> log.log

    -rw-r--r-- 1 root root     25 10-28 17:02 log.log

    -rw-r--r-- 1 root root     37 10-28 17:07 log.txt

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxrwx 2 root root   4096 10-28 14:47 test3

    drwxrwxrwx 2 root root   4096 10-28 14:47 test4

    [root@localhost test]# find . -type f -mtime +14 -exec rm {} ;

    [root@localhost test]# ll

    总计 312

    -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

    lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -> log.log

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxrwx 2 root root   4096 11-12 19:32 test3

    drwxrwxrwx 2 root root   4096 11-12 19:32 test4

    [root@localhost test]# 

    说明:

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

    实例3:在目录中查找更改时间在n日以前的文件并删除它们,在删除之前先给出提示

    命令:

    find . -name "*.log" -mtime +5 -ok rm {} ;

    输出:

    [root@localhost test]# ll

    总计 312

    -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

    lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -> log.log

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxrwx 2 root root   4096 11-12 19:32 test3

    drwxrwxrwx 2 root root   4096 11-12 19:32 test4

    [root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} ;

    < rm ... ./log_link.log > ? y

    < rm ... ./log2012.log > ? n

    [root@localhost test]# ll

    总计 312

    -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxrwx 2 root root   4096 11-12 19:32 test3

    drwxrwxrwx 2 root root   4096 11-12 19:32 test4

    [root@localhost test]#

    说明:

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

    实例4:-exec中使用grep命令

    命令:

    find /etc -name "passwd*" -exec grep "root" {} ;

    输出:

    [root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} ;

    root:x:0:0:root:/root:/bin/bash

    root:x:0:0:root:/root:/bin/bash

    [root@localhost test]#

    说明:

    任何形式的命令都可以在-exec选项中使用。  在上面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个root用户。

    实例5:查找文件移动到指定目录  

    命令:

    find . -name "*.log" -exec mv {} .. ;

    输出:

    [root@localhost test]# ll

    总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

    drwxrwxr-x 2 root root 4096 11-12 22:49 test3

    drwxrwxr-x 2 root root 4096 11-12 19:32 test4

    [root@localhost test]# cd test3/

    [root@localhost test3]# ll

    总计 304

    -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

    -rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

    -rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

    [root@localhost test3]# find . -name "*.log" -exec mv {} .. ;

    [root@localhost test3]# ll

    总计 0[root@localhost test3]# cd ..

    [root@localhost test]# ll

    总计 316

    -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

    -rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

    -rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxr-x 2 root root   4096 11-12 22:50 test3

    drwxrwxr-x 2 root root   4096 11-12 19:32 test4

    [root@localhost test]#

    实例6:用exec选项执行cp命令  

    命令:

    find . -name "*.log" -exec cp {} test3 ;

    输出:

    [root@localhost test3]# ll

    总计 0[root@localhost test3]# cd ..

    [root@localhost test]# ll

    总计 316

    -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

    -rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

    -rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxr-x 2 root root   4096 11-12 22:50 test3

    drwxrwxr-x 2 root root   4096 11-12 19:32 test4

    [root@localhost test]# find . -name "*.log" -exec cp {} test3 ;

    cp: “./test3/log2014.log” 及 “test3/log2014.log” 为同一文件

    cp: “./test3/log2013.log” 及 “test3/log2013.log” 为同一文件

    cp: “./test3/log2012.log” 及 “test3/log2012.log” 为同一文件

    [root@localhost test]# cd test3

    [root@localhost test3]# ll

    总计 304

    -rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

    -rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

    -rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

    [root@localhost test3]#

     
     

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

    命令:

    find . -type f -print | xargs file

    输出:

    [root@localhost test]# ll

    总计 312

    -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

    -rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

    -rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxrwx 2 root root   4096 11-12 19:32 test3

    drwxrwxrwx 2 root root   4096 11-12 19:32 test4

    [root@localhost test]# find . -type f -print | xargs file

    ./log2014.log: empty

    ./log2013.log: empty

    ./log2012.log: ASCII text

    [root@localhost test]#

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

    命令:

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

    输出:

    [root@localhost test]# find / -name "core" -print | xargs echo "" >/tmp/core.log

    [root@localhost test]# cd /tmp

    [root@localhost tmp]# ll

    总计 16

    -rw-r--r-- 1 root root 1524 11-12 22:29 core.log

    drwx------ 2 root root 4096 11-12 22:24 ssh-TzcZDx1766

    drwx------ 2 root root 4096 11-12 22:28 ssh-ykiRPk1815

    drwx------ 2 root root 4096 11-03 07:11 vmware-root

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

    命令:

    find . -perm -7 -print | xargs chmod o-w

    输出:

    [root@localhost test]# ll

    总计 312

    -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

    -rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

    -rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxrwx 2 root root   4096 11-12 19:32 test3

    drwxrwxrwx 2 root root   4096 11-12 19:32 test4

    [root@localhost test]# find . -perm -7 -print | xargs chmod o-w

    [root@localhost test]# ll

    总计 312

    -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

    -rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

    -rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxr-x 2 root root   4096 11-12 19:32 test3

    drwxrwxr-x 2 root root   4096 11-12 19:32 test4

    [root@localhost test]#

    说明:

    执行命令后,文件夹scf、test3和test4的权限都发生改变

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

    命令:

    find . -type f -print | xargs grep "hostname"

    输出:

    [root@localhost test]# find . -type f -print | xargs grep "hostname"

    ./log2013.log:hostnamebaidu=baidu.com

    ./log2013.log:hostnamesina=sina.com

    ./log2013.log:hostnames=true[root@localhost test]#

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

    命令:

    find . -name * -type f -print | xargs grep "hostnames"

    输出:

    [root@peida test]# find . -name * -type f -print | xargs grep "hostnames"

    ./log2013.log:hostnamesina=sina.com

    ./log2013.log:hostnames=true[root@localhost test]#

    说明:

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

    实例6:使用xargs执行mv 

    命令:

    find . -name "*.log" | xargs -i mv {} test4

    输出:

    [root@localhost test]# ll

    总计 316

    -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

    -rw-r--r-- 1 root root     61 11-12 22:44 log2013.log

    -rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxr-x 2 root root   4096 11-12 22:54 test3

    drwxrwxr-x 2 root root   4096 11-12 19:32 test4

    [root@localhost test]# cd test4/

    [root@localhost test4]# ll

    总计 0[root@localhost test4]# cd ..

    [root@localhost test]# find . -name "*.log" | xargs -i mv {} test4

    [root@localhost test]# ll

    总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

    drwxrwxr-x 2 root root 4096 11-13 05:50 test3

    drwxrwxr-x 2 root root 4096 11-13 05:50 test4

    [root@localhost test]# cd test4/

    [root@localhost test4]# ll

    总计 304

    -rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

    -rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

    -rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

    [root@localhost test4]#

    实例7:find后执行xargs提示xargs: argument line too long解决方法:

    命令:

    find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

    输出:

    [root@pd test4]#  find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

    rm -f 

    [root@pdtest4]#

    说明:

    -l1是一次处理一个;-t是处理之前打印出命令

    实例8:使用-i参数默认的前面输出用{}代替,-I参数可以指定其他代替字符,如例子中的[] 

    命令:

    输出:

    [root@localhost test]# ll

    总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

    drwxrwxr-x 2 root root 4096 11-13 05:50 test3

    drwxrwxr-x 2 root root 4096 11-13 05:50 test4

    [root@localhost test]# cd test4

    [root@localhost test4]# find . -name "file" | xargs -I [] cp [] ..

    [root@localhost test4]# ll

    总计 304

    -rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

    -rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

    -rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

    [root@localhost test4]# cd ..

    [root@localhost test]# ll

    总计 316

    -rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

    -rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

    -rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxr-x 2 root root   4096 11-13 05:50 test3

    drwxrwxr-x 2 root root   4096 11-13 05:50 test4

    [root@localhost test]#

    说明:

    使用-i参数默认的前面输出用{}代替,-I参数可以指定其他代替字符,如例子中的[] 

    实例9:xargs的-p参数的使用 

    命令:

    find . -name "*.log" | xargs -p -i mv {} ..

    输出:

    [root@localhost test3]# ll

    总计 0

    -rw-r--r-- 1 root root 0 11-13 06:06 log2015.log

    [root@localhost test3]# cd ..

    [root@localhost test]# ll

    总计 316

    -rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

    -rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

    -rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxr-x 2 root root   4096 11-13 06:06 test3

    drwxrwxr-x 2 root root   4096 11-13 05:50 test4

    [root@localhost test]# cd test3

    [root@localhost test3]#  find . -name "*.log" | xargs -p -i mv {} ..

    mv ./log2015.log .. ?...y

    [root@localhost test3]# ll

    总计 0[root@localhost test3]# cd ..

    [root@localhost test]# ll

    总计 316

    -rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

    -rw-r--r-- 1 root root     61 11-13 06:03 log2013.log

    -rw-r--r-- 1 root root      0 11-13 06:03 log2014.log

    -rw-r--r-- 1 root root      0 11-13 06:06 log2015.log

    drwxr-xr-x 6 root root   4096 10-27 01:58 scf

    drwxrwxr-x 2 root root   4096 11-13 06:08 test3

    drwxrwxr-x 2 root root   4096 11-13 05:50 test4

    [root@localhost test]#

    find命令查找实例:

    查找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"

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

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

     

    在/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

    find  /tmp  -name tmp.txt  -exec cat {} ;

    $find . -name "yao*"  | xargs file
    $find  . -name "yao*"  |  xargs  echo   "" > /tmp/core.log

    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  /etc -name "passwd*"  -exec grep  "cnscn"  {}  ;  #看是否存在cnscn用户

    Linux下find一次查找多个指定类型文件,指定文件或者排除某类文件,在 GREP 中匹配多个关键

     

     

     
    Linux下find一次查找多个指定文件或者排除某类文件,在 grep 中匹配多个关键字的方法
    (1)Linux下find一次查找多个指定文件:
    查找a.html和b.html
    1. find . -name "a.html"  -name "b.html"  

    find . -regex '.*.txt|.*.doc|.*.mp3'
    1. find . -regex '.*.txt|.*.doc|.*.mp3'  
    2. ./a.txt  
    3. ./a.doc  
    4. ./a.mp3  

    (2)排除某些文件类型:
    排除目录下所有以html结尾的文件:
    1. find . -type f ! -name "*.html"    

    1. find . -type f ! -name "*.html"       
    2. ./ge.bak.02.09  
    3. ./ge.html.changed.by.jack  
    4. ./a.txt  
    5. ./a.doc  
    6. ./a.mp3  

    (3)排除多种文件类型的示例:
    1. find . -type f ! -name "*.html" -type  f ! -name "*.php" -type  f ! -name "*.svn-base"  -type  f ! -name "*.js"  -type  f ! -name "*.gif"  -type  f ! -name "*.png"  -type  f ! -name "*.cpp"  -type  f ! -name "*.h"  -type  f ! -name "*.o"  -type  f ! -name "*.jpg"  -type  f ! -name "*.so"  -type  f ! -name "*.bak"  -type  f ! -name "*.log"   

    (3)在 GREP 中匹配多个关键字的方法:
    grep查找多个数字的文件:
    -r 递归,-E:正则  -l:只显示文件名
    1. root@116.255.139.240:~/a# grep -r -E '0341028|100081|10086|10001' *  
    2. a.txt:100081  
    3. b.txt:10086  
    4. c/cc.txt:0341028  
    5. c/cc.txt:100081  
    6. c/cc.txt:10086  
    7. c/cc.txt:10001  
    8. c.txt:10001  
    9. d.txt:0341028  

    1. grep -r  -E -l '0341028|100081|10086|10001' *     
    2. a.txt  
    3. b.txt  
    4. c/cc.txt  
    5. c.txt  
    6. d.txt  

    多种类型文件示例:
     
    1. find . -name "*.html" -o -name "*.js"|xargs grep -r "BusiTree"   


    用Awk:
     
    1. find . -name "*.php"|awk '{print "cat " $0 " |grep -H dbsys.mxxxx.justwinit.cn"}'|sh  

    find命令的复杂查找
    #find . -size -10c –print | ls –l      //在当前目录下查找100~200块长的文件并显示文件的实际块数。

    $find ./ -perm -002 -exec mv {} {}.old ;         //将查找到文件的名字加上.old(相当于重命名)

    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" {} ;

    # A='find ./ -name "*php"' | ls -l --full-time $A 2>/dev/null | grep "2004-11-30 16:36:37      //查找2004-11-30 16:36:37时更改过的文件
    $find / -name access_log 2 >/dev/null     //无错误查找,所有的错误信息都被输入到/dev/null    目录

    $ find . –print

        -print指明打印出匹配文件的文件名(路径),’ ’作为分割符。使用-print0可指明使用’’作为分割符。

        我们可根据文件名进行搜索,如搜索以.txt结尾的文件名:

    $ find . –name “*.txt” –print

        另外还有一个-iname选项,与-name选项的区别仅在于-iname选项忽略字母大小写。

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

    $ find . ( –name “*.txt” –o –name “*.pdf” ) –print

    ./new.txt

    ./new.pdf

      上面的代码打印出所有的.txt和.pdf文件。(以及)用于将它们之中的内容视为一个整体。

    选项-path(同样也有-ipath)将文件路径作为一个整体进行匹配,如:

    $ find . –path “*new*” –print

    ./new.txt

    ./new.pdf

    ./new.py

        选项-regex的参数和-path的类似,只不过-regex(同样也有-iregex)是基于正则表达式来匹配文件路径的。

    $ find . –regex “.*(.txt|.pdf)$” –print

    ./new.txt

    ./new.pdf

        find也可以用”!”否定参数的含义,如:

    $ find . ! –name “*.txt” –print

    .

    ./new.pdf

    ./new.py

        find命令在使用时会遍历所有的子目录。但可以使用-maxdepth和-mindepth来限制find命令遍历的深度。如:

    $ find . maxdepth 1 –print

    $ find . mindepth 2 –print

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

        Linux中文件具有不同的类型,例如普通文件、目录、字符设备、块设备、符号链接、硬链接、套接字以及FIFO等。Find命令中的-type可以对文件搜索进行过滤。如:

    $ find . –type d –print    #列出所有的目录

    $ find . –type f –print    #列出普通文件

    $ find . –type l –print    #列出符号链接

    $ find . –type c –print    #列出字符设备

    $ find . –type b –print    #列出块设备

    $ find . –type s –print    #列出套接字

    $ find . –type p –print    #列出FIFO

        Linux文件系统中的每一个文件都有三种时间戳:

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

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

    l  变化时间(-ctime):文件元数据(例如权限或所有权)最后一次改变的时间。

        -atime、-mtime、-ctime可作为find的时间参数,它们可以整数值给出,单位是天。这些整数值还可以带有-或+,如:

    $ find . type f –atime -7 -print        #打印最近7天被访问过的所有文件

    $ find . type f –atime 7 –print        #打印恰好在7天前被访问过的所有文件

    $ find . type f –atime +7 –print       #打印出访问时间超过7天的所有文件

        另外,还有以分钟作为计量单位的,包括:

    l  -amin(访问时间)

    l  -mmin(修改时间)

    l  -cmin(变化时间)

        find命令还有一个-newer参数,使用-newer,可以指定一个用于比较时间戳的参考文件,如:

    $ find . –type f –newer new.txt –print     #打印比file.txt修改时间更新的所有文件

        find还可以根据文件大小搜索:

    $ find . –type f –size +2k                  #大于2KB的文件

    $ find . –type f –size -2k                  #小于2KB的文件

    $ find . –type f –size 2k                   #大小等于2KB的文件

        除了k之外,还有:

    l  b——块(512字节)。

    l  C——字节。

    l  w——字(2字节)。

    l  k——千字节。

        文件匹配还可以根据文件权限进行,如:

    $ find . –type f –perm 644 –print           #打印出权限为644的文件

        还可以用该方法找出那些没有设置好权限执行权限的PHP文件:

    $ find . type f –name “*.php” ! –perm 644 –print

        也可以根据文件的所有权进行搜索,如:

    $ find . –type f –user baojie –print         #打印用户baojie拥有的所有文件

        find命令可以借助选项-exec与其他命令进行结合,如将所有.py添加可执行权限:

    $ find . –name “*.py” –exec chmod +x {} ;

        {}是一个特殊的字符串,与-exec选项结合使用。对于每一个匹配的文件,{}会被替换成响应的文件名。

        无法在-exec参数中直接使用多个命令,但可以把多个命令写到一个shell脚本中,然后再-exec中使用这个脚本。

        -exec能够同printf结合起来生成有用的输出信息。例如:

    $ find . –name “*.txt” –exec printf “Text file: %s ” {} ;

        find命令在执行搜索时还可以跳过一些子目录,如下示例:

    $ find . ( -name “old” –prune ) –o ( -type f –print )

        以上命令打印出不包括在old目录中的所有文件的名称。

    使用find命令在linux系统中查找文件时,有时需要忽略某些目录,可以使用 -prune 参数来进行过滤。 不过必须注意:要忽略的路径参数要紧跟着搜索的路径之后,否则该参数无法起作用。

    例如:指定搜索/home/zth目录下的所有文件,但是会忽略/home/zth/astetc的路径:  

    find /home/zth -path "/home/zth/astetc" -prune -o -type f -print
     

    按照文件名来搜索则为:  

    find /home/zth -path "/home/zth/astetc" -prune -o -type f -name "cdr_*.conf" -print
     

    要忽略两个以上的路径如何处理?  

    find /home/zth /( -path "/home/zth/astetc" -o -path "/home/zth/etc" /) -prune -o -type f  -print find /home/zth /( -path "/home/zth/astetc" -o -path "/home/zth/etc" /) -prune -o -type f  -name "cdr_*.conf" -print  

    注意:/( 和/) 前后都有空格。

    查找某个文件包含内容,以下语句可以解决目录带空格的问题:  

    find ./ -name "mysql*" -print0  |xargs -0 grep "SELECT lead_id FROM vicidial_list where vendor_lead_code"

    如果目录不带空格,可以这样:  

    find ./ -name "mysql*"  |xargs  grep "SELECT lead_id FROM vicidial_list where vendor_lead_code"

    通过以上的例子,大家应该可以掌握find命令查找文件时,忽略相关目录的方法了。

    如果想同时删除A和B文件则可以用-o   连接条件  
    find   -name   "*"   -o   -name   "A"   -o   -name   "B"   -newer   A   !   -newer   B   -exec   rm   -f   {}   ;  

     
  • 相关阅读:
    齐文词根词缀---1、第一讲
    元音发音规则
    英语重读规则
    英语单词重读规则
    unicode,ansi,utf-8,unicode big endian编码的区别
    Netty中粘包和拆包的解决方案
    【转载】 DirectByteBuffer内存释放
    Netty 中ChannelOption的含义以及使用的场景Netty 中ChannelOption的含义以及使用的场景
    Connection reset原因分析和解决方案
    Netty实现Socket
  • 原文地址:https://www.cnblogs.com/qmfsun/p/3811142.html
Copyright © 2011-2022 走看看