Linux文件查找find和locate
目
录
locate文件查找
概述
安装
yum install -y mlocate
locate
-c:符合条件的文件数量
-b:基名查找(按文件名称关键字查找,而不是文件路径关键字)
locate依赖于事先构建好的索引库
系统自动实现(周期性)
手动更新数据库(updatedb)
locate文件查找的特性
1、查找速度快
2、模糊查找
3、不是实时更新
文件查找概述
Linux系统中的find命令在查找文件是非常有用而且方便
他可以根据不同条件来进行查找文件:例如权限,拥有者,修改时间,文件大小等等。同时find是linux下必须掌握的。
find命令的基本语法如下:
命令 |
选项 |
路径 |
表达式 |
动作 |
find |
[options] |
[path] |
[expression] |
[action] |
文件名查找
-name:区分大小写
-iname:忽略大小写
通配符:*和? *:匹配多个任意字符 ?:匹配一个任意字符
-regex:基本正则表达式模糊查找文件,匹配是整个路径,而非其名;
区分大小写
[root@oldboy pubilc]# find /pubilc/ -name "ifcfg-ens33"
/pubilc/ifcfg-ens33
不区分大小写
[root@oldboy pubilc]# find /pubilc/ -iname "ifcfg-ens33"
/pubilc/ifcfg-ens33
/pubilc/IFCFG-ENS33
文件大小查找
-size[+|-] #UNIT
常用单位:k、m、g
查找大于10m的文件
[root@oldboy pubilc]# find /pubilc/ -size +10M -ls
33587283 15360 -rw-r--r-- 1 root root 15728640 8月 23 16:22 /pubilc/file3.txt
33587284 12288 -rw-r--r-- 1 root root 12582912 8月 23 16:23 /pubilc/file2.txt
查找等于10M的文件
[root@oldboy pubilc]# find /pubilc/ -size 10M -ls
33587282 10240 -rw-r--r-- 1 root root 10485760 8月 23 16:21 /pubilc/file1.txt
查找小于10M的文件
[root@oldboy pubilc]# find /pubilc/ -size -10M -ls
33587264 0 drwxr-xr-x 4 root root 111 8月 23 16:22 /pubilc/
51095307 0 drwxr-xr-x 2 root root 6 8月 23 16:11 /pubilc/ifcfg-ens33
619630 0 drwxr-xr-x 2 root root 6 8月 23 16:11 /pubilc/IFCFG-ENS33
33587281 6144 -rw-r--r-- 1 root root 6291456 8月 23 16:21 /pubilc/file.txt
时间戳查找
以"天"为单位
-atime[-|+]#
#:之前第#天的文件
-#:七天内的文件
+#:七天之前的文件
以分钟为单位:
-amin:
-mmin
-cmin:
创建文件测试文件
[root@oldboy pubilc]# for i in {01..31};do date -s 201808$i && touch file-$i;done
查找20以前的文件
[root@oldboy pubilc]# find /pubilc/ -atime +20 -ls
33587281 0 -rw-r--r-- 1 root root 0 8月 1 00:00 /pubilc/file-01
33587282 0 -rw-r--r-- 1 root root 0 8月 2 00:00 /pubilc/file-02
33587283 0 -rw-r--r-- 1 root root 0 8月 3 00:00 /pubilc/file-03
33587284 0 -rw-r--r-- 1 root root 0 8月 4 00:00 /pubilc/file-04
查找5天以内的文件
[root@oldboy pubilc]# find /pubilc/ -atime -5 -ls
33587264 4 drwxr-xr-x 2 root root 4096 8月 31 00:00 /pubilc/
33588261 0 -rw-r--r-- 1 root root 0 8月 27 00:00 /pubilc/file-27
33588262 0 -rw-r--r-- 1 root root 0 8月 28 00:00 /pubilc/file-28
33588263 0 -rw-r--r-- 1 root root 0 8月 29 00:00 /pubilc/file-29
33588264 0 -rw-r--r-- 1 root root 0 8月 30 00:00 /pubilc/file-30
33588265 0 -rw-r--r-- 1 root root 0 8月 31 00:00 /pubilc/file-31
查找前5天,当天的文件
[root@oldboy pubilc]# find /pubilc/ -atime 5 -ls
33588260 0 -rw-r--r-- 1 root root 0 8月 26 00:00 /pubilc/file-26
文件从属关系查找
-user:查找属主指定用户的所有文件;
-group:查找属组指定组的所有文件;
-uid:查找属主指定UID的所有文件
-gid:查找属组指定的GID的所有文件
-nouser:查找没有属主的文件;
nogroup:查找没有属组的文件;
查找属主是oldboy的文件
[root@oldboy pubilc]# find / -user oldboy 2>/dev/null
28341 4 -rw-rw-r-- 1 oldboy oldboy 445 8月 22 21:11 /home/oldboy/2.txt
619618 0 -rw-rw-r-- 1 oldboy oldboy 0 8月 22 21:21 /home/oldboy/ab
451578 4 -rw-rw-r-- 1 oldboy oldboy 93 8月 22 21:32 /home/oldboy/ping.sh
451575 4 -rw------- 1 oldboy oldboy 2427 8月 22 21:32 /home/oldboy/.viminfo
查找属组是oldboy的文件
[root@oldboy pubilc]# find / -group oldboy -ls
28318 12 -rw------- 1 oldboy oldboy 12288 8月 22 10:01 /home/oldboy/.123.swp
28319 0 -rw-rw-r-- 1 oldboy oldboy 0 8月 22 21:21 /home/oldboy/1.txt
28341 4 -rw-rw-r-- 1 oldboy oldboy 445 8月 22 21:11 /home/oldboy/2.txt
查找UID是的文件
[root@oldboy pubilc]# find / -uid 1000 -ls 2>/dev/null
25716 4 -rw-r--r-- 1 oldboy oldboy 231 4月 11 08:53 /home/oldboy/.bashrc
25719 4 -rw------- 1 oldboy oldboy 1115 8月 22 21:50 /home/oldboy/.bash_histo
查找GID是的文件
[root@oldboy pubilc]# find / -gid 1000 -ls 2>/dev/null
25714 4 -rw-r--r-- 1 oldboy oldboy 18 4月 11 08:53 /home/oldboy/.bash_logout
25715 4 -rw-r--r-- 1 oldboy oldboy 193 4月 11 08:53 /home/oldboy/.bash_profi
查找没有属主的文件
[root@oldboy pubilc]# find / -nouser 2>/dev/null -ls
619630 0 -rw-rw---- 1 1002 mail 0 8月 31 00:08 /var/spool/mail/aaa
17563463 0 drwx------ 2 1002 1002 62 8月 31 00:08 /home/aaa
17563476 4 -rw-r--r-- 1 1002 1002 18 4月 11 08:53 /home/aaa/.bash_logout
17563511 4 -rw-r--r-- 1 1002 1002 193 4月 11 08:53 /home/aaa/.bash_profile
17563512 4 -rw-r--r-- 1 1002 1002 231 4月 11 08:53 /home/aaa/.bashrc
33587290 0 -rw-r--r-- 1 1002 1002 0 8月 10 00:00 /pubilc/file-10
33574983 0 -rw-r--r-- 1 1002 1002 0 8月 16 00:00 /pubilc/file-16
查找没有属组的文件
[root@oldboy pubilc]# find / -nogroup 2>/dev/null -ls
17563463 0 drwx------ 2 1002 1002 62 8月 31 00:08 /home/aaa
17563476 4 -rw-r--r-- 1 1002 1002 18 4月 11 08:53 /home/aaa/.bash_logout
17563511 4 -rw-r--r-- 1 1002 1002 193 4月 11 08:53 /home/aaa/.bash_profile
17563512 4 -rw-r--r-- 1 1002 1002 231 4月 11 08:53 /home/aaa/.bashrc
33587290 0 -rw-r--r-- 1 1002 1002 0 8月 10 00:00 /pubilc/file-10
33574983 0 -rw-r--r-- 1 1002 1002 0 8月 16 00:00 /pubilc/file-16
文件类型查找
-type
f:普通文件
d:目录文件
l:链接文件
c:字符设备文件
p:管道文件
b:块设备文件
普通文件
find /public -type f
目录
find /public -type d
链接文件
find /public -type l
管道文件
find /run -type p
块设备
find /dev -type b
字符设备
find /dev -type c
套接字文件
find /dev -type s
权限查找
-perm [/|-] mode
mode:精确权限匹配;
/mode:任何一类用户(g、u、o)的权限中的任何一位(r、w、x)符合条件即满足;
9位权限位之间存在"或"关系;
-mode:每一类用户(g、u、o)的权限中的每一位(r、w、x)同时符合条件即满足;
9位权限位之间存在"与"关系;
完全匹配444权限
拥有者至少有r— 组至少有r—其他人至少有r--
[root@oldboy pubilc]# find /pubilc/ -perm -444 -ls
组合查找
与:-a(默认)
或:-o
非:-not,!
!A -a !B = !(A -o B)
!A -o !B = !(A -a B)
在/public目录下查找属于属于oldboy用户并且属于oldgirl组的文件
[root@oldboy pubilc]# find /pubilc/ -user oldboy -a -group oldgirl -ls
33587284 0 -rw-r--r-- 1 oldboy oldgirl 0 8月 4 00:00 /pubilc/file-04
33587285 0 -rw-r--r-- 1 oldboy oldgirl 0 8月 5 00:00 /pubilc/file-05
33587286 0 -rw-r--r-- 1 oldboy oldgirl 0 8月 6 00:00 /pubilc/file-06
33587287 0 -rw-r--r-- 1 oldboy oldgirl 0 8月 7 00:00 /pubilc/file-07
在/public目录下查找属于oldboy或者属于oldgirl的文件
[root@oldboy pubilc]# find /pubilc/ -user oldboy -o -user oldgirl -ls
33587288 0 -rw-r--r-- 1 oldgirl oldboy 0 8月 8 00:00 /pubilc/file-08
处理动作
当查找到一个文件后,需要对文件进行处理
-print:输出到标准输出,默认的动作;
-ls:类似于对查找的文件执行"ls -l"命令,输出文件的详细信息;
-delete:删除查找到的文件;
-fls:/PATH/TO/SOMETILE:把查找到的所有文件的长格式信息保存至指定文件中;
-ok COMMAND {} \; :对查找的每个文件执行有COMMAND表示的 命令,每次操作都由用户进行确认;
-exec COMMAND {} \; :对查找的每个文件执行由COMMAND表示的命令,不需要确认;
打印查询到的文件
[root@oldboy pubilc]# find /pubilc/ -name "file-*"
/pubilc/file-01
/pubilc/file-02
/pubilc/file-03
[root@oldboy pubilc]# find /pubilc/ -name "file-*" -print
/pubilc/file-01
/pubilc/file-02
/pubilc/file-03
拷贝文件
[root@oldboy pubilc]# find /etc/sysconfig/ -name "ifcfg-ens33" -exec cp {} ./ \;
[root@oldboy pubilc]# find /etc/sysconfig/ -name "*ens33" -ok cp {} ./ \;
< cp ... /etc/sysconfig/network-scripts/ifcfg-ens33 > ? y
删除文件
[root@oldboy pubilc]# find /pubilc/ -name "file-01" -exec rm {} \;
本地文件保留最近7天的备份,备份服务器保留3个月
[root@oldboy pubilc]# find /pubilc/ -mtime +7 -delete
[root@oldboy pubilc]# find /pubilc/ -mtime +30 -delete
Linux文件查找find和locate的更多相关文章
- linux文件查找-find和locate
一.find 使用语法:find [查找目录] [查找规则] [查找完后执行的action] find是根据具体目录进行搜索 1.查找目录 如果不指定查找目录,默认在当前目录下进行查找 如果需要 ...
- linux文件查找(find,locate)
文件查找: locate: 非实时,模糊匹配,查找是根据全系统文件数据库进行的: # updatedb, 手动生成文件数据库 速度快 find: 实时 精确 ...
- Linux文件查找命令find用法整理(locate/find)
Linux文件查找查找主要包括:locate和find 1.locate 用法简单,根据数据库查找,非实时,用法: locate FILENAME 手动更新数据库(时间可能较长) updatedb 2 ...
- linux 文件查找,which,whereis,locate,find
linux 文件查找,which,whereis,locate,find 一:which 主要用于查找可执行命令的所在位置: 如图,查找命令 ls的目录: 二:whereis 主要用于查找命令的帮助文 ...
- Linux文件查找.md
Linux 文件查找 在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 w ...
- linux文件查找find命令
linux文件查找find命令 1.文件查找 基本介绍 在文件系统上查找符合条件的文件 linux上常见的文件查找工具:find命令 查找分类 实时查找 精确查找 基本语法 find [option ...
- Linux文件查找命令find,xargs详述【转】
转自:http://blog.csdn.net/cxylaf/article/details/4069595 转自http://www.linuxsir.org/main/?q=node/137 Li ...
- Linux学习总结(十五)文件查找 which whereis locate find
which命令 用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录.which指令会在环境变量$PATH设置的目录里查找符合条件的文件.也就是说,使用which命令,就 ...
- Linux文件查找命令具体解释-which whereis find locate
原创BLog.转载请注明出处 http://blog.csdn.net/hello_hwc? viewmode=contents which命令 首先查看man which的说明 which - sh ...
随机推荐
- c++模板专门化
#include <iostream> #include<cstring> using namespace std; template <typename T> T ...
- mysql用户和授权
CREATE USER 'monitor'@'10.224.32.%' IDENTIFIED BY '123@abAB'; mysql> GRANT select,insert,update O ...
- redis查数据
1 连接服务 [root@redis1-20 ~]# telnet 127.0.0.1 6380 Trying 127.0.0.1... Connected to 127.0.0.1. Escape ...
- Android屏幕适配-安卓切图
一.Android中的单位 1.dp(dip):density-independent pixels,这并不是一个绝对的单位,而只是一个相对的概念,代表的是屏幕写对角线上每inch上像素点的个数. 2 ...
- Keepalived+Nginx实现Nginx的高可用
集群规划 主机名 IP VIP Nginx:port KeepAlived主备 KA_NG_01 192.168.30.130 192.168.30.120 8088 MASTER KA_NG_02 ...
- 在linux中使用多个redis端口来构建redis集群
大家好,这是我制作的redis集群搭建视频教程. 服务器:ubnutu server(版本18.10) redis:redis-4.0.12 我这里就简单说明下大概步骤了,详细请观看教学视频. 首先更 ...
- javascript回调函数那些事~
什么是回调函数? 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数.回调函数不是由该函数的实现方直 ...
- python工具之exccel模板生成报表
from Db import Db from log import log import xlwt import xlrd from xlutils.copy import copy import s ...
- 2019/05/11 Java内存结构
1. 类加载子系统:负责从文件系统或者网络加载Class信息,加载的信息存放在一块称之方法区的内存空间. 2. 方法区:就是存放类的信息.常量信息.常量池信息.包括字符串字面量和数字常量等. 3. ...
- Java关键字-volatile
关键字volatile可以说是Java虚拟机提供的最轻量级的同步机制. 一旦某个共享变量(类的成员变量.类的静态成员变量)被volatile修饰之后,那么就具备了两层语义: 1.保证了不同线程对这个变 ...