How to Use the find command in Linux

Source: Internet
Author: User
Tags grep regular expression

1. Search for files
In Linux, the command for searching files is the find command, syntax:
Find [start Directory] search for condition operations such as searching for abc. cpp files from the root directory
Find/-name abc. cpp
All. cpp files in the current directory
Find.-name "*. cpp"
The find command can be used with the-exec parameter to perform further operations on the queried file.
-The exec parameter is followed by the command, and its termination is the end sign. Therefore, the semicolon after this command is indispensable, given that the semicolons in each system have different meanings, the escape character '\' is used before '\'.
Xargs expands the find result and uses it as a grep parameter.
Find./-name "*. tmp"-exec rm-rf "{}"/;
Delete all temporary files
However, commands such as rm mv report an error when operating on a large number of files-bash:/bin/rm: Argument list too long
Available xargs Solutions
Delete all. cpp files in the current directory
Find.-name "*. tmp" | xargs rm
The find command passes the matching file to the xargs command, while the xargs command only obtains part of the file, not all, at a time, unlike the-exec option. In this way, it can first process the first part of the obtained files, then the next batch, and continue like this.
Find and grep are used together to find the file content in the specified directory. Grep is used to find strings in files or rows that can match regular expressions.
Grep command
Grep (global search regular expression_r (RE) and print out the line, fully search for regular expressions and print rows) is a powerful text search tool, it can use regular expressions to search for text and print matching rows.
Grep Regular Expression metacharacters (basic set)
^ Start of the anchor row: '^ grep' matches all rows starting with grep.
$ The End Of The Anchor row is as follows: 'grep $ 'matches all rows ending with grep.
. Match a non-linefeed character such as: 'gr. P' match gr followed by any character, followed by p.
* Match zero or multiple previous characters, for example, '* grep'. Match All one or more spaces followed by the grep line. . * Represents any character.
[] Matches a character in a specified range, for example, '[Gg] rep' matches Grep and grep.
[^] Match a character that is not within the specified range, for example, '[^ A-FH-Z] rep' match a line that does not start with a letter that does not contain the A-R and T-Z, followed by rep.
\ (.. \) Indicates matching characters, such as '\ (love \)', and love is marked as 1.
\ <Specify the start of a word, for example :'\
\> Anchor specifies the end of a word. For example, 'grep \> 'matches the row containing the word ending with grep.
X \ {m \} repeats the characters x and m consecutively. For example, 'O \ {5 \} 'matches the rows that contain 5 consecutive o Records.
X \ {m, \} repeats the character x continuously for at least m times, for example, 'O \ {5, \} 'matches rows with at least 5 o consecutively.
X \ {m, n \} repeats the character x consecutively, at least m times, no more than n times, for example, 'O \ {5, 10 \} 'matches rows of 5-10 consecutive o.
\ W matches a character with a number, that is, a [A-Za-z0-9], for example, 'G \ w * P' matches a character with G followed by zero or more characters or numbers, then p.
The inverse form of \ W w. It matches a non-word character, such as a period ending. \ W * can match multiple.
The \ B word lock. For example, '\ bgrep \ B' only matches grep, that is, it can only be a grep word, with spaces on both sides.
Find, grep, and xargs are often used in combination as follows:
Find-name ". cpp" | xargs grep 'test' find all cpp files in the current directory that contain test rows. Author:

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.