Five search commands in Linux: grep, find, locate, whereis, and which

Source: Internet
Author: User

1,Grep

Grep (General regular expression parser, universal rule Expression Analysis Program) is a powerful text search tool that can use regular expressions to search text and print matching rows.

Its syntax is as follows:

Grep [Option] Pattern [File Name]

. Match any character
* Match 0 or more * characters before
^ Match the beginning of a row
$ Match the end of a row
[] Matches any character in []. [] indicates the range,
For example, [A-Z] indicates any one of the letters A to Z.
\ Conversion character
The options in the command are:
-B: displays the block number.
-C only displays the total number of lines in the mode in each specified file
-Uppercase and lowercase letters in I Mode
-H does not display the file name in the include mode on this row.
-L only display the file name in the include Mode
-N: row number of the row in the display mode
-S: if the specified file does not exist or is unreadable, no error message is displayed.
-V: Display All rows that do not contain the Mode

Where, pattern is the string to be matched. For example, in the/etc/passwd file, find the line containing the "Carey" character:

$Grep Carey/etc/passwd

To make good use of the grep tool, we need to write a regular expression. Therefore, we will not explain all the functions of grep here. We will only list a few examples to illustrate how to write a regular expression.

$Grep 'test' D *

Display all the lines containing test in files starting with D.

$Ls-L | grep '^ public'

Filter the LS-L output content in the MPs queue and display only the rows starting with "public.

The followed ^ character forces the grep command to find public only at the beginning of each line. The entire search mode (pattern) is enclosed in single quotes, so that shell ignores them. Shell removes single quotes and sends the search mode to the grep command.

$Grep-I 'Hello world' menu. h main. c

The Rows matching "Hello World" in the menu. h and Main. c files are displayed, case insensitive.

For example:

Grep 'sample'-R *

2,Find

Find is the most common and powerful search command. You can use it to find any file you want.

The format of find is as follows:

  Find <Specify directory> <Specify conditions> <Action>

-<Specified directory>: the directory to be searched and all its subdirectories. The current directory is used by default.

-<Specified condition>: the features of the file to be searched.

-<Specified action>: perform specific processing on the search results.

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

Find instance:

$Find.-Name 'my * '-ls

Search for all files in the current directory (including subdirectories, the same below) whose names start with "my" and display their details.

$Find/home-user RTOS

Search for files whose owner is RTOS in/home

$Find.-Type F-mmin-10

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

$Find/-type F-size + 100 m

Search for all files larger than mb IN THE SYSTEM

Note:If you are looking for an archive, using find is a good idea. However, because find consumes a lot of hard disks while searching for data, there is no need to use find! There are better commands that can replace yo, that is, whereis and locate ~

3,Locate

The locate command is actually another method of writing "Find-name", but it is much faster than the latter because it does not search for a specific directory, instead, search for a database (/var/lib/locatedb) that contains information about all local files. The Linux system automatically creates the database and updates the database once a day. Therefore, the latest changed files cannot be found using the locate command. To avoid this problem, you can use the updatedb command to manually update the database before using locate.

Use instance of the locate command:

$Locate/etc/SH

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

$Locate-I ~ /M

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

4,Whereis

The whereis command can only be used for searching program names, and only binary files (parameter-B), man description files (parameter-m), and source code files (parameter-S) can be searched ). If the parameter is omitted, all information is returned.

Examples of using the whereis command:

$Whereis grep

Grep:/bin/grep/usr/share/man/man1p/grep.1p.gz/usr/share/man/Man1/grep.1.gz

5,Which

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

Use instance of the which command:

$Which grep

/Bin/grep

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.