码迷,mamicode.com
首页 > 其他好文 > 详细

文本查找工具find命令

时间:2016-03-12 14:56:55      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:linux find 搜索文本

在Linux系统上操作时,有时会忘记一个文件放在哪个目录下,这是可以用到文件查找工具去查找到相应的文件,文件查找工具有locate、find两个工具

文件查找: 

      1.locate:   locate命令是通过linux上构建的一个索引数据库来查询的,遍历整个数据库去搜索所匹配的文件,而且这个索索引数据库是Linux系统自己维护和更新的,索引的构建是在系统比较空闲的时候自动进行的周期性任务。

         虽然数据库的更新是系统自动更新的,但也可以通过手动updatedb更新,但是更新的时候需要遍历整个根目录(/),所以极消耗系统资源。

        locate的特点:查找速度快,模糊查询,非实时查找     

         [root@localhost nginxlog]# locate passwd

          /etc/passwd

          /etc/passwd-

         /etc/pam.d/passwd

      .....

     locate命令虽然查找快,但是如果你创建了一个文件,索引数据还没更新,文件是查找不到的。      

          [root@localhost nginxlog]# touch mageedu

         [root@localhost nginxlog]# ls

            mageedu  nginx-1.4.7.tar.gz  test    

          [root@localhost nginxlog]# locate mageedu

          [root@localhost nginxlog]#    

         注意:有的centos6没有locate命令,可以yum -y install mlocate,然后updatedb下。

  

         2、find:   find是实时查找工具,通过遍历指定路径下的文件系统完成文件查找。

                 特点:查找速度略慢,实时查找,精确查找

                     find [OPTION]... [查找路径] [查找条件] [处理动作]

                        查找路径:指定具体的目标路径,默认为当前当前路径

                        查找条件:可以查找文件名,大小,类型,权限,文件修改时间等

                        处理动作:对符合条件的文件做处理操作;默认输出到屏幕

             2.1、查找条件:

                       根据文件名查找:-name,支持用global(通配符);-iname表示不区分大小写

                       [root@localhost nginxlog]# find /etc -name "*passwd"

                       根据属主、属组查找:-user,-group,-uid,-gid,-nouser,-nogroup

                        [root@localhost nginxlog]# find /etc -user root

                       根据文件类型去查找:

                        -type TYPE: -d,-f,-l,-s(套接字文件) ,-p(管道文件),-c,b

                        [root@localhost nginxlog]# find /etc -type f 

                        根据文件大小去查找:

                          -size [+|-]#unit:  #表示大小

                        [root@localhost nginxlog]# find /etc -size +2M 

                     注意:+2M表示大于2M的文件;2M表示是1M-2M的文件;-2M表示小于2M的文件,即0M-1M;

                      根据时间去查找:

                       -atime [+|-]#:  #表示天数       注意:这里天数是按24小时计算!

                        [root@localhost ~]# find /etc -atime 3 -type f

                       例子中3表示查找以前“第三天”的文件,如果+3表示查找“第三天“以前的文件,如果-3表示查找

                        “第三天”到现在的时间;

                        技术分享  

                         已分钟为单位:

                              -amin、mmin、cmin


                        根据权限查找:

                            -perm [-|/] mode

                              mode:表示精确匹配查找,必须按给定的权限查找文件。

                              /mode:表示只要ugo当中有一个符合就可以

                             -mode:ugo同时都必须拥有为其指定的权限,ugo中只要权限位包含所指定的权限就可以。                                        例如:

                                [root@localhost tmp]# find /tmp -perm 755 -ls

                                101319180    0 -rwxr-xr-x   1 root     root            0 Mar 12 18:01 /tmp/test.txt               

                                [root@localhost tmp]# find /tmp -perm /755 -ls 

                                100663426    0 -rw-------   1 root     root             0 Mar 11 15:49 /tmp/yum.log

                                101319179    0 -rwxrwxrwx   1 root     root            0 Mar 12 17:57 /tmp/test.xtx

                                101319180    0 -rwxr-xr-x   1 root     root            0 Mar 12 18:01 /tmp/test.txt

                                这里出现rm-也符合是因为0可以表示-或者r或者w或者x。                                

                                [root@localhost tmp]# find /tmp -perm -755 -ls

                                101319179    0 -rwxrwxrwx   1 root     root            0 Mar 12 17:57 /tmp/test.xtx

                                101319180    0 -rwxr-xr-x   1 root     root            0 Mar 12 18:01 /tmp/test.txt

                                r-x是包含在rwx当中的

                          举例:

                                   找出/etc目录下都没有写权限的文件或目录;

                                   [root@localhost tmp]# find /etc -not -perm /222 -ls                                  

                                   33852189    4 -r--r--r--   1 root     root           33 Mar 11 15:52 /etc/machine-id

                                   34203544    4 ----------   1 root     root          370 Mar 11 15:58 /etc/gshadow

                                   34203344    4 ----------   1 root     root          717 Mar 11 15:58 /etc/shadow

                                   只要把ugo当中任何一个有写权限的都找出来,然后执行取反操作。

                                   找出/etc目录下至少有一部分没有执行权限的文件或目录;                  

                                   [root@localhost tmp]# find /etc -not -perm -111 -ls

                                   33554562    4 -rw-r--r--   1 root     root          465 Mar 11 15:49 /etc/fstab

                                   33554563    0 -rw-------   1 root     root            0 Mar 11 15:49 /etc/crypttab

                                   34323086    4 -rw-r--r--   1 root     root           70 Mar 12 17:13 /etc/resolv.conf

                                   只要把ugo当中都有执行权限的用户找出,然后执行取反操作。

                                   找出/tmp目录下都有执行权限,且其它用户有写权限的文件或目录;                                  

                                   [root@localhost tmp]# find   -perm -113 -ls                                    

                                   101319179    0 -rwxrwxrwx   1 root     root            0 Mar 12 17:57 /tmp/test.xtx

                                   101319180    0 -rwxr-xrwx   1 root     root            0 Mar 12 18:01 /tmp/test.txt

                       

                         find命令中的组合用法:

                                                    与:-a

    或:-o

    非:-not, !            注意:优先级是或和与>非的,-o和-a>-not

 !A -a !B = !(A -o B)

 !A -o !B = !(A -a B)   

找出/tmp目录下,属主不是root,且文件名不是fstab的文件;两种写法:

[root@localhost tmp]#  find /tmp \( -not -user root -a -not -name ‘fstab‘ \) -ls

[root@localhost tmp]#  find /tmp -not \( -user root -o -name ‘fstab‘ \) -ls

                           

                   find的处理动作:                           

                                        -print:默认的处理动作,显示至屏幕;

-ls:类似于对查找到的文件执行“ls -l”命令;

-delete:删除查找到的文件;

-fls /path/to/somefile:查找到的所有文件的长格式信息保存至指定文件中;

-ok COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令;

对于每个文件执行命令之前,都会交互式要求用户确认;

-exec COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令; 

 {}: 用于引用查找到的文件名称自身;

                           注意:find传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令;

            有些命令不能接受过多参数,此时命令执行可能会失败;另一种方式可规避此问题:

                                     find |xargs COMMADN

                           例子:                                                    

                                   1、[root@localhost tmp]# find /tmp   -name ‘test*‘ -print

                                        /tmp/test.xtx

                                        /tmp/test.txt                                   

                                  2、 [root@localhost tmp]# find /tmp   -name ‘test*‘ -ls

                                       101319179    0 -rwxrwxrwx   1 root     root            0 Mar 12 17:57 /tmp/test.xtx

                                       101319180    0 -rwxr-xrwx   1 lanxt    lanxt           0 Mar 12 18:01 /tmp/test.tx                                

                                  3、 [root@localhost tmp]# find /tmp   -name ‘test*‘ -delete

                                       [root@localhost tmp]# ll

                                        total 4

                                       -rwx------. 1 root root 813 Mar 11 15:59 ks-script-_JF4cv

                                        -rw-------. 1 root root   0 Mar 11 15:49 yum.log

                                   4、[root@localhost ~]# find /tmp -name ‘t*‘ -fls /root/rm.log

                                        [root@localhost ~]# cat /root/rm.log 

                                        101319179    0 -rw-r--r--   1 root     root            0 Mar 12 19:14 /tmp/test

                                        101319180    0 -rw-r--r--   1 root     root            0 Mar 12 19:14 /tmp/txt                                   

                                   5、[root@localhost ~]# find /tmp -name ‘t*‘ -ok rm {}  \;

                                         < {} ... /tmp/test > ?                                    

                                   6、[root@localhost tmp]# find /tmp -name ‘t*‘ -type f  -exec rm {}   \;

                                         [root@localhost tmp]# ll

                                         -rwx------. 1 root root 813 Mar 11 15:59 ks-script-_JF4cv

                                         -rw-------. 1 root root   0 Mar 11 15:49 yum.log

                                  7、 [root@localhost tmp]# find /tmp -name "test*" -o -user root -o -user lanxt -ls

                                        [root@localhost tmp]#                                           

                    [root@localhost tmp]# find /tmp -name "test*" -o -user root -o -user lanxt |xargs ls -l

                                          -rw-r--r--. 1 root  root    0 Mar 12 19:26 /tmp/test

                                          -rw-r--r--. 1 lanxt root    0 Mar 12 19:26 /tmp/test1

                                          -rw-r--r--. 1 root  root    0 Mar 12 19:26 /tmp/test2


                    练习:用脚本实现,每天晚上4点半,找出/nginxlog下的格式为“nginx_testXX.log”的日志,如果该日志大于2M,则将其删除,删除后,把删除的文件名记录到/tmp/rm.log。

                            每天晚上4点半:可以用crontab定时计划实现

                              30 4 * * * 

                            找出大于2M的日志记录到/tmp/rm.log下:

                             find /nginxlog -name  "nginx*.log" -size +2M  &>/tmp/rm.log  或者

                             find /nginxlog -name  ‘nginx*.log‘ -size +2M  -fls /tmp/rm.log  或者

                             find /nginxlog -name  ‘nginx*.log‘ -size +2M  |tee /tmp/rm.log

                             然后将其删除:

                             cat /tmp/rm.log |xargs rm

                             cat /tmp/rm.log |grep -o ‘/[^[:space:]]\+‘ |xargs rm

                             find /nginxlog -name  ‘nginx*.log‘ -size +2M  |tee /tmp/rm.log |xargs rm

                        注意:如果是在工作中,必须先记录,在执行操作


    


 




                                 

                         



   






                                    

                                    


                             

                             

                        

                    

                   

                       

                             

                              

           

                


            

 



文本查找工具find命令

标签:linux find 搜索文本

原文地址:http://lanxianting.blog.51cto.com/7394580/1750193

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!