linux find, locate, where is, which, type

Keywords: Database shell ssh Linux

Article directory

1,find

Find is the most commonly used and powerful find command, which can find any type of file.

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

Parameter interpretation:
Pathname: pathname is the search directory and its subdirectories. By default, it is the current directory

Common option s:
-Name: find files by file name

-user: find files by their owner

-Group: find files by their group

-perm: find files by file permissions

-prune: do not find in the currently specified directory

For example, it is known that there is a file named passwd in the directory / etc, / etc/pam.d and / user/bin. Let's see the function of - prune

[root@Gin /]# find -name "passwd"  ## To 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  ##To find a file named passwd in the current directory and its subdirectories (except the / etc directory and its subdirectories).
./usr/bin/passwd
[root@Gin /]# find . -path ./etc -prune -o -name "passwd"  ##Distinguish between with - print parameter and without - print parameter
./usr/bin/passwd
./etc
 
[root@Gin /]# find . -path ./usr -prune -o -name "passwd" -print  ##To find a file named passwd under the current directory and its subdirectories (except the / usr directory and its subdirectories).
./etc/passwd
./etc/pam.d/passwd
 
[root@Gin scripts]# cp /etc/passwd ./
find / \( -path /usr/bin -o -path /etc \) -prune -o -name "passwd" -print  ##To find a file named passwd under the current directory and its subdirectories (except the / usr/bin directory and its subdirectories; / etc directory and its subdirectories). Because parentheses are not recognized directly on the command line, escape characters \, 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 the file according to the file modification time, - N means the file modification time is within n days from now, + n means the file modification time is within n days from now

-Type: find a type of file (b: block device file; d: directory file; c: character device file; p: pipeline file; l: link file; f: normal file)

-nogroup: find the file without a valid group, that is, the group to which the file belongs does not exist in / etc/group

-nouser; looks for a file without a 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 to write "find name", but the search method is different from find, which is much faster than find. Because it does not search for specific directories, it searches for the specified files in a database (/ var/lib/locatedb). The secondary database contains all the information of the local file. This database is automatically created by linux system. The database is updated by updatedb program. Updatedb is periodically established by cron daemon. By default, it is updated once a day. So you can't search for the latest updated file by using the locate command, unless you manually use the updatedb command before using the locate command to find the file New database.

[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 ##After updating the database
[root@Gin scripts]# locate locatetest
/gin/scripts/locatetest
[root@Gin scripts]# rm -f locatetest ##After deleting files
[root@Gin scripts]# find -name "locatetest"
[root@Gin scripts]# locate locatetest
/gin/scripts/locatetest
[root@Gin scripts]# locate locatetest
[root@Gin scripts]#

Note: every time a new file is updated or deleted, the file information saved in the database before updatedb will not change, that is, after a new file is added, the specified file cannot be searched by locate before updatedb. Also, when deleting a file whose information is already in the database, updatedb can still search for the information of the file by using locate, even though the file no longer exists

So when using locate, execute the updatedb command first

3. Where is command

The where is command can only be used to search for binary (- b), source (- s), and description (- m). If the parameter is omitted, all information is returned.

[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 command

The which command searches the PATH specified by the PATH variable for the location of the specified system command. Use echo $PATH to display the value of the current PATH variable.

[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 command

The type command is mainly used to distinguish whether a command is provided by a shell or an external independent binary. If it comes with the shell, you will be prompted that the command is shell buildin, otherwise the location of the command will be listed. For example: cd is the command of the shell. When you use which to search, which will search according to the PATH set by the PATH variable. The result shows no cd in type cd is the shell buildin command. ssh is not a shell command, and the PATH of ssh will be displayed when type is used.

[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
180 original articles published, 44 praised, 140000 visitors+
Private letter follow

Posted by alexus on Thu, 16 Jan 2020 09:46:02 -0800