Home > Article > Operation and Maintenance > How to find files in Linux
How to find files in Linux: 1. Use the find command to search according to the attributes of the file, such as file name, file size, owner, group, whether it is empty, access time, modification time, etc. 2. Use the locate command to check the file location with the database. Whereis command can only be used to search for binary files, source code files, and man manual pages.. 4. Use the which command to view the location of the executable file. 5. Use the type command to search. 6. Use grep command to search.
find command to find files
Using the Linux find command, you can use different search criteria, such as name, type, owner, size, etc. Search the directory tree. The basic syntax is as follows:
# find path expression search-term
The following is an example of using the find command to find a specific file based on the file name:
# find -name test.file
The command will search the entire directory tree to find a file named test.file, And its storage location will be provided. You can try it using an existing filename on your Linux.
The find command can sometimes take several minutes to find the entire directory tree, especially if there are many files and directories in the system. To significantly reduce time, you can specify the directories to search. For example, if you know that test.file exists in /var, there is no need to search other directories. In this way, you can use the following command:
# find /var -name test.file
find You can also search for files based on options such as time, size, owner, permissions, and more. If you need more information about these options, check out the manual for the Linux find command.
# man find
locate command finds files
To use the locate command in Linux, you first need to install it.
If you are using Ubuntu, run the following command to install locate:
# apt-get update# apt-get install mlocate
If you are using CentOS, run the following command to install locate:
# yum install mlocate
Phase Compared with find, use locate to find files in the database faster. To update the search database, run the following command:
# updatedb
Syntax for finding files using locate:
# locate test.file
Similar to the find command, the locate command also has a variety of options to filter the output. To learn more you can check the manual of Linux Locate command.
# man locate
whereis command searches for files
whereis command can only be used to search for program names, and only searches for binary files (parameter -b) and man description files (parameter -m) and source code files (parameter -s). If parameters are omitted, all information is returned.
Usage examples of whereis command:
$ whereis grep
Description: whereis looks for specific files and can only be used to find binary files, source code files and man manual pages
The which command searches for files
which checks the location of the executable file. Only programs with environment variables set can use it
The purpose of the "which" command is to search for the location of a particular system command within the paths specified by the PATH variable, and returns the first search result.. Using the which command, you can check whether a system command is available and the exact location where the command is executed.
Usage examples of which command:
$ which grep
type command to find files
type command is not actually a search command, it is used to distinguish a certain Whether the command comes with the shell or is provided by an independent binary file outside the shell. Using the -p parameter will display the path of the external command, similar to the which command.
Usage examples of type command:
$ type cd
The system will prompt that cd is the shell’s built-in command (build-in).
$ type grep
The system will prompt that grep is an external command and display the path of the command.
$ type -p grep
After adding the -p parameter, it is equivalent to the which command.
The grep command searches for files
grep searches based on the contents of the file, and will match each line of the file according to the given pattern (patter).
Basic format:
find [options] expression
Main parameters:
[options]Main parameters:
-c: Output only matching lines count.
-i: Case-insensitive
-h: File names are not displayed when querying multiple files.
-l: When querying multiple files, only the file names containing matching characters are output.
-n: Display matching lines and line numbers.
-s: Do not display error messages that do not exist or have no matching text.
-v: Display all lines that do not contain matching text.
pattern Main parameters of regular expression:
\: Ignore the original meaning of special characters in the regular expression.
^: Matches the starting line of the regular expression.
$: Matches the end line of the regular expression.
\02e4e1fd167bb101ea582aba708e7248: To the end of the line matching the regular expression.
[ ]: A single character, such as [A], that is, A meets the requirements.
[ - ]: Range, such as [A-Z], that is, A, B, C to Z all meet the requirements.
.: All single characters.
*: There are characters, and the length can be 0.
Example:
grep -r "string" is very convenient
1)grep 'test' d*
#Display all lines containing test in files starting with d
2)grep 'test' aa bb cc
#Display the lines containing test in the aa, bb, cc files
3)grep '[a-z]\{5\ }' aa
#Display all lines that contain strings with at least 5 consecutive lowercase characters per line of string
4)grep magic /usr/src
#Display /usr Files in the /src directory (excluding subdirectories) contain magic lines
5)grep -r magic /usr/src
#Display files in the /usr/src directory (including Subdirectory) contains lines containing magic
6)grep -w pattern files
: only matches the entire word, not part of the string (such as matching 'magic', not 'magical' ),
The above is the detailed content of How to find files in Linux. For more information, please follow other related articles on the PHP Chinese website!