Common Linux Command-find grep

Source: Internet
Author: User
Tags lenovo

1. 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 common form of the find command is: Find [path...] [expression]

Path: Specifies the path to be searched

Expression: can be divided into "-options [-print-Exec-OK...]"

-Print: outputs matching files.

-Type: searches for files of the specified type. Common options are as follows:

B-block device files.
D-directory.
C-character device file.
P-MPs queue file.
L-Symbolic Link file.
F-common file.

-Exec: Execute the shell command after exec on the matching file in the form of 'COMMAND '{}\;. Note: spaces between {} And \;

Instance: delete all files ending with EXE in the current directory:

lc@lc-Lenovo:~/work/test$ lsbss.exe  data.exe  main.clc@lc-Lenovo:~/work/test$ find ./ -name "*.exe" -exec rm {} \;lc@lc-Lenovo:~/work/test$ lsmain.clc@lc-Lenovo:~/work/test$ 

Instance: Find the directory files in the current directory:

lc@lc-Lenovo:~/work/test$ lsdir1  dir2  main.clc@lc-Lenovo:~/work/test$ find ./ -type d -print././dir2./dir1
lc@lc-Lenovo:~/work/test$ find -name "dir*" -type d -print./dir2./dir1

-Name: search by file name

-Mtime: query by Time

-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.

Instance: Find and delete files whose changes ended with .txt are within one day under the current directory.

lc@lc-Lenovo:~/work/test$ lsdir1  dir2  main.c  test1.txt  test2.txtlc@lc-Lenovo:~/work/test$ find -name "*.txt" -mtime -1 -ok rm {} \;< rm ... ./test1.txt > ? y< rm ... ./test2.txt > ? ylc@lc-Lenovo:~/work/test$ lsdir1  dir2  main.clc@lc-Lenovo:~/work/test$ 

-Size: search by file size

Instance: Find and delete Empty files in the current directory

LC @ LC-Lenovo :~ /Work/test $ LS-ll total usage 12drwxrwxr-x 2 LC 4096 August 16 10:48 dir1drwxrwxr-x 2 LC 4096 August 16 10:48 dir2-rw-rw-r -- 1 LC 0 August 16 11:01 file1-rw-rw-r -- 1 LC 0 August 16 11:06 file2-rw-rw-r -- 1 LC 124 August 16 09:08 main. CLC @ LC-Lenovo :~ /Work/test $ find./-size 0-exec RM {}\; LC @ LC-Lenovo :~ /Work/test $ lsdir1 dir2 main. CLC @ LC-Lenovo :~ /Work/test $

Xagrs:

When you use-exec to process the searched file, pass all matching files together to-exec. Some systems have limits on the length of the parameter passed to exec, so that after the find command runs for several minutes, an overflow error occurs. The error message is usually "the parameter column is too long" or "parameter column overflow ". The find command passes the matching file to the xargs command, while the xargs command obtains only a part of the file each time, so that it can process the first part of the file and then the next batch, and continue.
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 some cases, there may be too many processes and the system performance may degrade, so the efficiency is not high. 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.

Instance: view the common file format in the current directory

lc@lc-Lenovo:~/work/test$ lsdir1  dir2  main.clc@lc-Lenovo:~/work/test$ find . -type f -print |xargs file./main.c:      C source, ASCII text./dir1/main.c: emptylc@lc-Lenovo:~/work/test$ 

Ii. grep command: comprehensively search for regular expressions and output the results.

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. Pair
Shell characters ($, *, [, |, ^, (,), \) with double quotation marks must 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.
^ 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-F and H-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.

Common options:

-?
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.

Instance:

1. Search for the main string in the main. c file

lc@lc-Lenovo:~/work/test$ cat main.c #include <stdio.h>#include <stdlib.h>int data_array[1024*1024] = {1};//define mainint main(int argc, char* argv[]){return (0);}lc@lc-Lenovo:~/work/test$ grep 'main' main.c //define mainint main(int argc, char* argv[])lc@lc-Lenovo:~/work/test$ 

2. Search for rows starting with int in the main. c file and print the row number

lc@lc-Lenovo:~/work/test$ cat main.c #include <stdio.h>#include <stdlib.h>int data_array[1024*1024] = {1};//define mainint main(int argc, char* argv[]){return (0);}lc@lc-Lenovo:~/work/test$ grep -n '\<int' main.c 5:int data_array[1024*1024] = {1};7:int main(int argc, char* argv[])lc@lc-Lenovo:~/work/test$ 

3. In Main. C, search for the row Printing Line with the specified frequency characters

lc@lc-Lenovo:~/work/test$ cat main.c #include <stdio.h>#include <stdlib.h>aaaaaaaaaaaaaaaaaadddddddddddddddddint data_array[1024*1024] = {1};//define mainint main(int argc, char* argv[]){return (0);}lc@lc-Lenovo:~/work/test$ grep -n '[a-c]\{5\}' main.c5:aaaaaaaaa7:aaaaa
lc@lc-Lenovo:~/work/test$ grep -n '[a-c]\{4,\}' main.c 5:aaaaaaaaa6:aaaa7:aaaaalc@lc-Lenovo:~/work/test$ 

~~~~~~~~~ The end ______________





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.