linux命令总结之查找命令find、locate、whereis、which、type

我们经常需要在系统中查找一个文件,那么在Linux系统中我们如何准确高效的确定一个文件在系统中的具体位置呢?一下我总结了在linux系统中用于查找文件的几个命令。

1、find命令

find是最常用也是最强大的查找命令,它可以查找任何类型的文件。

find命令的一般格式为:find <指定目录><指定条件><指定动作>,即find pathname -options [-print -exec -ok]

参数解释:

pathname:pathname为搜索的目录及其子目录,默认情况下为当前目录

常用的option选项:

-name:按文件名来查找文件

-user:按照文件的属主来查找文件

-group:按照文件所属的组来查找文件

-perm:按照文件权限来查找文件

-prune:不在当前指定目录中查找

例如:已知在/etc,/etc/pam.d以及/user/bin目录下都有一个名为passwd的文件,我们看一下-prune的作用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@Gin /] # find -name "passwd"  ## 为在当前目录及其子目录下下查找名为passwd的文件
. /usr/bin/passwd
. /etc/passwd
. /etc/pam .d /passwd
 
[root@Gin /] # find . -path ./etc -prune -o -name "passwd" -print  ##为在当前目录及其子目录(除了/etc目录及其子目录)下查找名为passwd的文件。
. /usr/bin/passwd
[root@Gin /] # find . -path ./etc -prune -o -name "passwd"  ##区别带-print参数与不带-print参数
. /usr/bin/passwd
. /etc
 
[root@Gin /] # find . -path ./usr -prune -o -name "passwd" -print  ##为在当前目录及其子目录(除了/usr目录及其子目录)下查找名为passwd的文件。
. /etc/passwd
. /etc/pam .d /passwd
 
[root@Gin scripts] # cp /etc/passwd ./
find / \( -path /usr/bin -o -path /etc \) -prune -o -name "passwd" -print   ##为在当前目录及其子目录(除了/usr/bin目录及其子目录;/etc目录及其子目录)下查找名为passwd的文件。由于命令行不能直接识别圆括号,因此要用转义字符\,在和前后都要有空格。
/gin/scripts/passwd

注意:find命令不加任何参数时,表示搜索路径为当前目录及其子目录,默认的动作为-print,即不过滤任何结果,也就是说输出所有的文件。

-mtime -n +n:按照文件修改时间来查找文件,-n表示文件修改时间距现在n天以内,+n表示文件修改时间据现在n天以前

扫描二维码关注公众号,回复: 6179737 查看本文章

-type:查找某一类型的文件(b:块设备文件;d:目录文件;c:字符设备文件;p:管道文件;l:链接文件;f:普通文件)

-nogroup:查找无有效所属组的文件,即文件所属的组在/etc/group中不存在

-nouser;查找无有效所属主的文件,即文件的所属主在/etc/passwd中不存在

2、locate命令

locate命令实际是”find -name”的另一种写法,但是查找方式跟find不同,它比find快得多。因为它不搜索具体目录,而是在一个数据库(/var/lib/locatedb)中搜索指定的文件。次数据库含有本地文件的所有信息,此数据库是linux系统自动创建的,数据库由updatedb程序来更新,updatedb是由cron daemon周期性建立的,默认情况下为每天更新一次,所以用locate命令你搜索不到最新更新的文件,除非你在用locate命令查找文件之前手动的用updatedb命令更新数据库。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@Gin scripts] # echo "I love linux" >locatetest
[root@Gin scripts] # locate locatetest ##更新数据库前
[root@Gin scripts] # find -name "locatetest"
. /locatetest
[root@Gin scripts] # updatedb ##更新数据库后
[root@Gin scripts] # locate locatetest
/gin/scripts/locatetest
[root@Gin scripts] # rm -f locatetest ##执行删除文件后
[root@Gin scripts] # find -name "locatetest"
[root@Gin scripts] # locate locatetest
/gin/scripts/locatetest
[root@Gin scripts] # locate locatetest
[root@Gin scripts] #

注意:每次有新文件更新和删除之后,在updatedb之前数据库中保存的文件信息不会改变,即新添加一个文件之后,updatedb之前用locate搜索不到指定的文件。同样再删除一个文件信息已经在数据库中的文件时,updatedb之前用locate照样能搜索到该文件的信息,,尽管此时该文件已经不存在了。

3、whereis命令

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

?
1
2
3
4
5
6
7
8
9
10
[root@Gin scripts] # whereis --help
whereis [ -sbmu ] [ -SBM dir ... -f ] name...
[root@Gin scripts] # whereis find
find : /bin/find /usr/bin/find /usr/share/man/man1/find .1.gz /usr/share/man/man1p/find .1p.gz
[root@Gin scripts] # whereis -b find
find : /bin/find /usr/bin/find
[root@Gin scripts] # whereis -m find
find : /usr/share/man/man1/find .1.gz /usr/share/man/man1p/find .1p.gz
[root@Gin scripts] # whereis -s find
find :

4、which命令

which命令是在PATH变量指定的路径中搜索指定的系统命令的位置。用echo $PATH可显示当前PATH变量的值。

?
1
2
3
4
[root@Gin scripts] # echo $PATH
/usr/local/sbin : /usr/local/bin : /sbin : /bin : /usr/sbin : /usr/bin : /root/bin
[root@Gin scripts] # which find
/bin/find

5、type命令

type命令主要用于区分一个命令到底是shell自带的还是外部独立的二进制文件提供的。如果是shell自带的则会提示此命令为shell buildin,否则会列出命令的位置。例如:cd为shell自带的命令,当用which查找时,which会按照PATH变量设置的路径进行搜索,结果显示no cd in…;用type cd则显示cd为shell buildin命令。ssh不是shell自带命令,用type时会显示ssh的路径。

?
1
2
3
4
5
6
[root@Gin scripts] # which cd
/usr/bin/which : no cd in ( /usr/local/sbin : /usr/local/bin : /sbin : /bin : /usr/sbin : /usr/bin : /root/bin )
[root@Gin scripts] # type cd
cd is a shell builtin
[root@Gin scripts] # type ssh
ssh is /usr/bin/ssh

转载

猜你喜欢

转载自blog.csdn.net/qq_35782149/article/details/80994342