Linux Find and grep commands in the detailed

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

Working under Linux, some commands can be much more efficient. This article will introduce you to the Find, grep command, his brother can be considered a necessary Linux command, I almost every day to use them. This article is structured as follows:

    • Find command
      • General form of the Find command
      • Common options and examples for the Find command
      • Find and Xargs
    • grep command
      • General form of the grep command
      • grep regular Expression meta-character set (base set)
      • Common options and examples of GREP commands
1. Find command

The Find command is a ubiquitous command and is 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 some matching criteria, such as locating files by file name, file type, user, or even time stamp. Here's an example of the power of the Find command.

1.1. 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 the common form of the Find command can be simplified to:

Find [path ...] [Expression]

    • The directory path that the Path:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory
    • Expression:expression can be divided into--"-options [-print-exec-ok ...]"
    • -options, specify common options for the Find command, and the following section details
    • The-print,find command outputs the matched file to the standard output
    • The-exec,find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form of ' command ' {} \;, note the space between {} and \;
      Find./-size 0-exec rm {} \; Delete files with a file size of zero (you can also do this: Rm-i ' find./-size 0 ' or find./-size 0 | Xargs Rm-f &)
      In order 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 {} \;
      In the/logs directory, look for files that change time before 5th and delete them: Find/logs-type f-mtime +5-exec rm {} \;
    • -ok, which is the same as-exec, simply executes the shell command given by the parameter in a more secure mode, prompting the user to determine whether to execute before executing each command.
      Find. -name "*.conf"-mtime +5-ok rm {} \; Finds all filenames in the current directory. Log end, change files over 5th, and delete them, just give a hint before deleting

Someone also summarizes the structure of the Find command:




Action_to_perform_on_results
1.2. Common options and examples of the Find command
  • -name
    Find files by file name.
    Find/dir-name FileName Locate the file named filename under the/dir directory and its subdirectories
    Find. -name "*.C" in the current directory and its subdirectories (with "." To find any file with the extension "C" in the
  • -perm
    Follow the file permissions to find the file.
    Find. -perm 755–print in the current directory to find files with permission bit 755, that is, the file owner can read, write, execute, other users can read, execute files
  • -prune
    Use this option to make the Find command not look in the currently specified directory, and if you use the-depth option at the same time,-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 in the/usr/sam directory that are not within the DIR1 subdirectory
  • -user
    Locate the file according to the owner of the file.
    Find ~-user sam–print look up files in the $home directory that belong to the main Sam file
  • -group
    Locate the file according to the group to which it belongs.
    Find/apps-group Gem–print Find files belonging to the Gem User group in the/apps directory
  • -mtime-n +n
    The file changes time to find the file,-n means that the file change time is now less than n days, + n means that the file change time is now N days ago.
    Find/-mtime-5–print in the system root for files with a change of time within 5th
    Find/var/adm-mtime +3–print in/var/adm directory to find the files that were modified before 3rd
  • -nogroup
    Finds a file that does not have a valid owning group, that is, the group to which the file belongs does not exist in/etc/groups.
    Find/–nogroup-print
  • -nouser
    Find a file without a valid owner, that is, the owner of the file does not exist in the/etc/passwd.
    Find/home-nouser–print
  • -newer file1! File2
    Look for changes that are newer than the file file1 but file2 older than the file.
  • -type
    Find a file of a certain type, such as:
    B-block device files.
    D-Directory.
    C-character device file.
    P-Pipeline file.
    L-Symbolic link file.
    F-Normal file.
    Find/etc-type D–print Find all directories in/etc directory
    Find. ! -type D–print to 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 file lengths greater than 1 m in the current directory
    Find/home/apache-size 100c–print looking for files with a file length of exactly 100 bytes in the/home/apache directory
    Find. -size +10–print Find files with a length of more than 10 blocks in the current directory (a chunk equals 512 bytes)
  • -depth: When looking for a file, first find the file in the current directory, and then look in its subdirectories.
    Find/-name "CON. File "-depth–print It will first match all the files 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 from the current directory to find files in the file system with the file name ending in XC (no other file system)
  • -follow: If the find command encounters a symbolic link file, it tracks to the file that the link points to.
1.3. Find and Xargs

When a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching files to exec execution. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column too Long" or "parameter column overflow". This is where the Xargs command is used, especially with the Find command.

The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on.

In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases there will be too many processes and degraded system performance, so the efficiency is not high;

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 to get the parameters in batches, and the number of parameters to get each time will be determined according to the command's options and the corresponding tunable parameters in the system kernel.

Take a look at how the Xargs command is used with the Find command, and give some examples.

Find. -type F-print | Xargs file looks up every normal document in the system, and then uses the Xargs command to test what type of file they belong to

Find/-name "core"-print | Xargs echo "" >/tmp/core.log find the Memory information dump file (core dump) throughout the system and save the results to the/tmp/core.log file:

Find. -type F-print | Xargs grep "hostname" uses the grep command to search for the word hostname in all common files.

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, with exec and Xargs, allows the user to execute almost all commands against the matching file.

2. grep command

grep (Global search Regular expression (RE) and print out of the line, full search of 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 lines.

2.1. General options and examples of GREP commands

grep [OPTIONS] PATTERN [FILE ...]
grep [OPTIONS] [-E PATTERN |-f file] [FILE ...]

The grep command searches for the pattern 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 styles. 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 with special meaning to the shell ($, *, [, |, ^, (,), \) must be enclosed in double quotation marks when appearing in the pattern parameter. If the pattern parameter is not a simple string, you must usually enclose the entire pattern in single quotation marks. In expressions such as [A-z],-(minus) CML can specify a range based on the sequence that is currently being collated. A collation sequence can define an equivalent class for use in a character range. If no file is specified, grep is assumed to be standard input.

2.2. grep regular expression meta-character set (Basic set)

^ The start of the anchor line, such as: ' ^grep ' matches all lines that begin with grep.

The end of the anchor line is as follows: ' grep$ ' matches all rows ending with grep.

. Match a non-newline character such as: ' GR.P ' matches gr followed by an arbitrary character followed by P.

* Match 0 or more previous characters such as: ' *grep ' matches all one or more spaces followed by the line of grep. . * Use together to represent any character.

[] matches a specified range of characters, such as ' [Gg]rep ' matches grep and grep.

[^] matches a character that is not within the specified range, such as: ' [^a-fh-z]rep ' matches a letter that does not contain a-f and h-z, immediately following the line of the Rep.

\(.. \) tag matching characters, such as: ' \ (love\) ', Love is marked as 1.

\< anchors the beginning of the word, such as: ' \<grep ' matches the line that contains the word that begins with grep.

\> anchors the end of the word, such as ' grep\> ' matches the line that contains the word that ends with grep.

X\{m\} repeats characters x,m times, such as: ' O\{5\} ' matches rows that contain 5 consecutive O.

X\{m,\} repeats the character x consecutively, at least m times, such as: ' O\{5,\} ' matches at least 5 consecutive O rows.

X\{m,n\} repeats the character x consecutively, at least m times, not more than n times, such as: ' O\{5,10\} ' matches rows that have a continuous 5--10 o.

\w matches a literal and numeric character, that is, [a-za-z0-9], such as: ' G\w*p ' matches a G followed by 0 or more literal or numeric characters, followed by P.

\w W reverse form, matching a non-word character, such as the dot period and so on. \w* can match multiple.

\b Word lock, such as: ' \bgrep\b ' only matches grep, that is, grep is only the word, both sides are spaces.

2.3. Common options and examples of GREP commands

-?

Show matching rows up and down at the same time? Line, such as: grep-2 pattern filename Displays the top and bottom 2 rows of a matching row.

-b,--byte-offset

Prints the block number where the line is printed before the matching line.

-C,--Count

Only the number of matched rows is printed and the matching content is not displayed.

-F File,--file=file

Extract the template from the file. An empty file contains 0 templates, so nothing matches.

-h,--no-filename

When searching multiple files, the matching file name prefix is not displayed.

-i,--ignore-case

ignores case differences.

-q,--quiet

Cancels the display, returning only the exit status. 0 indicates that a matching row was found.

-l,--files-with-matches

Prints a list of files that match the template.

-l,--files-without-match

Prints a list of files that do not match the template.

-n,--line-number

Prints the line number in front of the matching line.

-s,--silent

does not display an error message about a file that does not exist or cannot be read.

-v,--revert-match

Anti-retrieval, displaying only rows that do not match.

-w,--word-regexp

If referenced by \< and \>, the expression is searched as a single word.

-v,--version

Displays software version information.

=====

Ls-l | grep ' ^a ' filters the contents of the Ls-l output through a pipeline, displaying only the lines that begin with a.

grep ' test ' d* shows all lines that contain test in a file that begins 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 containing a string of at least 5 consecutive lowercase characters per string.

grep ' W (es) t.* ' AA if West is matched, then es is stored in memory, labeled 1, and then searched for any character (. *) followed by another ES (), which is found to display the line. If you use Egrep or GREP-E, do not use the "" number to escape, directly written as ' W (es) t.* ' on it.

Grep-i pattern Files: Search by case-insensitive. Case sensitive by default

Grep-l pattern Files: Lists only the matching file names,

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 [number] lines, 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.

Linux Find and grep commands in the detailed

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.