Linux文件查找find和locate



第1章 locate文件查找    1

1.1 概述    1

1.2 locate文件查找的特性    1

第2章
文件查找概述    1

第3章    1

3.1 文件名查找    1

3.2 文件大小查找    1

3.3 时间戳查找    1

3.4 文件从属关系查找    1

3.5 文件类型查找    1

3.6 权限查找    1

3.7 组合查找    1

3.8 处理动作    1

  1. locate文件查找

  2. 概述

安装

yum install -y mlocate

locate

-c:符合条件的文件数量

-b:基名查找(按文件名称关键字查找,而不是文件路径关键字)

locate依赖于事先构建好的索引库

系统自动实现(周期性)

手动更新数据库(updatedb)

  1. locate文件查找的特性

1、查找速度快

2、模糊查找

3、不是实时更新

  1. 文件查找概述

Linux系统中的find命令在查找文件是非常有用而且方便

他可以根据不同条件来进行查找文件:例如权限,拥有者,修改时间,文件大小等等。同时find是linux下必须掌握的。

find命令的基本语法如下:


命令


选项


路径


表达式


动作


find


[options]


[path]


[expression]


[action]

  1. 文件名查找

-name:区分大小写

-iname:忽略大小写

通配符:*和?    *:匹配多个任意字符 ?:匹配一个任意字符

-regex:基本正则表达式模糊查找文件,匹配是整个路径,而非其名;

区分大小写

[[email protected] pubilc]# find /pubilc/ -name "ifcfg-ens33"

/pubilc/ifcfg-ens33

不区分大小写

[[email protected] pubilc]# find /pubilc/ -iname "ifcfg-ens33"

/pubilc/ifcfg-ens33

/pubilc/IFCFG-ENS33

  1. 文件大小查找

-size[+|-] #UNIT

常用单位:k、m、g

查找大于10m的文件

[[email protected] 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的文件

[[email protected] pubilc]# find /pubilc/ -size 10M -ls

33587282 10240 -rw-r--r-- 1 root root 10485760 8月 23 16:21 /pubilc/file1.txt

查找小于10M的文件

[[email protected] 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

  1. 时间戳查找

以"天"为单位

-atime[-|+]#

#:之前第#天的文件

-#:七天内的文件

+#:七天之前的文件

以分钟为单位:

-amin:

-mmin

-cmin:

创建文件测试文件

[[email protected] pubilc]# for i in {01..31};do date -s 201808$i && touch file-$i;done

查找20以前的文件

[[email protected] 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天以内的文件

[[email protected] 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天,当天的文件

[[email protected] pubilc]# find /pubilc/ -atime 5 -ls

33588260 0 -rw-r--r-- 1 root root 0 8月 26 00:00 /pubilc/file-26

  1. 文件从属关系查找

-user:查找属主指定用户的所有文件;

-group:查找属组指定组的所有文件;

-uid:查找属主指定UID的所有文件

-gid:查找属组指定的GID的所有文件

-nouser:查找没有属主的文件;

nogroup:查找没有属组的文件;

查找属主是oldboy的文件

[[email protected] 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的文件

[[email protected] 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是的文件

[[email protected] 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是的文件

[[email protected] 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

查找没有属主的文件

[[email protected] 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

查找没有属组的文件

[[email protected] 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

  1. 文件类型查找

-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

  1. 权限查找

-perm [/|-] mode

mode:精确权限匹配;

/mode:任何一类用户(g、u、o)的权限中的任何一位(r、w、x)符合条件即满足;

9位权限位之间存在"或"关系;

-mode:每一类用户(g、u、o)的权限中的每一位(r、w、x)同时符合条件即满足;

9位权限位之间存在"与"关系;

完全匹配444权限

拥有者至少有r— 组至少有r—其他人至少有r--

[[email protected] pubilc]# find /pubilc/ -perm -444 -ls

  1. 组合查找

与:-a(默认)

或:-o

非:-not,!

!A -a !B = !(A -o B)

!A -o !B = !(A -a B)

在/public目录下查找属于属于oldboy用户并且属于oldgirl组的文件

[[email protected] 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的文件

[[email protected] 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

  1. 处理动作

当查找到一个文件后,需要对文件进行处理

-print:输出到标准输出,默认的动作;

-ls:类似于对查找的文件执行"ls -l"命令,输出文件的详细信息;

-delete:删除查找到的文件;

-fls:/PATH/TO/SOMETILE:把查找到的所有文件的长格式信息保存至指定文件中;

-ok COMMAND {} \; :对查找的每个文件执行有COMMAND表示的 命令,每次操作都由用户进行确认;

-exec COMMAND {} \; :对查找的每个文件执行由COMMAND表示的命令,不需要确认;

打印查询到的文件

[[email protected] pubilc]# find /pubilc/ -name "file-*"

/pubilc/file-01

/pubilc/file-02

/pubilc/file-03

[[email protected] pubilc]# find /pubilc/ -name "file-*" -print

/pubilc/file-01

/pubilc/file-02

/pubilc/file-03

拷贝文件

[[email protected] pubilc]# find /etc/sysconfig/ -name "ifcfg-ens33" -exec cp {} ./ \;

[[email protected] pubilc]# find /etc/sysconfig/ -name "*ens33" -ok cp {} ./ \;

< cp ... /etc/sysconfig/network-scripts/ifcfg-ens33 > ? y

删除文件

[[email protected] pubilc]# find /pubilc/ -name "file-01" -exec rm {} \;

本地文件保留最近7天的备份,备份服务器保留3个月

[[email protected] pubilc]# find /pubilc/ -mtime +7 -delete

[[email protected] pubilc]# find /pubilc/ -mtime +30 -delete

原文地址:https://www.cnblogs.com/majinhai/p/9534103.html

时间: 2024-08-29 00:29:28

Linux文件查找find和locate的相关文章

Linux文件查找之find&locate

Linux文件查找之find&locate 一.概述 Linux系统核心的思想之一"一切皆文件",对于这么多的文件,如何快速查找过滤呢?下面我们就看看系统提供的文件查找命令find和locat,熟练使用find命令是运维人员的必经之路 二.find的用法及示例 1.find特点 查找速度略慢 精确查找 实时查找 只能搜索有读取和执行权限的目录 2.find用法 用法:find  [options]  [查找路径]  [ 查找条件]  [处理动作] 查找条件: 根据文件类型查找

linux文件查找(find,locate)

文件查找: locate: 非实时,模糊匹配,查找是根据全系统文件数据库进行的: # updatedb, 手动生成文件数据库 速度快 find: 实时 精确 支持众多查找标准 遍历指定目录中的所有文件完成查找,速度慢: find 查找路径 查找标准 查找到以后的处理运作 查找路径:默认为当前目录 查找标准:默认为指定路径下的所有文件 处理运作:默认为显示 匹配标准: -name 'FILENAME':对文件名作精确匹配 文件名通配: *:任意长度的任意字符 ? [] -iname 'FILENA

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

Linux文件查找查找主要包括:locate和find 1.locate 用法简单,根据数据库查找,非实时,用法: locate FILENAME 手动更新数据库(时间可能较长) updatedb 2.find 实时,精确,功能强大,用法: find 路径 查找标准 查找后动作 (1)路径:. 或者 ./ 均表示当前目录 (2)查找标准: -name 'FILENAME' 文件名精确匹配(支持通配符* ? []) -iname 'FILENAME' 文件名模糊匹配(不分大小写) -regex P

linux 文件查找 (locate 与 find)

文件查找 目录 1.locate2.find 一. locate locate命令基于数据库db,所以不能实时更新 他有如下的特点: 1.查找速度快 2.是模糊查找(可能搜到了文件,也有可能搜到了目录) 3.是非实时的查找 4.由于权限的原因可能只查找到一部分的文件 locate 参数: -i 不区分大小写 -n N 只列举前N个项目 -r 支持正则表达式 locate xiaoyu 搜索名称或路径中带有"xiaoyu"的文件 例: 二. find 实时查找工具,通过遍历指定路径完成文

Linux文件查找.md

Linux 文件查找 在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 whereis whereis命令是定位可执行文件.源代码文件.帮助文件在文件系统中的位置.这些文件的属性应属于原始代码,二进制文件,或是帮助文件.whereis 程序还具有搜索源代码.指定备用搜索路径和搜索不寻常项的能力. 语法 whereis [-bmsu] 文件或者目录名称 参数 -b 定位可执

Linux文件查找

Linux文件查找 Linux的基本法则之一为一切皆文件,Linux遵循FHS(Filesystem Hierarchy Standard),文件系统目录标准,FHS采用树形结构组织文件:在使用过程中,我们会遇到知道文件名却不知道文件位置的情况,或者查找某些类型的文件,此时我们需要文件查找的功能. 一.常用查找命令: Locate与finad: locate命令为非实时查找模式:非实时查找,需要根据索引查找,依赖于索引,而索引构建相当占用资源:索引的创建是在系统空闲时由系统自动进行(每天任务):

Linux 文件查找命令find,xargs详述

Linux文件查找命令find,xargs详述     <exec 后期讲解> 关于find命令 由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下.即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限. 在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统). 一.find 命令格式 1.find命令的一般形式为:

Linux文件查找命令find,xargs详述

转自:http://www.linuxsir.org/main/node/137本文进行了整理修订 Linux文件查找命令find,xargs详述 摘要: 本文是find 命令的详细说明,可贵的是针对参数举了很多的实例,大量的例证,让初学者更为容易理解:本文是zhyfly兄贴在论坛中:我对本文进行了再次整理,为方便大家阅读: 目录 版权声明 前言:关于find命令 一.find 命令格式 1.find命令的一般形式为: 2.find命令的参数: 3.find命令选项: 4.使用exec或ok来执

文件查找 /which/whereis/locate/find/grep

Which #查找固定的目录# [[email protected] home]# which ba /usr/bin/which: no ba in (/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin) [[email protected] home]# which ls alias ls='ls --color=auto' /bin/ls 我们可以看到 whic