文件查找 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
文件查找 locate 和 find的更多相关文章
- 【Linux】【Shell】【Basic】文件查找locate,find
1.locate: 1.1. 简介:依赖于事先构建好的索引库: 系统自动实现(周期性任务): 手动更新数据库(updatedb): 1.2. 工作特性:查找速度快:模糊 ...
- Linux文件查找与打包
一.文件查找 locate与find是经常使用的Linux 命令,刚接触Linux时对这两个命令的使用傻傻的分不清.现在我们来对比一下两个命令到底有哪些区别. 1.1 locate locate让使用 ...
- vim文本编辑及文件查找应用3
文件查找 locate,find两个命令 在文件系统上查找符合条件的文件: 实现工具:locate,find locate命令: 依赖于事先构建好的索引库,索引库可以由下边两种方式构建 系统自动实现( ...
- Linux文件查找实现
文件查找 locate:非实时查找(依赖数据库的方式) find(实时查找) locate:-- 模糊搜索(不适合经常改变的文件) locate 查询系统上预建的文件索引数据库 /var/lib/ml ...
- Linux文件查找命令find用法整理(locate/find)
Linux文件查找查找主要包括:locate和find 1.locate 用法简单,根据数据库查找,非实时,用法: locate FILENAME 手动更新数据库(时间可能较长) updatedb 2 ...
- linux文件查找-find和locate
一.find 使用语法:find [查找目录] [查找规则] [查找完后执行的action] find是根据具体目录进行搜索 1.查找目录 如果不指定查找目录,默认在当前目录下进行查找 如果需要 ...
- 文件查找:locate、find
文件查找:在文件系统上查找符合条件的文件: locate, find 非实时查找(数据库查找):locate //不是遍历系统文件,把当前系统目录下的所有文件抽取出来制作成一个索引(或者叫数据库), ...
- linux 文件查找,which,whereis,locate,find
linux 文件查找,which,whereis,locate,find 一:which 主要用于查找可执行命令的所在位置: 如图,查找命令 ls的目录: 二:whereis 主要用于查找命令的帮助文 ...
- 查找文件which locate find
(1)which:查找命令文件路径 which ls //命令的路径查找是根据PATH环境变量 whereis ls echo $PATH //打印PATH环境变量 (2)locate:查找任意文件 ...
随机推荐
- 前端基础之CSS(总结)
css学什么?? 主要学习选择器和属性,选择器是去找到标签的位置,属性是给标签增加需要的样式. CSS选择器 1.基本选择器: 1.标签选择器 2.ID选择器 3.类选择器(class="c ...
- SICP读书笔记 3.4
SICP CONCLUSION 让我们举起杯,祝福那些将他们的思想镶嵌在重重括号之间的Lisp程序员 ! 祝我能够突破层层代码,找到住在里计算机的神灵! 目录 1. 构造过程抽象 2. 构造数据抽象 ...
- 关于Maven的一点理解
maven是一个项目管理工具,主要作用是: 1.依赖管理(jar包,工程之间); 2.统一开发规范和工具.完成项目的一步构建 3.工程聚合.继承.依赖 其核心配置文件就是pom.xml:pom即Pro ...
- 03-matplotlib-折线图
import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates ''' 折线图,用直线段将各数 ...
- dvwa——命令注入&文件包含
命令注入 commond_injection 源码.分析.payload: low: <?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input ...
- which命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/jkin/p/10289085.html Linux which命令用于查找文件. which指令会在环境变量$PATH ...
- 关于linux下的命令
1.文件和目录操作命令 pwd:显示当前的工作目录 cd:切换目录 tree:以树形结构图显示目录下的所有内容 mkdir:创建目录 touch:创建空文件或改变文件的时间戳属性 ls:显示目录下的内 ...
- django_models_Meta字段详解
Django模型类的Meta是一个内部类,它用于定义一些Django模型类的行为特性.而可用的选项大致包含以下几类 abstract 这个属性是定义当前的模型是不是一个抽象类.所谓抽象类是不会对应数据 ...
- 20162325 金立清 S2 W3 C13
20162325 2017-2018-2 <程序设计与数据结构>第3周学习总结 教材学习内容概要 查找是在一组项内找到指定目标或是确定目标不存在的过程 高效的查找使得比较的次数最少 Com ...
- Chapter 3 软件项目管理
软件项目具有产品的不可见性.项目的高度不确定性.软件过程的多变化性.软件人员的高流动性的显著特征.有效的软件项目管理集中于人员.产品.过程和项目四个方面.软件项目的生命周期有项目启动.项目规划.项目实 ...