How does Linux find files?

09-06-2023

Ways to find files in Linux: 1. Use the find command to find files according to their attributes, such as file name, file size, owner, belonging 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 which command to view the location of executable files. 5, use the type command to find. 6. use the grep command to find it.

Find command to find files.

Using the find command of Linux, you can search the directory tree by different search criteria, such as name, type, owner, size, etc. 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 for a file named test.file, and will provide its storage location. You can try it with an existing file name on your Linux. The find command sometimes takes several minutes to find the entire directory tree, especially if there are many files and directories in the system. To significantly reduce the time, you can specify the directory 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 can also search files according to time, size, owner, permissions and other options. If you need more information about these options, you can check the manual of Linux find command.

# man find

Locate command to find files

To use the locate command in Linux, you need to install it first.

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

Compared with find, locate can 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 to find files.

The whereis command can only be used to search program names, and only binary files (parameter -b), man description files (parameter -m) and source code files (parameter -s) are searched. If the parameter is omitted, all information is returned.

Examples of whereis command:

  $ whereis grep

Description: whereis to find specific files, which can only be used to find binary files, source code files and man pages.

Which command to find files

Which checks the location of executable files, which can only be used by programs with environment variables set.

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.。 With which command, you can check whether a system command is available and the exact location where the command is executed.

Use example of which command:

  $ which grep

Type command to find files

In fact, the type command is not a search command. It is used to distinguish whether a command is provided by the shell itself or by an independent binary file outside the shell. Using the -p parameter displays the path of the external command, similar to which command.

Examples of the use of the type command:

  $ type cd

The system will prompt that the cd is the shell's build-in.

  $ type grep

You will be prompted that grep is an external command and the path of the command will be displayed.

  $ type -p grep

After adding the -p parameter, it is equivalent to which command.

Grep command to find files

Grep searches according to the contents of the file, and matches every line of the file according to a given patter.

Basic format:

find [options] expression

Main parameters:

[options] Main parameters:

-c: Only the count of matching lines is output.

-I: case-insensitive

-h: Do not display file names 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 an error message that does not exist or has no matching text.

-v: Displays all lines that do not contain matching text.

Main parameters of pattern regular expression:

: Ignore the original meaning of special characters in regular expressions.

: Matches the start line of a regular expression.

$: Matches the end line of a regular expression.

>: to the end of the line matching the regular expression.

[]: single character, such as [A], that is, A meets the requirements.

[-]: range, such as [A-Z], that is, A, B, C and 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* # displays all the lines containing test in the files starting with d.

2) grep' test' aabbcc # displays the lines containing test in aa, bb and cc files.

3) grep' [a-z] {5}' aa # displays all lines containing strings with at least 5 consecutive lowercase characters per line.

4)grep magic /usr/src # shows that the files in the /usr/src directory (excluding subdirectories) contain magic lines.

5)grep -r magic /usr/src # shows that the files (including subdirectories) in the /usr/src directory contain lines of magic.

6)grep -w pattern files: only match the whole word, not part of the string (such as matching 'magic' instead of 'magical'),

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