The Linux operating system features five search-related commands: whereis, locate, which, type, and find. Each command has its applicable scenarios and methods. This article summarizes the usage of these five search commands and includes examples to illustrate their application, serving as a reference for system administrators using Linux cloud servers.
Here's the translated text in professional, fluent English with the appropriate use of computer and cloud computing industry terminology:
Using 5 Search Commands on a Linux Cloud Server
The Linux operating system features five search-related commands: whereis, locate, which, type, and find. Each command has its applicable scenarios and methods. This article summarizes the usage of these five search commands and includes examples to illustrate their application, serving as a reference for system administrators using Linux cloud servers.
The whereis command is used to locate binary files, source code, and manual pages. It searches within a few standard installation directories, rather than scanning every file in the system. This limitation of whereis is also the reason for its high search speed. It's important to note that whereis does not search for shell built-in commands.
For instance, to find the ls command:
whereis ls
The result shows, /bin/ls is the path for ls, and the other two are paths to its manuals.
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, and then execute the whereis command again:
cp /bin/ls ~
cp /bin/ls /usr/bin/ls
whereis ls
The result shows that whereis did not search the home directory, as it is not one of the standard installation directories.
ls: /bin/ls /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
Using whereis to find a shell built-in command like cd:
whereis cd
The result only displays the path to the manual.
cd: /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz
The locate command is a search tool based on a file database (/var/lib/mlocate/mlocate.db), which is a snapshot of the entire filesystem. locate typically performs a fuzzy match, meaning it will find all files containing the filename, often returning numerous results. It's noteworthy that the file database is usually updated daily, so recently created or deleted files might not be found. You can manually update this database by executing the updatedb command. As it searches based on the file database rather than the filesystem, locate is also quite fast.
Continuing with ls as an example:
locate ls
This yields many results containing ls. To narrow down the output, you can use the -b flag, which employs an exact match search.
locate -b "\ls"
This will result in output containing only the paths where the ls command is located.
/bin/ls
The which command searches within the PATH environment variable and typically returns only the first result, operating at high speed. If we're searching for an alias, which will map it to the corresponding real path before searching.
For example, if we defined an alias named ll, the search command is:
which ll
The result is as follows:
alias ll='ls -l --color=auto'
/bin/ls
To display all results, add the -a flag.
which -a your_command
The type command is used to display the type of a command, such as an alias, keyword, function, built-in command, or file. Like the whereis command, type also searches within a few standard installation directories.
Display the type of the cd command:
type cd
cd is a shell builtin
Display the type of a binary file:
type sudo
sudo is /usr/bin/sudo
Display an alias:
type ls
ls is aliased to `ls --color=auto'
Among the five search commands, the find command is the most powerful but also the slowest. Unlike the other four commands, find operates on the filesystem, searching node by node. It can even perform operations on the search results. Due to its extensive capabilities, this article only showcases some common examples of find.
Basic usage of the find command:
find [path] [option] [action]
To find a file named aaa in the home directory and its subdirectories:
find ~ -name 'aaa'
To find files in the entire filesystem that were modified within the last 24 hours:
find / -mtime 0
To find files in the Nginx web directory and its subdirectories that belong to the user nginx:
find /usr/share/nginx/html/ -user nginx
To find files with permissions set to 744 in the current directory:
find -perm -0744
To find a file named aaa in the current directory and display its details:
find -name 'aaa' -exec ls -l {} \;
These are the introductions to the five search commands in the Linux operating system, hopefully providing useful information to users.
23-02-2024 02:02:07
22-02-2024 03:19:32
22-02-2024 03:16:03
22-02-2024 03:14:03
22-02-2024 03:11:58