Powerful and frequently used commands in Linux: Find, Grep__linux

Source: Internet
Author: User
Tags grep regular expression file permissions egrep

Source: Wu Qin (Tyler)
Link: cnblogs.com/skynet/archive/2010/12/25/1916873.html

Working under Linux, some commands can greatly improve efficiency. This article to introduce you to find, grep command, his brother can be regarded as a mandatory Linux command, I almost every day to use them. The structure of this article is as follows: Find command

General form of Find command
Common options and examples for Find commands
Find and Xargs grep command

General form of grep command
grep regular Expression meta-character set (base set)
Common options for grep commands and instance 1, find command

The Find command is a ubiquitous command, one of the most useful commands in Linux. The Find command is used to search for files in a directory (and subdirectories), and you can specify matching criteria, such as locating files by file name, file type, user, or even timestamp. Here is an example to experience the power of the Find command.

1. The general form of the Find command

The general form of the find command given in the man document is:

Find [-h] [l] [P] [D-debugopts] [-olevel] [path ...] [Expression]

In fact [-h] [-l] [P] [-D debugopts] [-olevel] These options are not commonly used (at least in my daily work, not used), the above find command of the common form can be simplified to:

Find [path ...] [Expression]

Path

The directory path found by the Find command. For example, to represent the current directory, with/to represent the system root directory

expression

Expression can be divided into--"-options [-print-exec-ok ...]"

-options

Specifies the common options for the Find command, which is described in detail in the following section

-print

The find command outputs matching files to standard output

-exec

The Find command executes the shell command given by the parameter to the matching file. The form of the corresponding command is ' command ' {};, note the space between {} and;

Find./-size 0-exec rm {};

Delete a file with a file size of zero (you can also do this: Rm-i find./-size 0 or get./-size 0 | Xargs Rm-f &)

To list the matching files with the Ls-l command, you can place the Ls-l command in the-exec option of the Find command:

Find. -type f-exec ls-l {};

Look for files in the/logs directory that were changed before 5th and delete them:

Find/logs-type f-mtime +5-exec rm {};

-ok

The same as-exec, except that the shell command given by the parameter is executed in a more secure mode, and before each command is executed, a prompt is given to allow the user to determine whether or not to execute.

Find. -name "*.conf"-mtime +5-ok rm {};
Finds all file names in the current directory to. Log end, change the time in the file above 5th, and delete them, but before deleting the first hint

Others have summed up the structure of the Find command:

Find start_directory test 
options 
criteria_to_match 
action_to_perform_on_results

common options and examples for 2.find commands

-name

Find files by file name.

Find/dir-name FileName: Find the file named filename under the/dir directory and its subdirectories
Find. -name "*.C": in the current directory and its subdirectories (with "." Indicates that any file with the extension "C" is found in

-perm

Locate files by file permissions.

Find. -perm 755–print: Find files in the current directory with a file permission bit of 755, which is the file owner can read, write, execute, other users can read, execute the file

-prune

Use this option to make the Find command not look in the currently specified directory, and if you use the-depth option, then-prune will be ignored by the Find command.

Find/apps-path "/apps/bin"-prune-o–print: Find files in the/apps directory, but do not want to find them in the/apps/bin directory
Find/usr/sam-path "/usr/sam/dir1"-prune-o–print: Find all files that are not in the Dir1 subdirectory under the/usr/sam directory

-user

Locate files according to the owner of the file.

Find ~-user Sam–print: Locate files in the $home directory that are owned by the owner as Sam

-group

Locate the file by the group to which the file belongs.

Find/apps-group Gem–print: Find files belonging to the Gem User group under the/apps directory

-mtime-n +n

According to the file change time to find the file, –n indicates that the file change time is now n days, + n indicates that the file change time is now N days ago.

Find/-mtime-5–print: Finds files in the system root that are within 5th of the change time
Find/var/adm-mtime +3–print: Find files in the/var/adm directory before the change time before 3rd

-nogroup

Finds a file that does not have a valid owning group, that is, the group that the file belongs to does not exist in/etc/groups.

Find/–nogroup-print

-nouser

Finds a file without a valid owner, that is, the owner of the file does not exist in/etc/passwd.

Find/home-nouser–print

-newer file1! file2

The Find change time is newer but older than the file File1 file file2.

-type

Find a type of file, such as:
b– block device files.
D-Directory.
C-Character device files.
p– pipeline file.
l– Symbolic link file.
F – Common Files.

Find/etc-type d–print: Find all directories under the/ETC directory
Find. ! -type D–print: Find all types of files except directories in the current directory
Find/etc-type L–print: Find all the symbolic link files in the/etc directory

-size n: [C] finds files with a file length of n blocks, with C indicating the length of the file in bytes.

Find. -size +1000000c–print: Find files with a file length greater than 1 m bytes in the current directory
Find/home/apache-size 100c–print: Find files with a file length of exactly 100 bytes in the/home/apache directory
Find. -size +10–print: Find files in the current directory that are longer than 10 blocks (one is equal to 512 bytes)

-depth: When looking for a file, first look for the files in the current directory and then look in their subdirectories.

Find/-name "CON. File "-depth–print: It will match all the files first and then go to the subdirectory to find

-mount: Does not cross the file system mount point when locating files.

Find. -name "*. XC "-mount–print: Find files in the current directory at the end of the file name XC (not into other file systems)

-follow: If the find command encounters a symbolic link file, it tracks to the file that the link points to.

3.find and Xargs

When you use the-EXEC option of the Find command to process matching files, the Find command passes all matching files to exec execution. However, some systems have limits on the length of commands that can be passed to exec, so that an overflow error occurs several minutes after the find command has been run. The error message is usually either "too long" or "parameter column overflow." This is the use of the Xargs command, especially with the Find command.
The Find command passes the matching file to the Xargs command, and the Xargs command gets only a portion of the file at a time instead of all, unlike the-exec option. This allows it to process the first part of the file, then the next batch, and so on.
In some systems, using the-EXEC option initiates a corresponding process for processing each matching file, rather than executing all the matching files as parameters, so that in some cases there will be too many processes to degrade the performance of the system, thus inefficient;
With the Xargs command, there is only one process. In addition, when using the Xargs command, whether to get all the parameters at once, or get the parameters in batches, and each time the number of parameters will be obtained according to the option of the command and the corresponding adjustable parameters in the system kernel.
See how the Xargs command is used with the Find command, and give some examples.

Find. -type F-print | Xargs file: Find each of the common files in the system and then use the Xargs command to test which types of files they belong to
Find/-name "core"-print | Xargs echo "" >/tmp/core.log: Find the Memory information dump file (core dump) throughout the system, and then save the results to the/tmp/core.log file
Find. -type F-print | Xargs grep "hostname": Search the word hostname in all normal files with the grep command
Find./-mtime +3-print|xargs Rm-f–r: Delete everything from 3 days ago (find. -ctime +3-exec RM-RF {};)
Find./-size 0 | Xargs rm-f &: Delete files with a file size of zero

The Find command, in conjunction with exec and Xargs, enables users to execute almost all commands on a file that matches. 2. grep command

grep (Global search Regular expression (RE) and print out of the line, a comprehensive search for regular expressions and print out rows) is a powerful text search tool that uses regular expressions to search for text. and print out the matching rows.

1. The General options and examples of GREP commands

grep [OPTIONS] pattern [FILE ...]
grep [OPTIONS] [e-pattern |-f file] [file ...]

The grep command searches for patterns specified by the pattern parameter and writes each matching row to the standard output. These patterns are qualified regular expressions that use the ED or egrep command style. If more than one name is specified in the file parameter, the grep command displays the name of the file that contains the matching row. Characters that have special meaning to the shell ($, *, [, |, ^, (,),) must be enclosed in double quotes when they appear in the pattern parameter.
If the pattern parameter is not a simple string, it is usually necessary to enclose the entire mode in single quotes. In expressions such as [A-z],-(minus) CML can specify a range based on the sequence currently being sorted. An collating sequence can define an equivalent class for use in a character range. If no files are specified, grep assumes the standard input.

2. grep regular expression meta-character set (base set)

^ : The beginning of the anchoring line

such as: ' ^grep ' matches all rows beginning with grep.

$ : End of Anchor Line

such as: ' grep$ ' matches all rows that end with grep.

. : Matches a character that is not a line feed

For example: ' GR.P ' matches the GR followed by an arbitrary character followed by P.

* : Match 0 or more previous characters

such as: ' *grep ' matches all one or more spaces immediately following the grep row ... * together, representing any character.

[] : matches a specified range of characters

such as ' [Gg]rep ' matches grep and grep.

[^] : Matches a character that is not in the specified range

For example, the ' [^a-fh-z]rep ' match does not contain a letter beginning with a-f and H-z, followed by the rep line.

(..) : Tag Matching characters

For example: ' (Love) ', Love is marked as 1.

> : Anchoring the end of a word

such as ' grep> ' matches a line containing a word that ends with grep.

x{m} : Continuous repeating character x,m times

such as: ' O{5} ' matches rows containing 5 consecutive O.

x{m,} : Continuous repeat character x, at least m times

such as: ' O{5,} ' matches rows with at least 5 consecutive O.

X{m,n} : Continuous repeat character x, at least m times, no more than n times

such as: ' o{5,10} ' matches a row of consecutive 5–10 O.

W : Match a literal and numeric character, i.e. [a-za-z0-9]

For example: ' Gw*p ' matches with G followed by 0 or more characters or numbers, followed by P.

w : Inverse form of w, matching a non-word character

such as the point number period. W* can match multiple.

b : Word lock character

such as: ' BGREPB ' only match grep, that is, only grep this word, both sides are spaces.

3. The common options and examples of GREP commands -?

Displays both the top and bottom of the matching row. Rows, such as: grep-2 pattern filename Displays the top and bottom 2 rows of matching rows at the same time. -b,–byte-offset

Print the block number in which the line is printed before the matching line. -c,–count

Prints only the number of rows that match and does not display the matching content. - F File,–file=file

Extracts the template from the file. The empty file contains 0 templates, so nothing matches. -h,–no-filename

When searching for multiple files, the matching filename prefix is not displayed. -i,–ignore-case

ignores case differences. -q,–quiet

Suppresses display and returns only the exit status. 0 indicates that a matching row was found. -l,–files-with-matches

Print a list of files that match the template. -l,–files-without-match

Print a list of files that do not match the template. -n,–line-number

Print the line number before the matching line. -s,–silent

Does not display error messages about the absence or inability to read files. -v,–revert-match

Reverse retrieve, showing only rows that do not match. -w,–word-regexp

If it is referenced, the expression is searched as a word. -v,–version

Displays software version information.

=====

Ls-l | grep ' ^a ': Filters the contents of the LS-L output through a pipe, showing only the rows that start with a.
grep ' Test ' d*: Displays all the rows that contain test in a file that starts with D.
grep ' Test ' AA bb cc: Shows the line matching test in the aa,bb,cc file.
grep ' [A-z] ' AA: Displays all lines that contain at least 5 consecutive lowercase characters for each string.
grep ' W (es) t. ' AA: If West is matched, es is stored in memory, labeled as 1, and then searched for any character (.), followed by another ES (), and found to show the line. If you use Egrep or GREP-E, you do not use the "" number to escape, directly written ' W (es) t.* ' on it.
Grep-i pattern Files: Case-insensitive search, which is case-sensitive by default.
Grep-l pattern files: Only matching file names are listed.
Grep-l pattern Files: Lists mismatched file names.
Grep-w pattern files: matches only the entire word, not part of the string (such as matching ' magic ', not ' magical ').
Grep-c number pattern files: matching contexts display the [number] line, respectively.
grep pattern1 | PATTERN2 files: Displays rows that match pattern1 or pattern2.
grep pattern1 Files | grep pattern2: Displays rows that match both PATTERN1 and pattern2.

references: about Linux
A detailed description of the grep command used, http://fanqiang.chinaunix.net/system/linux/2007-03-15/5110.shtml Linux file Lookup command Find,xargs detailed, http:// www.linuxsir.org/main/?q=node/137#1.1 Man document (Mans Find, man grep)

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.