总言:服务器上的文件如浩瀚星空,想要找到指定的一颗星辰何其难哉。幸好linux给我们提供了两样搜索命令帮组搜索:locate、find

一、locate:

  locate命令查询文件需要依赖系统本身的一个数据库,这个数据库每天会例行的执行一次。当我们拥有这个数据库了,就可以快捷的搜寻文件。

locate依托四个组件:
	1./usr/bin/updatedb   #更新数据库文件,通过crontab每天自动运行
	2./usr/bin/locate     #查询功能
	3./etc/updatedb.conf  #配置查询的条件
	4./var/lib/mlocate/mlocate.db #存放文件信息的数据库

使用方式:
[root@mysql5 ~]# locate passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/lib64/security/pam_passwdqc.so
/lib64/security/pam_unix_passwd.so
.....

  locate查询很快速,但是查找条件过于单一,并且不是实时的。例如查找十分钟内系统修改过的文件,locate就无能为力了。

二、find:

  find在条件指定的目录结构中搜索文件,并执行指定的操作。可以定制很多查找条件,功能很给力。

命令格式:find [options] [查找路径] [查找条件] [处理动作]

1.默认设置:

查找路径:默认当前目录;

查找条件:默认为查找路径下的所有文件;

处理动作:默认为显示,即echo。

2.查找条件:

-name "文件名称",支持使用globbing;

ps:file globbing:星号(*)用于匹配文件名中任意字符;问号(?)匹配文件中的单个字符;方括号([若干字符])匹配括号中的任一一个字符.注:file globbing不是严格的正则表达式。

[root@mysql5 ~]# find /etc/passwd  -name passwd
/etc/passwd

-iname “文件名称”,查找不区分字符大小写;

[root@mysql5 ~]# touch abcd
[root@mysql5 ~]# touch aBCd
[root@mysql5 ~]# ls
abcd  aBCd  anaconda-ks.cfg  install.log  install.log.syslog
[root@mysql5 ~]# find . -name abcd
./abcd
[root@mysql5 ~]# find . -iname abcd
./abcd
./aBCd

  -user UserName:根据属主查找

  -group GroupName:根据属组查找

   -uid UID 根据用户id查找

   -gid GID 根据组ID查找

   -nouser:查找没有属主的文件(ps:假若当前用户tom创建了一个文件a,现在tom用户被删除了。文件a就是没有属主属组的)

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

[root@mysql5 ~]# useradd tom
[root@mysql5 ~]# passwd tom 
Changing password for user tom.
New password: 
BAD PASSWORD: it is WAY too short
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@mysql5 ~]# touch a
[root@mysql5 ~]# chown tom.tom a
[root@mysql5 ~]# ls -la a
-rw-r--r--. 1 tom tom 0 Dec 30 01:54 a
[root@mysql5 ~]# userdel -r tom
[root@mysql5 ~]# ls -la a
-rw-r--r--. 1 500 500 0 Dec 30 01:54 a
[root@mysql5 ~]# find -nouser
./a
[root@mysql5 ~]#

3.组合条件:

-a:与,同时满足
-o:或
-not,!:非取反

[root@mysql5 ~]# touch 1 22 ab
[root@mysql5 ~]# ls
1  22  a  ab  abcd  aBCd  anaconda-ks.cfg  install.log  install.log.syslog
[root@mysql5 ~]# find -name a -o -name 1
./a
./1

4.根据文件类型或大小查找:

1)-type:根据文件类型查找:

f:普通文件

d:目录

b:块文件

c:字符文件

l:符号链接设备

p:命名管道设备

s:套接字

[root@mysql5 ~]# ls
1  22  a  ab  abc  abcd  aBCd  anaconda-ks.cfg  install.log  install.log.syslog  test
[root@mysql5 ~]# find -type d 
.
./abc
./test

 

2)-size:根据文件大小查找

-size [+|-] #Unit

eg.-size +2M 查找大于2M的文件,常用的单位:k,M,G

5.根据时间戳查找:

  p_w_picpath

find可以根据文件的时间戳来查找:

-amin n   查找系统中最后N分钟访问的文件
-atime n  查找系统中最后n天小时访问的文件
-cmin n   查找系统中最后N分钟被改变文件状态的文件
-ctime n  查找系统中最后n天被改变文件状态的文件
-mmin n   查找系统中最后N分钟被改变文件数据的文件
-mtime n  查找系统中最后n天被改变文件数据的文件
#min后接时间是分钟,time后接时间是天数

6.根据文件权限查找:

-perm [+|-] MODE
    MODE:精确匹配
    +MODE:任何一位用户的任何一位权限匹配即可,常用于查找某类的某特定权限是否存在
    -MODE:每类用户的指定要检查的权限位都要匹配
[root@mysql5 ~]# ls -l
total 52
-rw-r--r--. 1 root root     0 Dec 30 02:01 1
-rw-r--r--. 1 root root     0 Dec 30 02:01 22
-rwxrwxrwx. 1  500  500     0 Dec 30 01:54 a
-rw-r--r--. 1 root root     0 Dec 30 02:01 ab
drwxr-xr-x. 2 root root  4096 Dec 30 02:04 abc
-rw-r--r--. 1 root root     0 Dec 30 01:48 abcd
-rw-r--r--. 1 root root     0 Dec 30 01:48 aBCd
-rw-------. 1 root root  1417 Dec 29 18:56 anaconda-ks.cfg
-rw-r--r--. 1 root root 27312 Dec 29 18:55 install.log
-rw-r--r--. 1 root root  7764 Dec 29 18:54 install.log.syslog
drwxr-xr-x. 2 root root  4096 Dec 30 02:04 test
[root@mysql5 ~]# find -perm 777
./a

7.处理动作:

文件找到了,我们经常要执行比如删除,复制,修改等等操作

 

-print :打印在标准输出上
    -ls:以长格式输出各文件信息
    -exec COMMAND {} \;:对查找到的文件执行指定的命令
    -ok COMMAND {} \;:交互式的-exec
        find把查找到的所有文件一次性地传递给-exec 执行
find | xargs COMMAND
[root@mysql5 ~]# find /etc -name passwd -print -exec cat  {} \;
/etc/pam.d/passwd
#%PAM-1.0
auth       include	system-auth
account    include	system-auth
password   substack	system-auth
-password   optional	pam_gnome_keyring.so
/etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
[root@mysql5 ~]# find /etc -name passwd -print -ls
/etc/pam.d/passwd
785290    4 -rw-r--r--   1 root     root          146 Feb 22  2012 /etc/pam.d/passwd
/etc/passwd
786527    4 -rw-r--r--   1 root     root         1338 Dec 30 01:55 /etc/passwd

8.find还有很多很多命令,常用的就是

-name,-iname,-user,-group,-uid,-gid,-nouser,-nogroup,-size,-type,-atime,-perm,-exec,-ok,-ls  
| xargs COMMAND。若还没有想找的,可以man find。