Linux file Lookup (locate and find)

Source: Internet
Author: User

File Find Directory

1.locate
2.find

I. Locate
    locate命令基于数据库db,所以不能实时更新    他有如下的特点:    1.查找速度快    2.是模糊查找(可能搜到了文件,也有可能搜到了目录)    3.是非实时的查找    4.由于权限的原因可能只查找到一部分的文件    locate 参数:                        -i  不区分大小写                        -n N 只列举前N个项目                        -r  支持正则表达式    locate xiaoyu                        搜索名称或路径中带有"xiaoyu"的文件

Cases:

Two. Find
    实时查找工具,通过遍历指定路径完成文件查找    有如下的工作特点:    1.查找速度略慢    2.精确查找    3.实时查找    4.可能只搜索到用户具备读取和执行权限的目录    find [查找路径] [查找条件] [处理动作]         查找路径 :制定具体目标路径,默认为当前目录         查找条件:            1.按搜索层级                    -maxdepth level 最大搜索目录深度,指定目录为第一级                    -mindepth  level 最小搜索目录深度

    2.根据文件所属组或所属人         -user UserName   查找文件所属人为指定用户(也可以为UID)的文件         -group groupName :查找文件所属组为指定组(也可以为GID)的文件

         -uid UserId              :查找属组为指定的UID的文件         -gid GroupId             :查找属组为指定的GID的文件

         -nouser                    :查找没有属主的文件         -nogroup                   :查找没有属组的文件

 3.根据文件名和inode查找         -name Name         :根据         -iname Nmae        :不区分文件名称大小写


As you can see, there are three files in the directory that contain "Xiaoyu", and when the execution is case-insensitive, only one file is displayed, and find is the exact lookup

         -inum   n                :按inode号查找         -samefile name           :相同inode号的inode文件

         -links   n                 :链接数为n的文件

Note: The Xiaoyu and Xiaoyu inode numbers are the same here, so the number of links is 2. The other number of links is 2, because there is a directory. And.. The two links

 -regex "PATTERN  :  以PATTERN匹配整个文件路径字符串,不仅仅是文件名称


Note:-regextype posix-extended indicates support for extended regular expressions

 4.根据文件类型查找     -type TYPE :             f : 普通文件             d: 目录文件             I:  符号链接文件(软链接)             s: 套接字文件             b: 块设备文件             c: 字符设备文件             p: 管道文件

Find all files in the/var directory that are subordinate to root and belong to the group Mail

5.组合条件            与:-a            或:-o            非:-not6.根据文件大小来查找              -size [option]                 num :(num-1,num]                 -num :[0,num-1]                 +num:(num,∞)

Find all files that are larger than 1M in the/etc directory and are of a normal file type

7.根据时间戳:            以天为单位:                              -atime 【num】(访问时间)                    num: 【num,num+1】                    +num:【num+1,∞】                    -num:【0,num)            -mtime (修改时间)            -ctime   (文件元数据修改时间)

Find files on the current system that are not owned by the master or group and have been visited in the last week

            以分钟为单位:            -amin 【num】(访问时间)                                    num:【num,num+1】                                    +num:【num+1,∞】                                    -num:【0,num)            -mmin (修改时间)            -cmin(文件元数据修改时间)8.  根据文件权限来查找:            -perm 【/ |  -】 MODE                MODE: 精确权限匹配                /MODE:任何一类对象中的权限中只要能一位匹配就行(或关系)                                                            当权限为大于等于5时,满足其中任意一项即可。                -MODE:每一类对象都必须同时拥有指定权限(与关系),0表示不关注                例:find -perm 755    匹配当前目录下权限仅为755的文件                             find -perm /444   三类对象只要其中一个具有r权限就行                             find -perm -444   三类对象每个都要具有r权限才可以

Find files with no write permission for all users in/etc directory

处理动作:            -print:   默认的处理动作            -ls   :   类似ls命令            -delete:   删除查找到的文件            -fls file :查找到的所有文件的长格式信息保存到指定文件中            -ok COMMAND {} \ ; 对查找到的每个文件都执行COMMAND命令,执行之前都会询问用户是否执行  

    -exec COMMAND {} \ ; 对查找到的每个文件都执行COMMAND命令,执行之前不会询问用户是否执行                           {} 用于引用查找到的文件名称自身参数替换 xargs        当文件很多时,且需要后续的一些操作,搭配管道符就会出错。xargs 可以和管道符搭配,        实现read一些命令,例如:

Linux file Lookup (locate and find)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.