1、which搜索有一定的局限性,找存放在PATH变量里的路径,而且文件是有可执行权限的。

2、whereis也有一定的局限怀,它只是在某一些目录下去搜,像bin、usr、man下这些目录去搜索相关的。

3、locate 也可以查找,若没安装,需要安装 yum install -y mlocate,再直接用命令updatedb就可以了(可以生成/var/lib/mlocate/mlocate.db),就可以用这个命令查找,但是不精准。而且它搜索不到/tmp/下的。


前面三个都一定的局限性,要想精准的搜索,得用到find命令。

find   查找路径    所查找的内容

  -name            按名称

  -type              按类型

  -mtime          创建时间或更改时间(大于就是+     小于就是-)

  -mmin           时间以分钟为单位

  -inum            按inode号

       -maxdepth 1        如果只想找到当前目录下(不要子目录以及子目录的子目录)的文件或目录


#查找/tmp/下所有目录

[root@wy ~]# find /tmp/ -type d

/tmp/

/tmp/.ICE-unix

/tmp/333

/tmp/222

/tmp/222/dir2


#查找/tmp/下所有文件

[root@wy ~]# find /tmp/ -type f

/tmp/333/33.txt

/tmp/333/12.txt

/tmp/222/file2

/tmp/222/file


#查找/tmp/下创建时间大于10天的

[root@wy ~]# find /tmp/ -mtime +10


#查找/tmp/下创建时间小于10天的

[root@wy ~]# find /tmp/ -mtime -10

/tmp/

/tmp/.ICE-unix

/tmp/333


#查找/tmp/下五分钟之内的

[root@wy ~]# find /tmp/ -mmin -5


#按inode号查找

[root@wy ~]# find /tmp/ -inum 141418


#查找/tmp/下30天之前的日志并删除

[root@wy ~]# find /tmp/ -type f -mtime +30 | xargs rm


#查找/tmp/下所有文件并重命名.bak

[root@wy ~]# find /tmp/ -type f | xargs -i mv {} {}.bak

解释说明:

如果再把重命名为.bak的还原,则这样:ls  /tmp/|sed 's/.bak$//g'|xargs -i mv {}.bak {}


#当前目录下权限为777的文件

find . -type f -perm 777


#搜索/tmp/目录下属主为user1属组为test的文件

find /tmp/ -type f -user user1 -group test