In Linux, some commands can greatly improve the efficiency. This article will introduce the find and grep commands. The two of them can be a must-have Linux Command and I will use them almost every day. The structure of this article is as follows:
- Find command
- Common form of the find command
- Common options and instances of the find command
- Find and xargs
- Grep command
- General grep Command Format
- Grep Regular Expression metacharacters (basic set)
- Common options and instances of grep commands
1. Find command
The find command is a ubiquitous command and one of the most useful commands in Linux. The find command is used to search for files in a directory (and sub-Directories). You can specify matching conditions, such as searching for files by file name, file type, user, or even timestamp. The following example shows how powerful the find command is.
1.1 General Form of the find command
The common form of the find command in man document is:
Find [-H] [-L] [-p] [-D debugopts] [-olevel] [path...] [expression]
In fact, [-H] [-L] [-p] [-D debugopts] [-olevel] are not commonly used (at least in my daily work, ), the common form of the above find command can be simplified:
Find [path...] [expression]
- Path: directory path searched by the find command. For example, use "." To represent the current directory, and use "/" to represent the root directory of the system.
- Expression: expression can be divided into -- "-options [-print-Exec-OK...]"
- -Options: Specify common options for the find command.
- -Print and find commands output matching files to standard output
- -Exec, find command: Execute the shell command given by this parameter on the matching file. The corresponding command is in the form of 'command' {}\;. Note the space between {} And \;.
Find. /-size 0-exec RM {} \; delete the file with zero size (you can also do this: Rm-I 'find. /-size 0 'or find. /-size 0 | xargs Rm-F &)
To use the LS-l command to list the matched files, you can place the LS-l command in the-exec option of the find command: Find. -Type F-exec LS-l {}\;
Find the files whose modification time is earlier than 5 days in the/logs directory and delete them: Find/logs-type F-mtime + 5-exec RM {}\;
- -OK, which has the same role as-exec, but executes the shell command given by this parameter in a safer mode. A prompt is displayed before each command is executed, let the user determine whether to execute.
Find. -Name "*. conf "-mtime + 5-OK RM {}\; find all file names in the current directory. files whose names end with logs and whose change time is more than five days ago are deleted, but a prompt is provided before the deletion.
Some people summarize the structure of the find command as follows:
find start_directory test
options
criteria_to_match
action_to_perform_on_results
1.2 common options and instances of the find command
- -Name
Search for files by file name.
Find/Dir-name filename find the file named filename under the/DIR directory and Its subdirectories
Find.-Name "*. c" searches for any file with the extension "c" in the current directory and its sub-directories (represented ".").
- -Perm
Find the file according to the file permission.
Find.-Perm 755-print: searches for files with a File Permission of 755 in the current directory. That is, the file owner can read, write, and execute files that other users can read and execute.
- -Prune
This option can make the find command not to be searched in the specified directory. If the-depth option is used at the same time,-prune will be ignored by the find command.
Find/apps-path "/apps/bin"-prune-o-print searches for files in the/apps directory, but does not want to search for files in the/apps/bin directory.
Find/usr/SAM-path "/usr/SAM/dir1"-prune-o-print: Find all files not in the dir1 subdirectory in the/usr/SAM directory.
- -User
Search for files by file owner.
Find ~ -User Sam-print: In the $ home directory, find the file whose owner is Sam.
- -Group
Find the file according to the file group.
Find/apps-group GEM-print find the files belonging to the Gem user group in the/apps directory
- -Mtime-N + n
Find the file according to the file change time.-N indicates that the file change time is less than N days from now, and + N indicates that the file change time is earlier than N days from now.
Find/-mtime-5-print: Find the file whose modification time is within 5 days in the system root directory
Find/var/adm-mtime + 3-print in the/var/adm directory, find the file whose modification time was earlier than 3 days.
- -Nogroup
Find the file with no valid 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/etc/passwd.
Find/home-nouser-print
- -Newer file1! File2
Find the file whose modification time is newer than file1 but earlier than file2.
- -Type
Find a type of file, such:
B-block device files.
D-directory.
C-character device file.
P-MPs queue file.
L-Symbolic Link file.
F-common file.
Find/etc-type D-print search all directories under the/etc directory
Find .! -Type D-print: searches all types of files except directories in the current directory.
Find/etc-type L-print all symbolic link files under the/etc directory
- -Size N: [c] searches for files with a length of N blocks. If a file contains C, the file length is measured in bytes.
Find.-size + 000000c-print search for files with a length greater than 1 MB in the current directory
Find/home/Apache-size 100c-print find a file with a length of exactly 100 bytes in the/home/Apache directory
Find.-size + 10-print search for files with more than 10 blocks in the current directory (one block equals 512 bytes)
- -Depth: when searching for a file, first find the file in the current directory and then find it in its subdirectory.
Find/-name "con. File"-depth-Print it will match all files first and then go to the subdirectory to find
- -Mount: the mount point of the file system is not crossed during file search.
Find.-Name "*. XC"-mount-print searches for files whose names end with XC in the current directory (do not enter other file systems)
- -Follow: If the find command encounters a symbolic link file, it will trace the file to which the link points.
1.3. Find and xargs
When you use the-exec option of the find command to process matched files, the find command passes all matching files to Exec for execution. However, some systems have limits on the length of commands that can be passed to exec, so that an overflow error will occur after the find command runs for several minutes. The error message is usually "the parameter column is too long" or "parameter column overflow ". This is the use of the xargs command, especially used with the find command.
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.
In some systems, the-exec option is used to initiate a corresponding process for processing each matching file, rather than executing all the matching files as parameters once; in this way, in some cases, there may be too many processes and the system performance may decline, resulting in low efficiency;
The xargs command has only one process. In addition, when using the xargs command, whether to obtain all parameters at a time or obtain parameters in batches, and the number of parameters obtained each time is determined based on the options of the command and the corresponding adjustable parameters in the system kernel.
Let's take a look at how the xargs command is used with the find command and give some examples.
Find.-Type F-print | xargs file searches for every common file in the system, and then uses the xargs command to test which files they belong.
Find/-name "core"-print | xargs echo "">/tmp/core. log finds the memory information dump file (core dump) in the entire system, and then saves the result to/tmp/core. in the log file:
Find.-Type F-print | xargs grep "hostname" Use the grep command to search for the hostname word in all common files.
Find./-mtime + 3-print | xargs Rm-F-r deletes everything 3 days ago (find.-ctime + 3-exec Rm-RF {}\;)
Find./-size 0 | xargs Rm-F & delete files with zero size
The find command works with exec and xargs to allow users to execute almost all the commands on the matched files.
2. grep command
Grep (Global Search Regular Expression (re) and print out the line, full search for regular expressions and print out rows) is a powerful text search tool, it can use regular expressions to search for text and print matching rows.
2.1 General options and instances of grep commands
Grep [Options] pattern [file...]
Grep [Options] [-e pattern |-F file] [file...]
The grep command is used to search for the pattern specified by the pattern parameter and write each matching row to the standard output. These patterns are qualified regular expressions that use the Ed or egrep command style. If multiple names are specified in the file parameter, the grep command displays the names of the files containing matching rows. Characters ($, *, [, |, ^, (,), and \) with special shell meanings must contain double quotation marks when they appear in the pattern parameter. If the pattern parameter is not a simple string, you must enclose the entire pattern in single quotes. In expressions such as [A-Z], a range can be specified based on the sequence being sorted. Sorting sequences can define equivalent classes for use in character ranges. If no file is specified, grep is assumed as a standard input.
2.2 grep Regular Expression metacharacter set (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.
\ <Specifies the start of a word, for example, '\ <grep' matches a row that contains a word starting with grep.
\> 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. 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.
2.3 common options and instances of grep commands
-?
At the same time, the upper and lower lines of matching rows are displayed? Line. For example, grep-2 pattern filename simultaneously displays the upper and lower rows of matching rows.
-B, -- byte-offset
Print the block number of the row before the matching row.
-C, -- count
Only the number of matched rows is printed, and the matching content is not displayed.
-F file, -- file = File
Extract templates from files. The empty file contains 0 templates, so nothing matches.
-H, -- no-filename
When multiple files are searched, the matching file name prefix is not displayed.
-I, -- ignore-case
Ignore case differences.
-Q, -- quiet
Cancel display. Only the exit status is returned. 0 indicates that the matched row is found.
-L, -- files-with-matches
Print the list of files matching the template.
-L, -- files-without-match
Print the list of files that do not match the template.
-N, -- line-Number
Print the row number before the matched row.
-S, -- silent
The error message about the nonexistent or unreadable file is not displayed.
-V, -- revert-match
Reverse search: Only unmatched rows are displayed.
-W, -- word-Regexp
If it is referenced by \ <and \>, the expression is used as a word search.
-V, -- version
Displays the software version information.
=====
Ls-L | grep '^ a' filters the LS-L output content through MPs queues and only displays rows starting with.
Grep 'test' D * displays all the lines containing test in files starting with D.
Grep 'test' aa bb cc is displayed in the AA, BB, and CC files that match the test row.
Grep '[A-Z] 'aa displays all rows of a string containing at least five consecutive lowercase characters.
Grep 'W (ES) T. * 'aa if the West is matched, the ES is stored in the memory, marked as 1, and any characters (. *). These characters are followed by another ES (). If they are found, the row is displayed. If you use egrep or grep-E, you do not need to escape it by using the "" sign. You can directly write it as 'W (ES) T.
Grep-I pattern files: searches case-insensitive. Case Sensitive by default
Grep-l pattern files: only names of matched files are listed,
Grep-l pattern files: Lists unmatched file names,
Grep-W pattern files: match only the entire word, not a part of the string (for example, match 'Magic ', not 'magical '),
Grep-C number pattern files: the matching context displays the rows of [number,
Grep pattern1 | pattern2 files: displays the rows matching pattern1 or pattern2,
Grep pattern1 files | grep pattern2: displays rows that match both pattern1 and pattern2.
References:
- About Linux grep command use detailed introduction, http://fanqiang.chinaunix.net/system/linux/2007-03-15/5110.shtml
- Linux File look-up command find, xargs details, http://www.linuxsir.org/main? Q = node/137 #1.1
- Man document (man find, man grep)