• 文件查找 locate 和 find


    locate

    locate命令依赖于一个数据库文件,系统默认每天会检索一次系统中的所有文件,然后将检索到的文件记录到数据库中;
    在执行查找时,可直接到数据库中查找记录,所以locate比find反馈更为迅速;
    在使用locate命令查找之前一般需要手动执行updatedb命令更新数据库;

    locate的定时任务定义在/etc/cron.daily/mlocate文件中。
    数据库文件为/var/lib/mlocate/mlocate.db
    手动更新数据库的命令为updatedb

    locate查找速度快,并且是模糊查找。

    常用选项:

    -i, --ignore-case: Ignore case distinctions when matching patterns. 忽略大小写。
    --regex: Interpret all PATTERNs as extended regexps. 支持扩展正则。
    

    find

    语法:

    find + 查找路径(默认为当前目录) + 查找条件 + 处理动作(默认为输出到标准输出) 
    

    1、根据文件名查找

    -name [pattern]
    -iname [pattern]: Like -name, but the match is case insensitive.
    

    2、根据文件类型查找

    -type
    
    支持的文件类型:
    
    f: regular file,普通文件
    d: directory,目录文件
    l: symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype. 符号链接文件
    b:block (buffered) special,块设备文件
    c: character (unbuffered) special,字符设备文件
    p: named pipe (FIFO),管道文件
    s: socket,套接字文件
    
    # find /dev -type b -ls     查找/dev下的所有块设备并显示信息
    # find /etc -type l -ls     查找/etc下的所有符号链接文件并显示信息
    

    3、按文件大小查找

    -size n[cwbkMG]
    
    支持的大小单位:
    
    `b'    for 512-byte blocks (this is the default if no suffix is used)
    `c'    for bytes
    `w'    for two-byte words
    `k'    for Kilobytes (units of 1024 bytes)
    `M'    for Megabytes (units of 1048576 bytes)
    `G'    for Gigabytes (units of 1073741824 bytes)
    
    参数n的制定方式:
    +n     for greater than n,
    -n     for less than n,
    n      for exactly n.
    

    4、按时间查找

    以天为单位(24hours):
    -atime n: File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.
    -ctime n: File's status was last changed n*24 hours ago.
    -mtime n: File's data was last modified n*24 hours ago.
    
    以分钟(minutes)为单位:
    -amin n: File was last accessed n minutes ago.
    -cmin n: File's status was last changed n minutes ago.
    -mmin n: File's data was last modified n minutes ago.
    
    参数n的制定方式:
    +n     for greater than n,
    -n     for less than n,
    n      for exactly n.
    

    5、根据权限查找

    -perm mode: File's  permission  bits are exactly mode (octal or symbolic). 精确匹配
    -perm -mode: All of the permission bits mode are set for the file. 每一类用户(u,g,o)的权限中的每一位(r,w,x)同时都符合条件
    -perm /mode: Any  of the permission bits mode are set for the file. 任何一类用户(u,g,o)的权限中的任何一位(r,w,x)符合条件
    
    权限为0的位置忽略,不参与匹配。
    
    # find . -perm 644 -ls        查找权限为644的文件;
    # find . -perm -022 -ls       查找g,o同时拥有w权限的文件;
    # find . -perm /222 -ls       至少有一类用户有写权限;
    # find . -perm /111 -ls       至少有一类用户有执行权限;
    # find . -perm /666 -ls       至少有一类用户有读或者写权限;
    # find . -perm /002 -ls       其他用户有写权限的文件;
    

    6、根据所属关系查找

    -user uname: File is owned by user uname (numeric user ID allowed). 查找属主为uname的文件
    -group gname: File belongs to group gname (numeric group ID allowed). 查找属组为gname的文件
    -uid n: File's numeric user ID is n. 查找属主uid为n的文件
    -gid n: File's numeric group ID is n. 查找属组gid为n的文件
    -nouser: No user corresponds to file's numeric user ID. 查找没有属主的文件
    -nogroup: No group corresponds to file's numeric group ID. 超着没有属组的文件
    

    7、逻辑操作符

    与
    expr1 expr2
    expr1 -a expr2
    expr1 -and expr2
    
    或
    expr1 -o expr2
    expr1 -or expr2
    
    非
    ! expr
    -not expr
    
    查找/var目录下属主为root,且属组为mail的所有文件或目录
    # find /var -user root -a -group mail -ls
    
    查找/usr目录下不属于root,bin或hadoop的所有文件或目录;
    # find /usr -not -user root -a -not -user bin -a -not -user hadoop -ls
    # find /usr -not (-user root -o -user bin -o -user hadoop ) -ls
    
    查找/etc目录下最近一周内其内容修改过,且属主不是root也不是hadoop用户的文件或目录;
    # find /etc -mtime -7 -a -not -user root -a not -user hadoop -ls
    # find /etc -mtime -7 -a -not ( -user root -o -user hadoop ) -ls
    
    查找当前系统上没有属主或属组,且最近一周内曾被访问过的文件或目录;
    # find / ( -nouser -o -nogroup ) -atime -7 -ls
    
    查找/etc目录下大于1M且类型为普通文件的所有文件;
    # find /etc -size +1M -type f -exec ls -lh {} ;
    
    查找/etc目录下所有用户都没有写权限的文件;
    # find /etc -not -perm /222 -ls 
    
    查找/etc目录下至少有一类用户没有执行权限的文件;
    # find /etc -not -perm -111 -ls
    
    查找/etc/init.d目录下,所有用户都有执行权限,且其他用户有写权限的所有文件;
    # find /etc/init.d/ -perm -111 -a -perm -002 -ls
    

    8、处理动作

    -print: True; print the full file name on the standard output, followed by a newline. 将查找到的文件打印到标准输出,默认的处理动作。
    -delete: Delete  files. 删除查找到的文件。
    -ls: True; list current file in ls -dils format on standard output. 按指定格式列出查找到的文件。
    -ok: command: Like -exec but ask the user first. 执行命令,但需要用户交互式确认。
    -exec command {} ; : Execute command. 直接执行命令。
    -exec command {} + : 新版find用法,内置了xargs,效果与find结合xargs使用相同。
    
    # find / nouser -a nogroup -ok chown root:root {} ; 查找没有属主和属组的所有文件,并更改为属于root;
    # find / -name "*.conf" -exec ls -l {} ; 列出系统中所有以.conf结尾的文件;
    # find / -name "*.tmp" -exec rm -rf {} ; 删除系统中所有临时文件;
    # find / -perm /002 -exec mv {} {}.danger ;
    
    # find /path -type f -exec rm '{}' ;
    # find /path -type f -exec rm '{}' +
    # find /path -type f | xargs rm -f
    
    {}:用于引用查找到的文件名称自身;
    

    find传递查找到的文件路径至后面的命令时,是先查找出所有符合条件的文件路径,并一次性传递给后面的命令;
    但是有些命令不能接受过长的参数,此时命令执行会失败。另一种方式可规避此问题:

    find | xargs COMMAND (一个一个处理,找到一个处理一个)

    xargs的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题。

    管道是实现 “将前面的标准输出作为后面的标准输入”
    xargs是实现 ”将标准输入作为命令的参数“
    
    # echo "--help" | cat    -->    cat "--help"
    # echo "--help" | xargs cat    -->    cat --help
    
    # find . -perm -7 -print | xargs chmod o-w
    # find . -type d -name "*.svn" | xargs rm -rf
    
  • 相关阅读:
    每日英语:Here's Why You Won't Finish This Article
    每日英语:'Still Out Of Work?' How To Handle Holiday Small Talk
    每日英语:How to Spend Christmas in Asia
    每日英语:Losing It at the Movies: Silly Beats Sober in China's Box Office
    每日英语:How Do iPhone Photos Impact Our Experience?
    一种使用CSS固定表头和列的方法
    一个asp.net中使用的flash展示控件
    gridview中cell的选择和编辑
    asp.net 2.0中的profile对象简介
    gridview中使用方向键标记选中行
  • 原文地址:https://www.cnblogs.com/keithtt/p/6930382.html
Copyright © 2020-2023  润新知