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

文件查找 locate 和 find的更多相关文章

  1. 【Linux】【Shell】【Basic】文件查找locate,find

    1.locate:   1.1. 简介:依赖于事先构建好的索引库: 系统自动实现(周期性任务): 手动更新数据库(updatedb):               1.2. 工作特性:查找速度快:模糊 ...

  2. Linux文件查找与打包

    一.文件查找 locate与find是经常使用的Linux 命令,刚接触Linux时对这两个命令的使用傻傻的分不清.现在我们来对比一下两个命令到底有哪些区别. 1.1 locate locate让使用 ...

  3. vim文本编辑及文件查找应用3

    文件查找 locate,find两个命令 在文件系统上查找符合条件的文件: 实现工具:locate,find locate命令: 依赖于事先构建好的索引库,索引库可以由下边两种方式构建 系统自动实现( ...

  4. Linux文件查找实现

    文件查找 locate:非实时查找(依赖数据库的方式) find(实时查找) locate:-- 模糊搜索(不适合经常改变的文件) locate 查询系统上预建的文件索引数据库 /var/lib/ml ...

  5. Linux文件查找命令find用法整理(locate/find)

    Linux文件查找查找主要包括:locate和find 1.locate 用法简单,根据数据库查找,非实时,用法: locate FILENAME 手动更新数据库(时间可能较长) updatedb 2 ...

  6. linux文件查找-find和locate

    一.find 使用语法:find  [查找目录]  [查找规则]  [查找完后执行的action] find是根据具体目录进行搜索 1.查找目录 如果不指定查找目录,默认在当前目录下进行查找 如果需要 ...

  7. 文件查找:locate、find

    文件查找:在文件系统上查找符合条件的文件: locate, find 非实时查找(数据库查找):locate  //不是遍历系统文件,把当前系统目录下的所有文件抽取出来制作成一个索引(或者叫数据库), ...

  8. linux 文件查找,which,whereis,locate,find

    linux 文件查找,which,whereis,locate,find 一:which 主要用于查找可执行命令的所在位置: 如图,查找命令 ls的目录: 二:whereis 主要用于查找命令的帮助文 ...

  9. 查找文件which locate find

    (1)which:查找命令文件路径 which ls //命令的路径查找是根据PATH环境变量 whereis ls echo $PATH //打印PATH环境变量 (2)locate:查找任意文件 ...

随机推荐

  1. 《how tomcat works》阅读笔记 - 2 - 门面设计模式,避免强制转换

    在第二章 2.3节中 try { servlet = (Servlet) myClass.newInstance(); servlet.service((ServletRequest) request ...

  2. 云计算时代,传统企业 IT 从业者如何做好转型?

    本文来源于国外社区 DZone,作者 Dennis O'Reilly 撰写过多篇关于云计算.混合云等内容的文章,本文内容围绕云计算时代,企业纷纷上云,传统 IT 从业者如何做好转型. 本文由“数梦工场 ...

  3. 03-matplotlib-折线图

    import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates ''' 折线图,用直线段将各数 ...

  4. Hyperledger Fabric 中channel配置相关数据结构

    channel Configuration Transaction Hyperledger Fabric区块链网络中的配置存储在一个configuration-transaction的集合中,每个ch ...

  5. bash登录过程 其实还不太了解,先码后看

    在刚登录Linux时,首先启动 /etc/profile 文件,然后再启动用户目录下的 ~/.bash_profile. ~/.bash_login或 ~/.profile文件中的其中一个,执行的顺序 ...

  6. 软件功能说明书final修订

    贪吃蛇(单词版)软件功能说明书final修订 1 开发背景 “贪吃蛇”这个游戏对于80,90后的人来说是童年的记忆,可以将其说为是一个时代的经典,实现了传统贪吃蛇的游戏功能:现在人们对英语的重视程度越 ...

  7. 【CSAPP笔记】4. 汇编语言——基础知识

    程序的机器级表示 计算机能读懂是机器代码(machine code)-- 用字节序列编码的低级操作 -- 也就是0和1.编译器基于编程语言的规则.目标机器的指令集和操作系统的规则,经过一系列阶段产生机 ...

  8. Android-TCP编程

    以下是PC端代码: package com.example.sxb.myapplication;import java.io.BufferedReader;import java.io.IOExcep ...

  9. Good Time 冲刺 六

    一.今日完成任务情况 第六天 日期:2018.6.19 王怡镔:今天完善了页面,对部分不足进行改进. 于鑫宇:对界面进行完善. 胡雅馨:今天完成前端页面,并改善后端,完善项目. 黄 鹤:做完最后的打卡 ...

  10. Current request is not a multipart request

    1. 文件上传需要在form表单中添加<form enctype="multipart/form-data"> 2. SpringMVC默认是关闭fileupload功 ...