Find files and file contents using Find Xargs grep under Linux

Source: Internet
Author: User
Tags uppercase letter in domain

1, check the file under a certain path.

Search for "*.log" files under/etc

Find/etc-name "*.log"

2, expand, List all files under a path, including subdirectories.

Find/etc-name "*"

3. Look for all files that contain the "Hello Abcserver" string under a path.

Find/etc-name "*" | Xargs grep "Hello abcserver"

or find/etc-name "*" | Xargs grep "Hello abcserver" >./cqtest.txt

4, the online excerpt of a few and find files or file content related to the order detailed said:

Regular expressions
\ Indicates the start and end of a word, respectively
Ex: A word that begins with T or T and the OM ends
Meta-character set for regular expressions:
1) ^ line beginning/^love/with Love;
2) $ line End/love$/The line ending with love;
3). /L.. e/matches all such lines, and the letter L is followed by any two characters followed by the line of E
4) * Repeat 0 times or any number of preceding characters
5) [x-y] character range
6) [^x-y] exclude character range
7) \ Escape character
8) \ Define the beginning and end of a word
9) \ (\) tag followed by matching character \ (love\) able \1er=loveable Lover
X\{m,n\} Repeat character x at least m this, up to n times
grep family
Re–regular expression; G:global; P:print
Out of the above 10 meta character sets, grep also supports the following re
\w letters or numbers
\w non-word characters (punctuation)
+ Repeat one or more of the preceding characters
| or Love (A:B) means Lovea or lobeb
? Single character
grep returns the status value ($?) 0 for success, 1 for failure, 2 for no file found
Rgrep recursive access to the directory tree
Ls-l |grep ^d Display Directory
-I is case insensitive
-N Displays matching rows and line numbers
-V Displays unmatched rows only
-#显示匹配行上下 # line
-a# line after matching line
-b# matching lines before printing # lines
-C outputs only the number of rows matching rows
-H does not display matching file names when querying multiple files
-L Query Multiple files display only matching filenames
-S does not display error messages
grep is used in conjunction with regular expressions
grep "^[^ABC]" *.F fetch lines that are not ABC
grep "[0-9]\{3\}[8]" *.f extracts rows similar to xxx8, X represents any number
grep "A\{2\}" *.f shows a line that appears at least two times, note that grep is a line-handling unit of
Grep-n "^$" file lists line numbers for empty rows
-e parameter, you can use the and and or mode
GREP-E "ABC | def "*.F displays lines that contain ABC or DEF
Ls-l | grep "^$" Display directory
Ls-l | grep "^d" Display file
Ls-l | grep "^d. X.. X.. X "Show directories with specific permissions
PS aux | grep "httpd" |grep-v "grep"
* * Use Find and Xargs
1. Find Pathname-options [-print-exec-ok]
1)-name: Search by file name
Find ~-name "*.txt"-print
Find ~-name "[A-z][0-9].txt"-print
2)-perm: Find Files by permissions
Find ~-perm 755-print look for files with permission 755
Find ~-perm 007-print look for files with 7 permissions on position O
Find ~-perm 4000-print looking for files with suid
Do not look in the current directory
4)-user and-nouser
Find ~-user Zhao-print Lookup file owner is Zhao file
Find ~-nouser-print file owner has been deleted
5)-group and-nogroup
Find ~-group zhao-print Locate file groups are Zhao files
6) by Time
Find ~-mtime-5-print file changed within 5 days of file
Find ~-mtime +3-print file changed 3 days ago
Find ~-newer file1-print finding a new file than file File1
7) Search by type
Find ~-type d-print finding all directories
8) by size
Find ~-size +1000000c-print finding files with file sizes larger than 1000000 bytes (1M)
9) Find files located in this file system
Find/-name "*.txt"-mount-print
The-exec,-ok:find command executes the shell command for the matching file, with the command form: ' command ' {} \;
-ok to confirm before executing the command
Find ~-type f-exec ls-l {} \;
Find/-name "*.log"-mtime +5-ok rm {} \;
Find. -name core-exec rm {} \;
Using the-x dev parameter
Prevent find from searching for other partitions
Find. -size 0-exec rm {} \;
Delete a file with a size of 0
2. Xargs is similar to-exec function
Find ~-type F | Xargs ls-l
Find/-name "*.log"-type f-print| Xargs grep-i DB0
Find. -type f |xargs grep-i "Mary"
Retrieving a string in all files Mary
LS *~ |xargs RM-RF
Delete all files ending with ~
sed [options] sedcommand inputfile >outputfile
SED does not touch the initial input file, do not modify it, if you want to save changes, it is important to redirect the output to a file
A\: Adds a line or text after the current line
C\: Replacing our text with a letter text
D: Delete a row
D: Delete the first line of the template block
I\: Inserting text above the current line
H: Copy the contents of the template block to the memory buffer
H: Append the contents of the template block to the memory buffer
G: Get the contents of the memory buffer and replace the text in the current template
G: Get the contents of the memory buffer and append to the text in the current module section
N: Reads the next input line and processes the new line with the next command instead of the first command
N: Append the next input line to the module section and insert a new line between the two to change the number of the current row
P: Print the line of the template block
P: Plot a row of template blocks
Q: Exit SED
R file: Read rows from File
!: indicates that the following command works on all rows that are not selected
s/re/strint/: Replacing regular expression re with string
=: Print the current line number
#command: Extend annotations before the next line break symbol
l Print matching lines, including display control symbols
* * Replacement Tag
G: All in-line replacement
P: Print Line
W: Write a line to a file
x: Swap the text in the die plate and buffer
Y: Translate a character into another character
**sed can use all the characters in the regular expression
New addition:
& Save Search characters s/love/**&**/replace love with **love**
**sed How to position text
X line number
XY from X to Y line
/patern/query includes rows for Patern
x,y! Does not include lines that specify X-y line numbers
Sed '/tom/d ' file
Delete the row that contains Tom;
Sed '/tom/!d ' file
Delete all rows that do not contain Tom;
Sed-n/north/p ' file
Print a line containing north;
Sed ' 3d ' file
Delete the third row;
Sed ' 3, $d ' file
Delete the third row to the last row;
Sed ' $d ' file
Delete the last line;
Sed '/north/d ' file
Delete the row containing north;
Sed-n ' s/west/north/g ' file
Replace all west for north;
Sed-n ' s/^west/north/p ' file
West at the beginning of a line is replaced with North, and the line where the substitution occurs is printed;
Sed ' s/[0-9][0-9]$/&.5/' file
All lines that end with two digits, and the last two digits are replaced by themselves and appended. 5;
Sed-n ' s/\ (mar\) got/\1ianne/p ' file
Replace Margot with Marianne and print the line where the substitution occurred;
Sed ' s#abc#abc#g ' file
All ABC is replaced by ABC (all characters immediately following the s are considered new separators);
Sed '/west/,/east/p ' file
Prints all rows containing the West row and the middle of the East Line;
Sed ' 1,/east/s/$/**a**/' file
The line end of the row and all rows containing the East line are prefixed with the string **a**;
Sed-e ' 1,3d '-e ' s/aa/bb/' file
Delete 1 to 3 lines first, then replace AA with BB;
Sed '/sam/r file1′file
Append the contents of the file File1 to the line containing the SAM;
Sed '/sam/w file1′file
There will also be a Sam row written to the file file1;
Sed '/^north/a\new line second line ' file
All lines beginning with a North plus space are appended with two lines of text, a\ for append text, \ for newline (required in tcsh, not required in bash);
Sed '/^north/i\new line ' file
Inserts a new row in front of the line at the beginning of the row;
Sed '/norht/{n; s/aa/bb/;} ' file
First match the row containing north, then execute a set of commands, n means move to the next line, and BB instead of AA;
Sed ' 1,3g/abcdefghijklmnopqrstuvwxyz/abcdefghijklmnopqrstuvwxyz/' file
Replace all lowercase letters in rows 1 through 3 with capital letters;
Sed '/lewis/{s/lewis/bbb;q;} ' file
First find the line matching Lewis, then use BBB instead of Lewis, then Q exit seq;
Sed-e '/north/h '-e ' $G ' file
First find the row containing the Norht, H copy to the memory buffer, then to the file followed by a line ($), G appended to the last line;
Sed-e '/we/{h;d;} '-E '/ct/{g:} ' File
Locate the line that contains we, copy and delete it, and then locate the line that contains the CT, attaching the copy to the back of him;
Sed-e '/north/h '-e ' $g ' file
First find the line containing the Norht, H copy to the memory buffer, then to the file followed by a row and overwrite him;
Sed-n ' l ' file
Prints all lines that contain characters that cannot be printed.
Sed-n '/.*ing/p ' file
Displays lines containing the words ending in ing;
Sed-n '/music/= ' file
Print the line number containing the music;
Sed ' ^ $d ' file
Delete empty lines
Sed ' s/^/abcd/g ' file
Add ABCD at the beginning of the line
Sed ' s/$/abcd/g ' file
Add ABCD at the end of the line
Sed ' s/rm$/played &/g ' file
& means to retain the replaced word at the time of substitution (rm–played RM)
Sed ' s/^m//g ' file
Remove the ^m at the end of the line (^m input, press CTRL + V and press Enter)
Gawk Program:
awk [-F Domain delimiter] ' commands ' inputfile
When Awk executes, its domain identifier is $1,$2,$3 ... $n, and $ A indicates all domains
Awk-v var=value assignment to a user-defined variable
awk ' pattern ' file
awk ' {action} ' file
awk ' Pattern {action} ' file
awk ' {print $} ' file
Print all the fields
awk '/mary/' file
Prints the row containing Mary in file;
awk ' {print $} ' file
Print the first column of a file (first field);
awk '/mary/{print $1,$2} ' file
Print a file containing the first and second fields of Mary's line;
DF | awk ' s4>75000′
Print file systems with free space greater than 75000
Date | awk ' {print ' month: ", $," \ n year: "$6} '
Formats the output of the date;
awk ' begin{ofmt= '%.2f "; print 1.25463} '
Specifies the digital output format, which retains a two-bit valid number (1.25) after the decimal point;
awk '/[ab]cdef/' file
Print lines that match acdef or bcdef;
awk ' {print NR, $} ' file
awk ' {print $, NR} ' file
NR current record number, one record per processing NR adds 1
The above command is equivalent to adding a line number after each line;
NF records the number of fields in each row;
Awk-f ' [: \ t] ' {print $} ' file
Specifies that the field delimiter is a space,: or Tab
awk '/^[a-z][a-z]+/' file
Print all lines that begin with an uppercase letter, followed by one or more lowercase letters;
awk ' $1~/[bb]ill/' file
The first field matches the line of Bill or bill;
awk ' $1!~/ly$/' file
The end of the first field is not a line of ly;
awk '/^ (no|no)/' File
Print lines with no or no at the beginning of the line;
awk ' BEGIN {print ' file head \ n '} {print $ \ $4} ' file
awk ' BEGIN {print ' file head \ n '} {print $ \ $4} ' file |tee out.file
Attach a file header when printing a file
awk ' BEGIN {print ' file head \ n '} {print $ \ $4} end {print ' End of file '} ' file
Attach a file header and end of file when printing a file
Using regular expressions in awk
awk ' {if ($1~/hello/) print $} ' file
If you have hello in domain 1, print all the fields
~//indicates a matching regular expression, and!~//indicates a mismatched regular expression
awk ' {if ($6,>=,==,!=,~,!~ (matching, mismatch)
awk ' $3==5346′file
The third field is equal to 5346, and the line is printed out;
awk ' $3>5000 {print $} ' file
The third field is greater than 5000 to print the first field of the row
awk ' {max={$1>$2}?$1:$2; print max} ' file
If the first domain is greater than the second domain, max=$1 otherwise =$2, print the maximum value
awk ' {print ($1==11? ") High\t "$:" Low\t "$)} ' file
&& logic and, | | Logical OR,! Logical non-
awk ' $2==$5 && $3>5′file
awk '/tom/,/jary/' file
Tom first appears and jary the first appearance between all rows;
awk '/north/{print $3+10} ' file
Three fields with North Row plus 10;
awk ' $3== ' AA ' {$2= ' dd '; print $} ' file
Converts the second field of a row of three fields to AA into DD and prints the row;
**awk Programming:
Using variables
awk ' $1~/tom/{wage=$2+$3; print wage} ' file
First, if you match Tom, the sum of the second and third fields is assigned to the variable wage and output;
awk ' begin{fs= ': "; Ofs= "\ t"; ors= "\ n"}{print} ' file
Pre-processing File settings
The domain delimiter (FS) is (:)
Output domain delimited (OFS) is (\ t)
The output record delimiter (ORS) is (\ n)
awk ' End{print ' The number of record is: ' NR} ' file
Execute END statement after processing the file
Total number of output records
awk '/mary/{count++} end{print "Mary was found" count "Times"} ' file
The number of times Mary appears in the Count file;
**awk Input/Output redirection
awk ' $4>=70 {print $, $ > ' outfile '} ' file
Results redirected to file outfile
awk ' begin{' date "|getline D; print d} '
Lose the date result to the Getline function and print
(getline input from standard output, piping, etc.)
awk ' begin{' date ' |getline D; split (D,mon); print mon[1] mon[2]} '
The date result is lost to the Getline function, Slpit D is decomposed into an array, and the 2nd variable is printed.
**split function: Split (String,array,field seperator)
awk ' begin{while ("ls" |getline) print} '
Output LS for each item in turn
' Begin{print ' What's your name? "; getline name 0) {lc++; print LC" "A}} '
awk ' Begin{while (getline a 0) {lc++; print LC}} '
If the file does not exist, Getline returns-1
Reach end of File return 0
Read a row to return 1
Read the file/etc/passwd, count the number of rows
**awk Control Statements
If statement:
awk ' {if ($6>50) print ' Too high '} ' file
awk ' {if ($6>20 && $250}{x++; print X} else {y++;p rint y} ' file
awk ' {if ($1~/peter/) {next} else {print}} ' file
If the first field contains Peter,awk, the line is ignored, the next line of the file is read, and the script executes from the beginning;
Loop statement:
awk ' {i=1; while (i file
Cat-v file display, including the control (-V)
Cat-n file is numbered per line
Cat-b file each non-empty line number
Cat-t file Displays tab in ^i
Pipeline | Pass the output of one command to another command for input
ls | grep File.doc
Find Files File.doc
W.H.O. | awk ' {print ' \ t ' $ ' $ ' only display user name and terminal
df-h | awk ' {print '} ' |grep-v ' filesystem ' (-v means the output does not include filesystem items)
df-h | awk ' {print '} ' |grep-v ' filesystem ' |sed ' s/\/dev\///g ' displays the device without/dev/
W.H.O. | Tee who.txt output to the screen while outputting to a file
W.H.O. | Tee-a Who.txt attached to the back of the file
5. File redirection
Ls-l |grep ^d >file.out all directory names redirect to a file
cat/etc/passwd | Awk-f: ' {print '} ' |sort >a.out (-f: Specified delimiter is:)
Cat >file.out reboot
At> Ctrl+d
Reboot after 3 hours
C – Calculator
Scale=3 sets the number after the decimal point
ibase=2 binary Operations
ls-d dir
Displays only the directory and does not display the files below it.
Update Superblock and write it to the hard drive
Scp[email protected]:/path/file1[email protected]:/path/file
Copy files between servers

Find files and file contents using Find Xargs grep under Linux

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