Use and summarized under linux find command

Background: find command is very useful, especially in the lookup file, this time need to be used with file wildcards.

 

1 Introduction

Why do we have to learn to use the find command?

Each operating system has thousands of files, for "everything is a file" linux operating system so it is no exception, everyone should be able to easily use the files in windows search function, but on linux it a function may not be very familiar with.

In fact, want to play linux you but also to firmly grasp this command because linux like windows that have a fixed file name suffix, and because the characteristics of the contending camps under linux, the same file in a different releases, there may be different , so if you can firmly grasp the use of the find command you in the dark on the road will be a lot of linux smoothly, and you'll find under linux file search function is very simple, and than find the windows powerful lot! Well, the following entry to the topic

2 find introduction 

Command to find files in Linux there are two: the locate and  find

First, referring to the next locate, locate the command is traversed (generated database command: it generated database updatedb) , decide to use this feature to find files quickly locate speed, but locate the file command only fuzzy matching, in the accuracy is worse, a brief introduction of the two options under it:

  #locate 

        -i // find files when not case-sensitive, such as: the locate passwd -i 

       -n // Find the results only show the first N lines such as: locate -n 5 passwd

 

The following focus on that the Find, the Find in without specifying a directory lookup is to traverse the entire system to find

 2.1 Syntax

  Using the format  :  

 find [specify lookup directory] [lookup rules] [look after the implementation of the action]

 

  [Specify lookup directory ] For example :

 

ps: to note here is to use the space between separate directory

 Find Rules 2.2

[Find Rules

    

(1) Find the file name

# -Name // Find the file name (pinpoint) 

 # -iname // Find the file name, but not case sensitive

 

Here we introduce additional knowledge at the file name wildcard

 

* Indicates a   wildcard character in any

           

 

? It represents a   single arbitrary character wildcard

 

         

[] Represents  any one wildcard character in brackets

 

     ( 2) based on user and group file belongs to locate the file

# -User // according to the owner to find the file 

 # -group // to find is a group based on file

          ( 3) According to the uid and  gid to find a user

 #find  /tmp  -uid  500  //查找uid是500 的文件

 #find  /tmp  -gid  1000 // 查找gid是1000的文件

          (4)-a  and  -o  and  –not的使用

ps:有用

# -a 连接两个不同的条件(两个条件必须同时满足)

# -o 连接两个不同的条件(两个条件满足其一即可)

# -not 对条件取反的

 

 

      

 

    (5)根据文件时间戳的相关属性来查找文件

          我们可以使用stat命令来查看一个文件的时间信息 如下:

 

      

 

复制代码
           #-atime

           #-mtime

           #-ctime

           #-amin

           #-mmin

           #-cmin
复制代码

   所以这里atime,mtime,ctime就是分别对应的“最近一次访问时间”“最近一次内容修改时间”“最近一次属性修改时间”,这里的atime的单位指的是“天”,amin的单位是分钟  

 

          #find  /tmp  –atime  +5           //表示查找在五天内没有访问过的文件(也就是5天前天没访问的文件)

          #find  /tmp  -atime  -5            //表示查找在五天内访问过的文件

          ps:在清理日志的时候,一般使用的是——最近一次内容修改的时间

find -mtime +30 -name "*[0-9]*" -exec rm {} \;

 

 

          (6)根据文件类型来查找文件

复制代码
          -type

                      f     // 普通文件

                      d     //目录文件

                      l     //链接文件

                      b     //块设备文件

                      c     //字符设备文件

                      p     //管道文件

                      s     //socket文件                
复制代码

 

         

        (7)根据大小来查找文件

               -size

          #find  /tmp  -size   2M           //查找在/tmp 目录下等于2M的文件

          #find  /tmp  -size  +2M           //查找在/tmp 目录下大于2M的文件

          #find  /tmp  -size  -2M           //查找在/tmp 目录下小于2M的文件

           (8)根据文件权限查找文件

              -perm

          #find  /tmp  -perm  755           //查找在/tmp目录下权限是755的文件

          #find  /tmp  -perm  +222          //表示只要有一类用户(属主,属组,其他)的匹配写权限就行

          #find  /tmp  -perm  -222          //表示必须所有类别用户都满足有写权限

 

           (9)-nouser  and  -nogroup

          #find  /  -nogroup –a –nouser       //在整个系统中查找既没有属主又没有属组的文件(这样的文件通常是很危险的,作为系统工程师的我们应该及时清除掉)

 

  2.3 [查找完执行的action]

复制代码
         # -print                                 //默认情况下的动作

         # -ls                                     //查找到后用ls 显示出来

         # -ok  [commend]                //查找后执行命令的时候询问用户是否要执行

        # -exec [commend]              //查找后执行命令的时候不询问用户,直接执行
复制代码

 

 

 

这里要注意{}的使用:替代查找到的文件

 

 

#删除查找到的超过30天没有访问过文件  
find  /tmp  -atime  +30  –exec rm –rf  {}  \; 

 

     我们也可以使用xargs来对查找到的文件进一步操作

    
………………………………………………………………………………………ok!     以上就是关于find的使用方法,排版有点失败,主要是字体调粗后无法调细了,悲催……但是希望各位读者看完这边博客后能有所收获…
 3 补充知识

1.删除文件命令:

find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \;

实例命令:

find /opt/soft/log/ -mtime +30 -name "*.log" -exec rm -rf {} \;

说明:

将/opt/soft/log/目录下所有30天前带".log"的文件删除。具体参数说明如下:

find:linux的查找命令,用户查找指定条件的文件;

/opt/soft/log/:想要进行清理的任意目录;

-mtime:标准语句写法;

+30:查找30天前的文件,这里用数字代表天数;

"*.log":希望查找的数据类型,"*.jpg"表示查找扩展名为jpg的所有文件,"*"表示查找所有文件,这个可以灵活运用,举一反三;

-exec:固定写法;

rm -rf:强制删除文件,包括目录;

{} \; :固定写法,一对大括号+空格+\+; 

Guess you like

Origin www.cnblogs.com/surplus/p/11441390.html