Detailed explanation of linux command find locate whereis which type

Linux command summary find command find , locate , whereis , which , type

We often need to find a file in the system, so how can we accurately and efficiently determine the specific location of a file in the system in a Linux system? Here I summarize several commands used to find files in linux system.

1. find command _

find is the most common and powerful find command, it can find any type of file.

The general format of the find command is: find < specified directory >< specified condition >< specified action > , that is, find pathname-options [-print -exec -ok]

Parameter explanation:

pathname : pathname is the searched directory and its subdirectories, by default the current directory

Common options :

-name : find files by filename

-user : Find files by their owner

-group : Find files by group they belong to

-perm : Find files by file permissions

-prune : do not look in the currently specified directory

For example: it is known that there is a file named passwd in the /etc , /etc/pam.d and /user/bin directories , let's take a look at the role of -prune

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

[root@Gin /]# find -name "passwd" ## Find a file named passwd in the current directory and its subdirectories

./usr/bin/passwd

./etc/passwd

./etc/pam.d/passwd

 

[root@Gin /]# find . -path ./etc -prune -o -name "passwd" -print ##In the current directory and its subdirectories (except the /etc directory and its subdirectories) to find the name passwd document.

./usr/bin/passwd

[root@Gin /]# find . -path ./etc -prune -o -name "passwd" ##The difference between with -print parameter and without -print parameter

./usr/bin/passwd

./etc

 

[root@Gin /]# find . -path ./usr -prune -o -name "passwd" -print ##Search for the name passwd in the current directory and its subdirectories (except /usr and its subdirectories) document.

./etc/passwd

./etc/pam.d/passwd

 

[root@Gin scripts]# cp /etc/passwd ./

find / \( -path /usr/bin -o -path /etc \) -prune -o -name "passwd" -print ## is in the current directory and its subdirectories (except for the /usr/bin directory and its subdirectories ; /etc directory and its subdirectories) for a file named passwd. Since parentheses are not directly recognized by the command line, the escape character \ is used, with spaces before and after.

/gin/scripts/passwd

Note: When the find command does not add any parameters, it means that the search path is the current directory and its subdirectories. The default action is -print , that is, no results are filtered, that is, all files are output.

-mtime -n +n : Find files according to the file modification time, -n indicates that the file modification time is within n days from now , +n indicates that the file modification time is n days ago

-type : Find a file of a certain type (b : block device file; d : directory file; c : character device file; p : pipe file; l : link file; f : normal file )

-nogroup : Find files with no valid group, that is, the group to which the file belongs does not exist in /etc/group

-nouser ; find files with no valid owner, that is, the owner of the file does not exist in /etc/passwd

2. locate command _

The locate command is actually another way of writing "find -name" , but the search method is different from find , which is much faster than find . Because it does not search specific directories, but searches for the specified file in a database (/var/lib/locatedb) . The secondary database contains all the information of the local files. This database is automatically created by the Linux system. The database is updated by the updatedb program. The updatedb is created periodically by the cron daemon . By default, it is updated once a day . to the latest updated file, unless you manually update the database with the updatedb command before finding the file with the locate command.

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 ##Before updating the database

[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照样能搜索到该文件的信息,,尽管此时该文件已经不存在了。

3whereis命令

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:

4which命令

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

5type命令

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

 

 引用:http://www.cnblogs.com/ginvip/p/6539393.html
细节影响成败,感谢 琴酒网络 的总结整理。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325396600&siteId=291194637