Five find commands under Linux: grep, find, locate, Whereis, which

Source: Internet
Author: User

Translated from http://www.cnblogs.com/wanqieddy/archive/2011/07/15/2107071.html

1 , grep

grep (general Regular expression Parser, universal regular expression parser) is a powerful text-search tool that can use regular expressions to search for text and print matching lines.

The syntax for using it is:

grep [ option ] pattern [ filename ]

. Match any one character
* Match 0 or more characters before *
^ Match Line start
$ Match Line End
[] matches any one of the characters in [], available in []-Indicates range,
For example [A-z] represents any one of the letters A through Z
\ turn Italian characters
The options in the command are:
-B Display block number
-C displays only the total number of rows in each specified file that contains the pattern
Letters in-I mode are case insensitive
-H does not display the file name of the containing pattern on the line
-L show only filenames that contain patterns
-N Displays the line number of the line in which the pattern is
-s Specifies that the file does not exist or is unreadable without prompting for an error message
-V Displays all rows that do not contain a pattern

Where pattern is the string to match. If you look for a line that contains the "Carey" character under the/etc/passwd file:

$ grep carey/etc/passwd

To use a good grep this tool, in fact, is to write a regular expression, so here does not have all the functions of grep to explain, only a few examples, explain a regular expression of the wording.

$ grep ' test ' d*

Displays all rows that contain test in a file that begins with D.

$ ls-l | grep ' ^public '

Filters the contents of the Ls-l output through a pipeline, displaying only rows that begin with public.

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

$ grep-i ' Hello World ' menu.h main.c

Displays lines that match "Hello World" in the Menu.h and main.c files, ignoring case.

For example:

grep ' sample '-R *

2 , Find

Find is the most common and powerful look-up command you can use to find any file you're looking for.

Find uses the following format:

  Find < Specify directory > < Specify Criteria > < Specify an action >

-< specified directory;: the directory to be searched and all its subdirectories. The current directory is assumed by default.

-< specified conditions;: The characteristics of the file to be searched.

-< specify actions;: Specific processing of 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, all files are returned) and displays them all on the screen.

Examples of use of find:

$ Find.-name ' my* ' – LS

Searches the current directory (with subdirectories, below) for all files with the file names that begin with my, and displays their details.

$ Find/home-user RTOs

Search for files owned by the owner as RTOs

$ Find.-type f-mmin-10

Searches the current directory for all the normal files that have been updated in the last 10 minutes. If you do not add the-type f parameter, search for normal files + special files + directories.

$ Find/-type f-size +100m

Find all files larger than 100M in the system

Description: If you are looking for a file, then using find will be a good idea. However, because find is very hard to find when it comes to finding data, there's nothing to do with find! There is a better command to replace Yo, that is whereis and locate!

3 , Locate

The locate command is actually another way of writing "Find-name", but much faster than the latter because it does not search for a specific directory, but instead searches for a database (/var/lib/locatedb), which contains all the local file information. The Linux system automatically creates this database and updates it automatically once a day, so you can't find the latest changed files using the Locate command. To avoid this situation, you can manually update the database by using the UpdateDB command before using locate.

Examples of use of the Locate command:

$ locate/etc/sh

Search all files in the ETC directory that begin with SH.

$ locate-i ~/m

Searches all files that begin with M in the user's home directory, and ignores case.

4 , Whereis

The Whereis command can only be used for program name searches, and only binary files (parameter-B), man description file (parameter-m), and source code file (parameter-s) are searched. If the argument is omitted, all information is returned.

Examples of use of 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 purpose of the which command is to search for the location of a system command in the path specified by the path variable, and return the first search result. That is, with the which command, you can see whether a system command exists, and the command that executes exactly which location.

Examples of use of the which command:

$ which grep

/bin/grep

Five find commands under Linux: grep, find, locate, Whereis, which

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.