How to use the search command of the Linux cloud server

06-06-2023

The knowledge points of how to use the search command of the Linux cloud server are not well understood by most people, so the editor summarizes the following content for you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can read it After finishing this article, you can gain something. Let's take a look at how to use the search command of this Linux cloud server.


whereis

The whereis command is used to find binary files, source code and help manuals. whereis only searches within the scope of several commonly used installation directories, and does not search all files on the entire system. Although this is the limitation of whereis, it is also for this reason that the search speed of whereis is very fast. Also worth noting that whereis doesn't look for those shell builtin commands.

For example, we want to find the ls command:

whereis ls

The results are as follows, where /bin/ls is the path of ls, and the other two are help manuals path of.

ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

If we Copy ls to the home directory and /usr/bin/ls directory, and execute the whereis command again:

cp /bin/ls ~ cp /bin/ls /usr/bin/ls whereis ls

The results are as follows. It can be found that whereis does not search the home directory, because the home directory is not one of the commonly used installation directories.

ls: /bin/ls /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

Use whereis to find the built-in commands of these shells:

whereis cd

The result is as follows, only the path of the help manual is displayed.

cd: /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz

locate

locate command is a search tool based on the file database (/var/lib/mlocate/mlocate.db), which is a mirror image of the entire file system. The search mode of the locate command is fuzzy matching by default, which means that all files containing the file name will be found, so the returned result is usuallymore. It's worth noting that the file database is usually updated on a daily basis, so it may not be possible to find some files that were just created or deleted. We can execute the updatedb command to update the database manually. Since the search is based on the file database rather than the file system, the execution speed of locate is also quite fast.

Still taking ls as an example to search:

locate ls

We will get a lot of results containing ls. In order to make the output more accurate, you can use the -b command, that is, use the exact match mode to find.

locate -b "\ls"

This way the output will only contain the path where the ls command is located.

/bin/ls

which

The search scope of which command is the environment variable PATH, and by default only the first result is returned, and the execution speed is very fast. If we are looking for an alias, the which command will also map to the real path corresponding to the alias before looking.

For example, if we define an alias named ll, the search command is as follows:

which ll

The result is as follows:

alias ll ='ls -l --color=auto' /bin/ls

If we want to display all the results, just add the -a command.

which -a your_command

type

The type command is used to display the type of a command, such as aliases, keywords, functions, built-in commands, files, etc. . Like the whereis command, the type command only searches within the scope of several commonly used installation directories.

Display the type of cd command:

type cd cd is a shell builtin

Display the type of a binary file:

type sudo sudo is /usr/bin/sudo

Show aliases:

type ls ls is aliased to `ls --color=auto'

find

Among the five search commands, the find command is the most powerful and the slowest Order. Different from the other four commands, the find command is based on the search of the file system, one file node by one file node. The find command can even perform subsequent operations on the results of the search.

Basic usage of the find command:

find [path] [option] [action]

In the home directory and its subdirectoriesIn the file system, find the file named aaa:

find ~ -name 'aaa'

In the entire file system, find the file within 24 hours of the modification time:

find / -mtime 0

In the nginx web directory and its subdirectories, look for files whose user is nginx:

find /usr/share/nginx/html / -user nginx

In the current directory, find files with permission 744:

find -perm -0744

In the current directory, find the file name aaa file, and display its details:

find -name 'aaa' -exec ls -l {} \;


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us