Five Linux commands take you to find the target file from massive files, practical Linux file search skills: find, locate, grep and other commands in detail

When we need to find a file in the Linux system, we may be confused because of the huge number of files or not knowing the specific location of the file. However, the Linux system provides many commands and tools that can help us quickly find files in the file system. The following will introduce five commonly used Linux file search commands and how to use them in detail.

1. find command

The find command can search for files in the specified directory, and supports searching by file name, file type, and file size. Its basic syntax is as follows:

find [路径] [选项] [表达式]

Among them, the path is the starting directory of the search, the option is used to control the scope and method of the search, and the expression is used to specify the search condition.

For example, to find a file named test.txt, you can use the following command:

find / -name test.txt

This command starts searching from the root directory, finds all files named test.txt, and outputs their paths.

2. locate command

The locate command can quickly find files in the system, which is faster than the find command, but the file index needs to be updated first. Its basic syntax is as follows:

locate [选项] 文件名

Among them, the option is used to control the scope and method of the search, and the file name is the file name to be searched or a string containing the file name.

For example, to find files whose filename contains test, you can use the following command:

locate test

This command will find all files containing the test string in the system and output their paths.

3. whereis command

The whereis command can find commands, source files and help documents, etc. Its basic syntax is as follows:
 

whereis [选项] 命令名

Among them, the option is used to control the type of search, and the command name is the command or file name to be searched.

For example, to find the path of the command ls, you can use the following command:
 

whereis ls

This command will search the executable file, source file and help document of the ls command in the system, and output their paths.

4. which command

The which command can find the executable file path of the command. Its basic syntax is as follows:
 

which [选项] 命令名

Among them, the option is used to control the type of search, and the command name is the command to be searched.

For example, to find the executable file path of the command ls, you can use the following command:
 

which ls

This command will find the executable file path of the ls command in the system and output its path.

5. grep command

The grep command can search for a specified string in a file. Its basic syntax is as follows:
 

grep [选项] 字符串 文件名

Among them, the option is used to control the search method, the character string is the character string to be searched, and the file name is the file name to be searched.

For example, to find the line containing hello in the file test.txt, you can use the following command:
 

grep "hello" test.txt

This command will look for lines containing hello in the test.txt file and output these lines.

In short, the Linux system provides a wealth of commands and tools that can help us find files quickly. In actual use, you can search for files more efficiently by selecting appropriate commands and options according to different needs.

Guess you like

Origin blog.csdn.net/weixin_42279822/article/details/130722493