Linux 学习:文件查找的使用

本次记录以下命令如locate, find的使用格式、常用选项及它们有哪些使用实例等。

wlocate与find命令的特性

locate与find命令都用来查找文件或目录的。但明显locate查找速度要比find快得多,原因在于locate不需要搜索具体目录,而是搜索一个数据库文件。一般来说,Linux系统会将系统内的所有文件都记录在一个数据库文件里面,Linux系统自动创建这个数据库,且每天自动更新一次,所以有时你还发现使用locate,会找到已经被删掉的文件!也会查不到最新变动过的文件。

find命令是直接查找硬盘,比较耗费时间。

1. Locate命令

用来查找文件或目录。它是非实时查找工具,依赖于事先构建好的索引,而不是在文件系统上直接搜索的。查找速度快,模糊查找。

Locate寻找的数据是由已创建的数据库/var/lib/mlocate里面的数据所查找到的,此数据库每天更新一次,所以当你新建文件后查找该文件,那么locate会告诉你“找不到”!因为必须要更新数据库。

手动通过updatedb命令读取/etc/updated.conf的设置去查找系统硬盘内的文件名,并更新/var/lib/mlocate内的数据库文件。

Locate依据/var/lib/mlocate内数据库记载,找出用户输入的关键字文件名。

查找文件与目录Locate命令格式:

locate [OPTION]... PATTERN...

OPTION:

-i 忽略大小写

-r 可接正则表达式的显示方式

[[email protected] tmp]# locate -r ‘pwd$‘ |grep -n ‘pwd$‘

1:/bin/pwd

2:/sbin/unix_chkpwd

3:/usr/lib64/cracklib_dict.pwd

4:/usr/share/cracklib/cracklib-small.pwd

5:/usr/share/cracklib/pw_dict.pwd

-n# 至多显示n个输出

[[email protected] tmp]# locate -n10 ‘pass‘

/etc/passwd

/etc/passwd-

/etc/openldap/certs/password

/etc/pam.d/gdm-password

/etc/pam.d/passwd

/etc/pam.d/password-auth

/etc/pam.d/password-auth-ac

/etc/profile.d/gnome-ssh-askpass.csh

/etc/profile.d/gnome-ssh-askpass.sh

/etc/security/opasswd

2. find命令

用来在指定的目录下查找文件。它是实时查找工具,精确匹配查找

在指定的目录下查找文件的命令格式:

find [OPTION]...[查找路径][查找条件][处理动作]

查找路径:如果未指定,则默认为当前路径

组合查找条件:

与条件: -a 可以省略

或条件: -o 优先级最低

非条件: -not, ! 优先级高

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

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

查找条件:

w 根据文件名进行查找

-name filename: 查找文件名为filename的文件(支持glob)

[[email protected] lab]# find /etc -name "*.conf" |tail -n 10|grep -n ‘conf‘

1:/etc/request-key.conf

2:/etc/prelink.conf

3:/etc/mke2fs.conf

4:/etc/logrotate.conf

5:/etc/ant.conf

6:/etc/mtools.conf

7:/etc/udev/udev.conf

8:/etc/dracut.conf

9:/etc/yp.conf

10:/etc/sgml/sgml.conf

-iname filename 不区分字符大小写

-regex “PATTERN”指定字符串作为寻找文件或目录的范本样式

[[email protected] lab]# find /lab -regex ‘.*\(\.txt\)$‘

/lab/readme.txt

/lab/regular_express.txt

/lab/character.txt

w 根据属主、属组查找

-user USERNAME 查找属主为指定用户的文件

[[email protected] lab]# find /lab -user Allen -ls

430089 4 -rw-r--r-- 1 Allen asdgrp 21 9月 11 13:33 /lab/readme.txt

-group GROUPNAME 查找属组为指定组的文件

[[email protected] ~]# find /lab -group asdgrp

/lab/readme.txt

-uid UserID 查找文件的属主为指定uid的文件

-gid GroupID 查找文件的属组为指定gid的文件

-nouser 查找没有属主的文件

-nogroup 查找没有属组的文件

[[email protected] ~]# ll /lab

总用量 52

drwxr-xr-x. 3 root root 4096 9月 6 09:54 123

-rw-r--r--. 1 root root 71 9月 7 12:50 character.txt

-rw-r--r--. 1 root root 899 8月 17 19:30 fstab

-rw-r--r--. 1 root root 19846 9月 9 17:26 functions

-rw-r--r--. 1 505 asdgrp 21 9月 11 13:33 readme.txt

-rw-r--r--. 1 root root 650 9月 6 09:55 regular_express.txt

-rw-r--r--. 1 root root 115 9月 6 09:57 t1

-rw-r--r--. 1 root root 112 9月 6 09:57 t2

-rw-r--r--. 1 root root 170 9月 6 09:58 t3

[[email protected] ~]# find /lab -uid 505 -ls

430089 4 -rw-r--r-- 1 505 asdgrp 21 9月 11 13:33 /lab/readme.txt

[[email protected] ~]# find /lab -nouser -ls

430089 4 -rw-r--r-- 1 505 asdgrp 21 9月 11 13:33 /lab/readme.txt

[[email protected] ~]# find /lab -nogroup -ls

430089 4 -rw-r--r-- 1 505 505 21 9月 11 13:33 /lab/readme.txt

w 根据文件类型进行查找

-type TYPE

[[email protected] lab]# find -type f -ls

430082 4 -rw-r--r-- 1 root root 112 9月 6 09:57 ./t2

430089 4 -rw-r--r-- 1 505 505 21 9月 11 13:33 ./readme.txt

430077 4 -rw-r--r-- 1 root root 650 9月 6 09:55 ./regular_express.txt

430092 20 -rw-r--r-- 1 root root 19846 9月 9 17:26 ./functions

430073 4 -rw-r--r-- 1 root root 899 8月 17 19:30 ./fstab

430081 4 -rw-r--r-- 1 root root 115 9月 6 09:57 ./t1

430083 4 -rw-r--r-- 1 root root 170 9月 6 09:58 ./t3

389380 4 -rw-r--r-- 1 root root 71 9月 7 12:50 ./character.txt

w 使用组合条件进行查找

[[email protected] ~]# find /lab -type f -a -nouser -ls

430089 4 -rw-r--r-- 1 505 505 21 9月 11 13:33 /lab/readme.txt

[[email protected] ~]# find /etc ! -name ‘*.conf‘ |tail -n 10

/etc/logrotate.d/yum

/etc/logrotate.d/dracut

/etc/logrotate.d/iscsiuiolog

/etc/logrotate.d/ppp

/etc/logrotate.d/libvirtd.lxc

/etc/logrotate.d/wpa_supplicant

/etc/logrotate.d/glusterfs

/etc/logrotate.d/sssd

/etc/logrotate.d/syslog

/etc/logrotate.d/psacct

[[email protected] ~]# find /etc -not -name "*.txt" -a -not -type l -ls |head -n 3

[[email protected] ~]# find /etc ! \( -name "*.txt" -o -type l \) -ls |head -n 3

389381 12 drwxr-xr-x 125 root root 12288 9月 11 15:49 /etc

414087 16 -rw-r--r-- 1 root root 12623 7月 24 09:34 /etc/autofs.conf

408995 4 drwxr-xr-x 3 root root 4096 8月 17 19:42 /etc/sound

w 根据文件大小来查找

-size: [+|-]#UNIT

#UNIT (#-1,#]

+UNIT (#,+∞)

-UNIT [0, #-1]

[[email protected] ~]# find /etc -size 3k -exec ls -lh {} \;<-- 找出>2k3k的文件

-rw-r--r--. 1 root root 2.5K 11月 11 2010 /etc/udev/rules.d/97-bluetooth-serial.rules

-rw-r--r--. 1 root root 2.6K 8月 17 2010 /etc/mtools.conf

-rwxr-xr-x. 1 root root 2.6K 10月 16 2014 /etc/gdm/Init/Default

-rw-r--r--. 1 root root 2.9K 11月 23 2013 /etc/init/readahead-collector.conf

-rw-r--r--. 1 root root 2.3K 7月 24 09:38 /etc/selinux/semanage.conf

-rw-r--r--. 1 root root 2.9K 7月 24 21:05 /etc/selinux/targeted/contexts/x_contexts

-rw-r--r--. 1 root root 2.1K 11月 11 2010 /etc/bluetooth/main.conf

-rw-r--r--. 1 root root 2.1K 9月 7 23:36 /etc/passwd

-rw-r--r--. 1 root root 2.6K 11月 12 2010 /etc/latrace.d/signal.conf

-rw-r--r--. 1 root root 2.3K 11月 12 2010 /etc/latrace.d/resource.conf

w 根据文件时间戳查找

访问时间(-atime/天,-amin/分钟):用户最近一次访问时间

修改时间(-mtime/天,-mmin/分钟):文件最后一次修改时间

变化时间(-ctime/天,-cmin/分钟):文件元数据最后一次修改时间

以atime为例,-atime 3表示访问时间范围为≥3天<4天

[[email protected] lab]# find /lab -mtime -3 –ls<-- 找出3天内的文件

297972 0 -rw-rw-rw- 1 root root 0 9月 9 06:20 /lab/jjj

[[email protected] lab]# date

2015年 09月 12日 星期六 05:41:29 CST

[[email protected] lab]# touch -m -t 201509080101 abc

[[email protected] lab]# find /lab -mtime +3 –ls <-- 找出4天前的文件

4770 4 -rw-r--r-- 1 root root 143 9月 7 23:05 /lab/testing

297967 4 -rw-r--r-- 1 root root 15 8月 28 21:04 /lab/inpub_txt

297966 4 -rw-r--r-- 1 root root 51 8月 28 20:29 /lab/var.err

297971 4 -rwxr-xr-x 1 Oracle agetest 113 9月 5 20:44 /lab/t2

297975 0 -rw-r--r-- 1 root root 0 9月 8 01:01 /lab/abc

297969 4 -rw-r--r-- 1 root root 51 9月 7 06:21 /lab/character.txt

297970 4 -rw-rw-rw- 1 Oracle agetest 170 9月 5 20:45 /lab/t3

297974 4 -rw-r--r-- 1 root root 114 9月 5 21:10 /lab/t1

w 根据权限查找

-perm [/|-]MODE

MODE:精确权限匹配

[[email protected] ~]# find /etc -perm 400 -ls

404218 4 -r-------- 1 root root 45 8月 17 19:37 /etc/openldap/certs/password

/MODE:任何一类对象(u,g,o)的任何一位权限符合条件即可

[[email protected] 123]# ll

总用量 4

drwxr-xr-x. 3 root root 4096 9月 6 09:54 456

-r--r--r--. 1 root root 0 9月 12 09:11 aa

-rw-r-----. 1 root root 0 9月 12 09:11 bb

-rw-rw----. 1 root root 0 9月 12 09:11 cc

-rwxrwxrwx. 1 root root 0 9月 12 09:11 dd

-rw-r--r--. 1 root root 0 9月 12 09:11 ee

[[email protected] 123]# find . -perm /020 -ls

554308 0 -rw-rw---- 1 root root 0 9月 12 09:11 ./cc

554309 0 -rwxrwxrwx 1 root root 0 9月 12 09:11 ./dd

-MODE:每一类对象指定的每位权限都必须同时存在

[[email protected] 123]# find . -perm -201 -ls

554303 4 drwxr-xr-x 3 root root 4096 9月 12 09:11 .

554304 4 drwxr-xr-x 3 root root 4096 9月 6 09:54 ./456

554305 4 drwxr-xr-x 2 root root 4096 9月 6 09:54 ./456/789

554309 0 -rwxrwxrwx 1 root root 0 9月 12 09:11 ./dd

处理动作:

-print:默认处理动作

-ls: 类似于对查找到的每个文件做"ls -l"操作

-delete: 删除查找到的文件

-fls /path/to/somefile: 查找到的文件的详细路径信息保存至指定文件中

-ok COMMAND {} \;

对每个文件执行指定的命令之前需要用户事先确认

-exec COMMAND {} \;

无需用户确认

{} 用于与-exec选项结合使用来匹配所有文件,然后被替换为相应的文件名

[[email protected] 123]# find . -perm -201 -ls

554303 4 drwxr-xr-x 3 root root 4096 9月 12 09:11 .

554304 4 drwxr-xr-x 3 root root 4096 9月 6 09:54 ./456

554305 4 drwxr-xr-x 2 root root 4096 9月 6 09:54 ./456/789

554309 0 -rwxrwxrwx 1 root root 0 9月 12 09:11 ./dd

[[email protected] 123]# find . -perm -201 -fls /lab/perm.txt

[[email protected] 123]# find . ! -perm /111 -exec ls -l {} \;

-rw-r--r--. 1 root root 0 9月 12 09:11 ./ee

-rw-rw----. 1 root root 0 9月 12 09:11 ./cc

-rw-r-----. 1 root root 0 9月 12 09:11 ./bb

-r--r--r--. 1 root root 0 9月 12 09:11 ./aa

[[email protected] 123]# find . ! -perm /111 -exec mv {} {}.old \;

[[email protected] 123]# ll

总用量 4

drwxr-xr-x. 3 root root 4096 9月 6 09:54 456

-r--r--r--. 1 root root 0 9月 12 09:11 aa.old

-rw-r-----. 1 root root 0 9月 12 09:11 bb.old

-rw-rw----. 1 root root 0 9月 12 09:11 cc.old

-rwxrwxr-x. 1 root root 0 9月 12 09:11 dd

-rw-r--r--. 1 root root 0 9月 12 09:11 ee.old

时间: 2024-08-10 21:28:42

Linux 学习:文件查找的使用的相关文章

Linux下文件查找与定位

Linux下一切皆文件,但是文件由于文件的属性的不同,在查找上可以针对不同的文件做相应的查找以便加快查询速度和减少资源的消耗. 1 .   which 命令[可执行文件的查找,系统文件中检索]:此命令是查找显示可执行命令的路径和别名,通过查找可以确定在/sbin . /usr/sbin. /usr/bin .哪个目录下, 是在系统和用户环境变量中存在的目录文件中查找的. 参数 解释 --all,  -a Print all matching executables in  PATH, not j

Linux下文件查找

linux文件查找 find 文件查找 locate 文件查找:在文件系统上查找符合条件的文件 locate 依赖于事先构建好的索引库     查询系统上预建的文件索引数据库     /var/lib/mlocate/mlocate.db 系统自动实现,周期性任务. 手动创建跟新数据库(updatedb,非常消耗系统资源) 慎用! 工作特性:     查找速度快     模糊查找     非实时查找,可能文件已经有变动或不存在     locate [option]...PATTERN...  

linux之文件查找find grep详解,以及压缩归档

.find linux里的实时查找工具,通过制定路径完成文件查找. find[options]...[查找路径] [查找条件] [处理动作] 查找路径:查找的位置,默认是当前文件夹. 查找条件:指定查找的标准,文件名,大小,类型,日期等. 处理动作:对符合条件的文件做什么类型操作,默认是输出. 查找条件 根据文件名查找 -name 指定名称,可以使用正则 find /etc -name *.conf -maxdepth 2 -iname 忽略大小写 -links n 引用磁盘次数为n的文件 fi

Linux下文件查找工具介绍

文件查找工具 locate find locate: 查询系统预建的文件索引数据库      1. /var/lib/mlocate/mlocate.db     2.因为依赖于事先构建的索引,而索引是系统在闲时自动进行的,管理员需要手动更新(updatedb). locate的特点:查找速度快        模糊查找        非实时查找        搜索的是文件的全路径,不仅仅是文件名        可能只搜索用户具备读取和执行权限的目录 基本语法:   -i :不区分大小写的搜索  

【Linux】文件查找:find

Linux下有多种查找文件指令:find.whereis.which和locate. ● which:查看可执行文件的位置 ● whereis:查看文件的位置 ● locate:配合数据库查看文件 ● find:实际搜寻硬盘查询 其中find是最常用和最强大的查找命令.它能做到实时查找,精确查找,但速度慢.下面主要对find指令相关用法进行详细介绍. ●●●find指令: [格式]find  [指定查找目录]  [查找规则]  [查找完后执行的action] 默认路径为当前目录:默认表达式为 -

Linux学习之查找命令汇总

我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索:        which  查看可执行文件的位置.       whereis 查看文件的位置.        locate   配合数据库查看文件位置.       find   实际搜寻硬盘查询文件名称. which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果.也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令. 1.

Linux之文件查找

总言:服务器上的文件如浩瀚星空,想要找到指定的一颗星辰何其难哉.幸好linux给我们提供了两样搜索命令帮组搜索:locate.find 一.locate: locate命令查询文件需要依赖系统本身的一个数据库,这个数据库每天会例行的执行一次.当我们拥有这个数据库了,就可以快捷的搜寻文件. locate依托四个组件: 1./usr/bin/updatedb   #更新数据库文件,通过crontab每天自动运行 2./usr/bin/locate     #查询功能 3./etc/updatedb.

linux 文件系统 &amp; 文件查找 &amp; 文件链接

学会磁盘分区.挂载文件系统.创建软硬链接.查找文件 [[email protected] ~]# fdisk -l 磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0x000ab236 设备 Boot Start End Block

linux之文件查找命令find技巧

1. 想查看当前文件夹及子文件夹里有没有文件名为"abc"的文件 find -name abc -name:表示要根据名称查找 2. 想查看当前文件夹及子文件夹里有没有"xyz"目录 find -type d -name xyz type:表示设定类型,d表示文件夹类型,可以替换为f(普通文件).l(链接文件) 3. 想找出当前文件夹及子文件夹里所有后缀是".txt"的文件 find -name *.txt *.txt 代表以.txt结尾的文件目

Linux学习之查找命令find

1.find  -name 根据名称查找文件 *通配所有字符: ?统配单个字符 2.find -iname 根据名称不区分大小写 3.find -size  根据文件大小 find / -size +2048000 大于100M:# + 表示大于多少 find / -size -2048000 小于100M: # - 表示小于 find / -size n2048000  等于100M: # n表示等于 4.find -user/group 根据创建用户名或群组查找文件 5.find -cmin