[Switch] Five search commands for Linux

Original address: http://www.ruanyifeng.com/blog/2009/10/5_ways_to_search_for_files_using_the_terminal.html

 

Recently, I'm learning Linux and here are some notes.

When using a computer, you often need to find files.

In Linux, there are many ways to do this. The foreign website LinuxHaxor summarizes five commands, you can see how many you know. Most programmers may use 2 or 3 of them frequently, and there should be not many people who are familiar with these 5 commands.

1. find

find is the most common and powerful find command, you can use it to find any file you are looking for.

The usage format of find is as follows:

  $ find <specified directory> <specified condition> <specified action>

  - <specified directory>: The directory to be searched and all its subdirectories. Defaults to the current directory.

  - <specified condition>: The characteristics of the file to be searched.

  - <Specified Action>: Perform specific processing on search results.

If no parameters are added, find searches the current directory and its subdirectories by default, and does not filter any results (that is, returns all files), and displays them all on the screen.

Use examples of find:

  $ find . -name 'my*'

Search the current directory (including subdirectories, the same below), all files whose file names begin with my.

  $ find . -name 'my*' -ls

Searches the current directory for all files whose names begin with my, and displays their details.

  $ find . -type f -mmin -10

Search the current directory for all regular files that have been updated in the past 10 minutes. If the -type f parameter is not added, it will search for ordinary files + special files + directories.

2. locate

The locate command is actually another way of writing "find -name", but it is much faster than the latter because it does not search a specific directory, but a database (/var/lib/locatedb), which contains All local file information. The Linux system automatically creates this database and automatically updates it once a day, so the latest changed files cannot be found using the locate command. To avoid this situation, you can use the updatedb command to manually update the database before using locate .

An example of the use of the locate command:

  $ locate /etc/sh

Search for all files starting with sh in the etc directory.

  $ locate ~/m

Search for all files starting with m in the user's home directory.

  $ locate -i ~/m

Search for all files starting with m in the user's home directory, ignoring case.

3. whereis

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

Example of using the whereis command:

  $ whereis grep

4. which

The function of the which command is to search the location of a system command in the path specified by the PATH variable and return the first search result. That is to say, using the which command, you can see whether a system command exists, and where the command is executed.

Example of using the which command:

  $ which grep

5. type

The type command is not actually a search command. It is used to distinguish whether a command is provided by the shell itself or provided by a separate binary file outside the shell. If a command is an external command, using the -p parameter will display the path of the command, which is equivalent to the which command.

Example of using the type command:

  $ type cd

The system will prompt that cd is the built-in command (build-in) of the shell.

  $ 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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327030928&siteId=291194637