linux文件查找-find和locate

时间:2022-12-02 14:38:43

一、find

使用语法:find  [查找目录]  [查找规则]  [查找完后执行的action]

find是根据具体目录进行搜索

1.查找目录

如果不指定查找目录,默认在当前目录下进行查找

如果需要在多个目录下查找,多个不同的目录通过空格隔开

2.查找规则

①根据文件名查找 -name

-name和-iname:后者表示不区分大小写

例如查找/tmp和/var目录下以.txt结尾的文件:find /tmp /var -name '*.txt'

②根据文件类型查找 -type

-type 文件类型,其中文件类型有

f:普通文件  d:目录文件  l:链接文件  b:块设备文件  c:字符设备文件  p:管道文件  s:socket文件

例如查找/tmp目录下的链接文件:find /tmp -type l

③根据所属用户和组查找

-user:根据属主查找

-group:根据属组查找

-nouser和-nogroup分别表示没有属主和属组,一般不允许系统中出现没有属主和属组的文件

例如查找/tmp目录下属主为oracle的文件:find /tmp -user oracle

例如查找/tmp目录下属组为phh的文件:find /tmp -grouop phh

例如查找系统中没有属主的文件:find / -nouser

④根据uid和gid查找

-uid:根据uid查找

-gid:根据gid查找

例如查找/tmp目录下uid为501的文件:find /tmp -uid 501

⑤根据大小查找 -size

size的单位可为b、c、w、k、M、G(各单位代指的含义可通过man find查看),如果不指定单位默认为b

-size 2M表示大小为2M,-size +2M表示大于2M,-size -2M表示小于2M

例如查找/tmp目录下大于1M的文件:find /tmp -size +1M

⑥根据权限查找 -perm

+  / -

⑦根据时间戳查找 -atime、-mtime、-ctime(单位均为天)

先通过stat命令查看时间戳,假设创建一个文件a.txt

[root@oldboy ~]# stat a.txt
File: `a.txt'
Size: Blocks: IO Block: regular file
Device: fd00h/64768d Inode: Links:
Access: (/-rw-r--r--) Uid: ( / root) Gid: ( / root)
Access: -- ::02.361000255 -
Modify: -- ::02.361000255 -
Change: -- ::02.361000255 -

最后三行,Access对应-atime,表示文件最后的访问时间

Modify对应-mtime,表示文件内容的最后修改时间

Change对应-ctime,表示文件状态的最后修改时间,包括大小、权限、属主属组、链接数等

例如查找/tmp目录下5天内未被访问过的文件:find /tmp -atime +5

例如查找/tmp目录下5天内被访问过的文件:find /tmp -atime -5

atime、mtime和ctime的单位为天,还有一组与它们对应的时间戳,分别为amin、mmin和cmin,单位为分钟

⑧多条件查询 -a、-o、-not

-a表示多个条件同时满足,例如查找/tmp目录下属主为oracle的.sh文件:find /tmp -user oracle -a -name '*.sh'

-o表示多个条件满足一个即可,例如查找/tmp目录下没有属主、或者没有属组的文件:find /tmp -nouser -o -nogroup

-not表示对条件取反,例如查找/tmp目录下属主不是root的文件:find /tmp -not -user root

3.查找完后执行的action

①-print:默认动作,将查询结果打印出来

[root@oldboy ~]# find -name '*.txt'
./a.txt
./test.txt
[root@oldboy ~]# find -name '*.txt' -print
./a.txt
./test.txt

②-ls:将查询结果通过ls显示出来

[root@oldboy ~]# find -name '*.txt' -ls
-rw-r--r-- root root Apr : ./a.txt
-rw-r--r-- root root Apr : ./test.txt

③-ok command \; 查找后执行命令,并询问用户是否执行

④-exec command \; 查找后直接执行命令,不询问用户

[root@oldboy ~]# find -name '*.txt' -ok chown phh {} \;
< chown ... ./a.txt > ? y
< chown ... ./test.txt > ? y
[root@oldboy ~]# ll *.txt
-rw-r--r--. phh root Apr : a.txt
-rw-r--r--. phh root Apr : test.txt
[root@oldboy ~]# find -name '*.txt' -exec chown root {} \;
[root@oldboy ~]# ll *.txt
-rw-r--r--. root root Apr : a.txt
-rw-r--r--. root root Apr : test.txt

上面例子中的{ }代表查找到的文件,\和;之间没有空格。

二、locate

locate命令用来查找文件或目录。创建Linux系统时会自动创建一个数据库/var/lib/mlocate/mlocate.db,并且每天自动更新一次,这个数据库中包含本地所有文件的信息。locate命令要比find -name快得多,原因在于它不搜索具体目录,而是搜索数据库。但是locate的查找不是实时的,有时会找到已经被删除的数据,或者刚刚建立的文件无法查找到,这是因为数据库文件没有更新。为了避免这种情况,在使用locate之前需先使用updatedb命令手动更新数据库。

整个locate工作其实是由四部分组成的:

/usr/bin/updatedb:主要用来更新数据库,通过crontab自动完成的

/usr/bin/locate:查询文件位置

/etc/updatedb.conf:updatedb的配置文件

/var/lib/mlocate/mlocate.db:存放文件信息的文件

使用语法:locate [参数] 模式

locate支持模糊匹配,例如查询文件名中包含passwd的文件:locate passwd

三.which

which会在PATH环境变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。即使用which命令,可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。

四.whereis

whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s),如果省略参数,则返回所有信息。

whereis和locate一样,都是通过数据库进行搜索。

cat: /bin/cat /usr/share/man/man1/cat..gz /usr/share/man/man1p/cat.1p.gz
[root@oldboy mlocate]# which passwd
/usr/bin/passwd
[root@oldboy mlocate]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man5/passwd..gz /usr/share/man/man1/passwd..gz
[root@oldboy mlocate]# locate passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/lib64/security/pam_passwdqc.so
/lib64/security/pam_unix_passwd.so
/usr/bin/gpasswd
/usr/bin/kpasswd
……