在linux中,查找命令主要有find和grep,以及mlocate/locate
其中find主要是文件级别的查找,grep是字符串级别查找,主要查找文件内容。具体区别:
(1)find:是根据文件的属性进行查找,如文件名,文件大小,所有者,所属组,是否为空,访问时间,修改时间等。
(2)grep:是根据文件的内容进行查找,会对文件的每一行按照给定的模式(patter)进行匹配查找。
(3)mlocate/locate 命令用来查找文件或目录,速度快。
一、find命令
find 是文件层次的查找(包括子目录,就是对当前路径树查找)
基本格式:find path name
1、按照文件名查找
(1)find /opt -name abc.txt #在/opt目录下查找文件abc.txt
(2)find /opt -name abc.txt #在/opt目录下文件abc.txt
(3)find /opt -name '*abc*' #使用通配符*。表示在/opt目录下查找文件名中含有字符串‘abc’的文件
(4)find / -name 'abc*' #表示当前目录下查找文件名开头是字符串‘abc’的文件
2、可指定文件类型查找
find / -name "abc*" -type d # 查找类型为目录类型
find / -name "abc*" -type f # 查找类型为文件类型
-type t
True if the file is of the specified type. Possible file types
are as follows:
b block special
c character special
d directory
f regular file
l symbolic link
p FIFO