locate及find查找命令
在文件系统上查找符合条件的文件:
实现工具:locate,find
locate:
依赖于事先构建好的索引库:
系统自动实现(周期性任务);
手动更新数据库(updatedb)
工作特性:
查找速度;
模糊查找;
非实时查找;
yum install -y mlocate
locate [OPTION]... PATTERN...
命令选项:
-b:只匹配路径中的基名;
path:
basename
dirname
-c:统计出共有多少个符合条件的文件
-r:BRE
注意:索引构建过程(updatedb)需要遍历整个跟文件系统,极消耗资源;
find:
实时查询工具,通过遍历指定起始路径下文件系统层级结构完成文件查找:
工作特性:
查找速度略慢;
精确查找;
实时查找;
用法:
find [OPTION] [查找起始路劲] [查找条件] [处理动作]
查找起始路劲:指定具体搜索目标起始路劲,默认当前目录;
查找条件:特定的查找标准,可以根据文件名、大小、类型、从属关系、权限等等进行,默认为找出指定路径下的所有文件;
处理动作:对符合查找条件作出的操作,例如删除等操作,默认为输出至标准输出;
查找条件:
表达式:选项和测试
测试:结果通常为布尔型(真or假)
文件名查找
-name;
-iname;忽略大小写
通配符?*
-regex:基于正则表达式模式查找文件,匹配是整个路径,而非其名;
//创建文件
[root@localhost ~]# touch /etc/sysconfig/network-scripts/{ifcfg-eth0,IFCFG-ETH0} //查找/etc目录下宝行ifcfg-eth0名称的文件
[root@localhost ~]# find /etc/ -name 'ifcfg-eth0'
/etc/sysconfig/network-scripts/ifcfg-eth0 //忽略大小写
[root@localhost ~]# find /etc/ -iname 'ifcfg-eth0'
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/IFCFG-ETH0 //查找/etc目录下包含ifcfg-名称的所有文件
[root@localhost ~]# find /etc/ -iname 'ifcfg-*'
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-ens33
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/IFCFG-ETH0
文件大小查找
-size [+|-] #UNIT
常用单位:k M G
#UNIT:(#-1,#)
5M:(4M,5M)
-#UNIT:(0,#-1)
-5M:(0,4M)
+#UNIT:(,OO)
+5M:(5,OO)
//查找大于5M
[root@localhost ~]# find /etc/ -size +5M //创建等于5M的文件
[root@localhost ~]# dd </dev/zero >/etc/test bs=1M count= //查找等于5M
[root@localhost ~]# find /etc/ -size 5M //查找小于5M
[root@localhost ~]# find /etc/ -size -5M
[root@localhost ~]# find /etc/ -size 5M -ls
-rw-r--r-- root root 12月 : /etc/test
[root@localhost ~]# find /etc/ -size 5M | xargs ls -l
-rw-r--r-- root root 12月 : /etc/test
时间戳查找
以“天”为单位:
-atime [+|-]:access time
文件访问时间,从CentOS/RHEL6,atime不会随着我们的每次访问而自动更改
更新周期:86400(秒)
修改文件内容
touch
#:find /etc -atime 7 之前第七天的文件
-#:find /etc -atime -7 七天内的文件
+#:find /etc -atime +7 七天之前的文件
-mtime:
最近更改时间
-ctime
最近改动时间
以“分钟”为单位:
-amin
-mmin
-cmin
//创建测试文件
[root@localhost ~]# for i in {..};do date -s $i && touch file-$i;done //查找7天以前的文件
[root@localhost ~]# find ./ -iname "file-*" -mtime + //查找最近7天的文件,不建议使用
[root@localhost ~]# find ./ -iname "file-*" -mtime - //查找之前第七天文件
[root@localhost ~]# find ./ -iname "file-*" -mtime
文件从属关系查找
-user:查找属主指定用户的所有文件;
-group:查找属主指定组的所有文件;
-uid:查找属主指定UID的所有文件;
-gid:查找属组指定GID的所有文件;
-nouser:查找没有属主的文件;
-nogroup:查找没有属组的文件;
//查找属主是jack
[root@localhost ~]# find /home -user jack //查找属组是admin
[root@localhost ~]# find /home -group admin //查找没有属主
[root@localhost ~]# find /home/ -nouser //查找没有属组
[root@localhost ~]# find /home/ -nogroup
文件类型查找
-type
f:普通文件
d:目录文件
l:符号链接文件
b:块设备文件
c:字符设备文件
p:管道文件
s:套接字文件
//文件f
[root@localhost ~]# find /dev -type f //目录d
[root@localhost ~]# find /dev -type d //链接l
[root@localhost ~]# find /dev -type l //块设备b
[root@localhost ~]# find /dev -type b //字符设备c
[root@localhost ~]# find /dev -type c //套接字s
[root@localhost ~]# find /dev -type s //管道文件p
[root@localhost ~]# find /run -type p
权限查找
-perm [/|-] mode
mode:精确权限匹配
/mode:任何一类用户(u,g,o)的权限中的任何一位(r,w,x)符号条件即满足;
9位权限之间存在“或”关系;
-mode:每一类用户(u,g,o)的权限中的每一位(r,w,x)同时符号条件即满足;
9位权限之间存在“与”关系;
//完全匹配644权限
[root@localhost ~]# find . -perm -ls //拥有者至少有021(-wx),组020(-w-),其他人400(r--)
[root@localhost ~]# find /home -perm - //拥有者至少有r,或者组成员至少有r,或者其他人至少有w权限
[root@localhost ~]# find /home -perm / //查找全局可写
[root@localhost ~]# find . -perm - -ls //包含uid
[root@localhost ~]# find /usr/sbin/ -perm - -ls
[root@localhost ~]# find /usr/sbin/ -perm - &>/dev/null
//包含sid
[root@localhost ~]# find /usr/sbin/ -perm - -ls //包含sticky
[root@localhost ~]# find /usr/sbin/ -perm - -ls
组合查找
与:-a(默认)
或:-o
非:-not,!
!A -a !B=!(A -o B)
!A -o !B=!(A -a B)
//查找属主是oldboy,属组是admin
[root@localhost ~]# find . -user oldboy -group admin //查找属主是oldboy,并且属组是admin
[root@localhost ~]# find . -user oldboy -a -group admin //查找属主是oldboy,或者属组是admin
[root@localhost ~]# find . -user oldboy -o -group admin //找出/tmp/test目录下属主为非root的所有文件
[root@localhost test]# find . -not -user root -ls
[root@localhost test]# find . ! -user root -ls //查找没有属组或属组
[root@localhost test]# find . -nouser -o -nogroup //找出/tmp/test目录下文件名不包含fastab字符串的文件
[root@localhost test]# find . -not -iname "*fastab*" -ls //找出/tmp目录下属主为非root,而且文件名不包含fstab字符串的文件
[root@localhost test]# find . -not -user root -a -not -iname "*fstab*" -ls
[root@localhost test]# find . -not \( -user root -o -iname "*fstab*" \) -ls
处理动作
当查找到一个文件后,需要对文件进行如何处理
-print:输出至标准输出,默认的动作;
-ls:类似于对查找的文件执行“ls -l”命令,输出文件的详细信息;
-delete:删除查找到的文件;
-fls /PATH/TO/SOMEFILE:把查找到的所有文件的长格式信息保存至指定文件中;
-ok COMMAND {} \;:对查找的每个文件执行由COMMAND表示的命令,每次操作都由用户进行确认;
-exec COMMAND {} \;: 对查找的每个文件执行由COMMAND表示的命令,不需确认;
//打印查询到的文件
[root@localhost ~]# find /etc/ -name "ifcfg*"
[root@localhost ~]# find /etc/ -name "ifcfg*" -ls
[root@localhost ~]# find /etc/ -name "ifcfg*" -print //拷贝文件
[root@localhost ~]# find /etc/ -name "ifcfg*" -exec cp -rvf {} /tmp \;
"/etc/sysconfig/network-scripts/ifcfg-lo" -> "/tmp/ifcfg-lo"
"/etc/sysconfig/network-scripts/ifcfg-ens33" -> "/tmp/ifcfg-ens33"
"/etc/sysconfig/network-scripts/ifcfg-eth0" -> "/tmp/ifcfg-eth0" //ok会不断提示
[root@localhost ~]# find /etc/ -name "ifcfg*" -ok cp -rvf {} /tmp \;
< cp ... /etc/sysconfig/network-scripts/ifcfg-lo > ? y
"/etc/sysconfig/network-scripts/ifcfg-lo" -> "/tmp/ifcfg-lo"
< cp ... /etc/sysconfig/network-scripts/ifcfg-ens33 > ? y
"/etc/sysconfig/network-scripts/ifcfg-ens33" -> "/tmp/ifcfg-ens33"
< cp ... /etc/sysconfig/network-scripts/ifcfg-eth0 > ? y
"/etc/sysconfig/network-scripts/ifcfg-eth0" -> "/tmp/ifcfg-eth0" //删除文件
[root@localhost ~]# find /etc/ -name "ifcfg*" -exec rm -rf {} \;
[root@localhost ~]# find /etc/ -name "ifcfg*" -delete //本地文件保留最近7天的备份,备份服务器保留三个月
find /backup -iname "*.bak" -mtime + -delete
find /backup -iname "*.bak" -mtime + -delete //给查找到的文件重命名
[root@localhost ~]# find . -name "*txt" -exec mv {} {}.bak \;
注意:find传递查找到的文件路径至后面的命令是,是先查找出所有符合条件的文件路径,并一次性传递给后面的命令,但有些命令不能接受过长的参数,
此时命令执行会失效,另一种方式能规避此问题。
find | xargs COMMAND
练习:
、查找/var目录下属主为root,且属组为mail的所有文件和目录;
[root@localhost ~]# find /var -user root -a -group mail -ls
drwxrwxr-x root mail 12月 : /var/spool/mail
drwx------ root mail 12月 : /var/spool/mqueue 、查找/usr目录下不属于root,bin或hadoop用户的所有文件和目录,用两种方法;
[root@localhost ~]# find /usr ! -user root -o ! -user bin -o ! -user hadoop
[root@localhost ~]# find /usr ! \( -user root -a -user bin -a -user hadoop \) 、查找/etc目录下最近一周内其内容修改过,且属主不是root用户也不是hadoop用户的文件或目录;
[root@localhost ~]# find /etc -mtime - -a ! -user root -a ! -user hadoop 、查找当前系统上没有属主或属组,且最近一周内曾被访问过的文件或目录;
[root@localhost tmp]# find / \( -nouser -o -nogroup \) -atime - -ls 、查找/etc目录下大于1M且类型为普通文件的所有文件;
[root@localhost ~]# find /etc/ -size +1M -a -type f -ls
[root@localhost ~]# find /etc/ -size +1M -a -type f -exec ls -lh {} \; 、查找/etc目录下所有用户都没有写权限的文件;
[root@localhost tmp]# find /etc -not -perm / -a -type f -exec ls -lh {} \; 、查找/etc目录下至少有一类用户没有执行权限的文件;
[root@localhost tmp]# find /etc -not -perm - -a -type f -exec ls -lh {} \; 、查找/etc/init.d目录下,所有用户都有执行权限,且其他用户有写权限的所有文件
[root@localhost tmp]# find /etc/init.d/ -perm - -type f -ls
locate及find查找命令的更多相关文章
- locate linux文件查找命令
locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...
- Linux下的五个查找命令:grep、find、locate、whereis、which
原文转自 http://www.cnblogs.com/wanqieddy/archive/2011/07/15/2107071.html 1.grep grep(General Regular Ex ...
- Linux的五个查找命令(find、locate、whereis、which、type)
1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...
- Linux的五个查找命令:find,locate,whereis,which,type
使用电脑的时候,经常需要查找文件. 在Linux中,有很多方法可以做到这一点.国外网站LinuxHaxor总结了五条命令,你可以看看自己知道几条.大多数程序员,可能经常使用其中的2到3条,对这5条命令 ...
- linux中查找命令find、locate、whereis、which、type区别
linux中查找命令find.locate.whereis.which.type区别 1. find Java代码 find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件.与查询数据库(/ ...
- Linux的五个查找命令find,locate,whereis,which,type
Linux的五个查找命令 1. find 最常见且最强大的命令,可以查找任何文件. 格式 $ find 指定目录 指定条件 指定动作 指定目录: 所要搜索的目录及其子目录,默认当前目录 ...
- 查找命令which、whereis、locate
1.find 最常用和最强大的查找命令.它能做到实时查找,精确查找,但速度慢. find的使用格式如下: $ find [指定目录] [指定条件] [指定动作] 指定目录:是指所要搜索的目录和其子 ...
- Linux下4个查找命令which、whereis、locate、find的总结
(1)which [-a] cmdname1 cmdname2 ...... 作用:locate a command,从环境变量PATH中,定位/返回与指定名字相匹配的可执行文件所在的路径 ...
- 【转】Linux的五个查找命令:find,locate,whereis,which,type
原文网址:http://www.ruanyifeng.com/blog/2009/10/5_ways_to_search_for_files_using_the_terminal.html 最近,我在 ...
随机推荐
- idea 2019.3 最新破解补丁和激活码,可破解至2089年!
链接:https://blog.csdn.net/qq_42914528/article/details/85617901 上面方法失效了,请尝试以下方式激活(2020.1.6更新) idea激活码( ...
- Corporative Network (有n个节点,然后执行I u,v(把u的父节点设为v)和E u(询问u到根节点的距离))并查集
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- Tomcat server.xml常用配置 含有外带文件及默认host
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE server-xml [<!ENTITY ...
- 2019 Python工程师平均薪资22K,又涨了11.9%!
转行的同学最关心的就是Python的薪资情况了.今天我们就来具体看一下Python现在的薪资情况. 这是一张网友统计的Python工程师工资情况图表.详细说明了现在Python工程师在各个城市的薪资情 ...
- LeetCode——739. 每日温度
根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperatures = ...
- Ubuntu---Git
本篇文章简单总结了常用 Git 的使用 前言 设置用户信息 1, Git 是分布式的 SSH 代码管理工具,远程的代码管理是基于 SSH 的,所以要使用远程的 Git 则需要 SSH 的配置. ste ...
- c语言中多维数组和指针的关系
如图: 执行结果: 说明:由执行结果可知,三个输出的结果相等(可能在不同的平台执行结果不相同,但三个的结果是相等的),数组multi的地址与数组multi[0]的地址相同,都等于存储的第一个整数的地址 ...
- UML-领域模型-定义
领域模型是OO分析中最重要和经典的模型(用例是重要的需求分析制品,但不是面向对象的).领域模型也是重点. 1.关系 2.例子 3.定义 领域模型没有定义方法的类图.只包括: 1).领域对象或概念类 2 ...
- C盘满了解决办法之pagefile.sys文件
pagefile.sys文件一般存在于C盘,只有点击了隐藏属性才能看见. 这个文件一般比较大,它是系统创建虚拟内存页面的文件.平时大家使用软件的时候对于产生大量的临时数据,这些数据需要占用大量内存,如 ...
- Python执行JS -- PyExecJS库
pip install PyExecJS 查看执行JS的环境 print(execjs.get().name) 返回值:JScript windows 默认执行JS的环境 返回值:Node.js (V ...