Find if all files in a directory contain a certain string
find .|xargs grep -ri "IBM"
Find out if all files in a directory contain a certain string, and print only the file name
find .|xargs grep -ri "IBM" -l
1. Regular expressions
(1) Regular expressions are generally used to describe the special usage of text patterns, consisting of ordinary characters (such as the character az) and special characters (called metacharacters, such as /, *, ?, etc. )composition.
(2) The basic meta character set and its meaning
^ : only match the beginning of the line. For example, ^a matches the lines starting with a abc,a2e,a12,aaa,......
$ : only matches the end of the line. For example, ^a matches a line ending with a bca,12a,aaa,.......
*: Match 0 or more of this single character. For example, (a)* matches empty space, a,aa,aaa,....
[]: only matches characters within []. It can be a single character or a sequence of characters. Use "," to separate the different strings to be matched. You can also use - to indicate the range of character sequences within [], such as [1-5] for [12345]
\: only used to mask the special meaning of a metacharacter. Such as \*,\',\",\|,\+,\^,\. etc .:
(dot) only matches any single character.
pattern\{n\}: only matches the number of occurrences of the preceding pattern. n is the number of times. For example, a\{2\} matches aa.
pattern\{n,\}: the meaning is the same as above, but the number of times is at least n. For example, a\{2,\} matches aa,aaa,aaaa,.... .
pattern\{n,m\}: The meaning is the same as above, but the number of times is between n and m.
For example, a\{2,4\} matches three (3) of aa, aaa, aaaa :
^$ : matches an empty line
^.$ : matches a line containing one character
\*\.pas : matches with *.pas All characters at the end or file
[0123456789] or [0-9]: Assume to match any number
[az]: Any lowercase letter
[A-Za-z]: Any uppercase and lowercase letter
[S,s]: Match case S
[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\} : match IP Address [0-9]\{3\} A string composed of three 0-9; \. : matching point (note that the point here is a special character, so use "\" to mask its meaning)
2.find introduction (1) The command to find files with certain characteristics can traverse the current directory or even the entire file system to view some files or directories. It is generally executed in the background when traversing a large file system.
(2) The general form of the find command
find pathname -options [-print -exec -ok]
-pathname : The directory path searched by the find command. For example, "." is used to represent the current directory, and / is used to represent the system root directory
-print:find command outputs the matching file to standard output
-exec:find command executes the shell command given by this parameter to the matching file, The corresponding command form is
'command'{} \; (note the space between {} and \)
-ok and -exec have the same effect, but execute the shell command given by this parameter in a safer mode. Before each command, a prompt will be given, allowing the user to determine whether to execute it.
Options are as follows:
-name : Find files by file name
-perm : Find files by file permissions
-user : Find files by file owner
-group : Find files by group to which the file belongs
-mtime -n +n Find the file according to the change time of the file, -n means the file change time is within n days from now, +n means the file change time is n days ago. The find command also has the -atime and -ctime options, but they are both similar to the -mtime option.
-size n[c] Find files with a file length of n blocks, with c, the file length is measured in bytes.
-nogroup Find files with no valid group, that is, the group to which the file belongs does not exist in /etc/groups
-newer file1 !file2 finds files whose change time is newer than file11 but older than
file22 -depth finds the specified directory first If there is no matching file, if there is none, look for
-type in the subdirectory to find a certain type of file, such as
b: block device file
d: directory
e: character device file
p; pipe file
l: Symbolic link file
f: Ordinary file
(3) Example of find command
find -name "*.txt" -print Find files ending in txt and output to the screen
find /cmd ".sh" -print Find all files in the /cmd directory sh file, and output
find . -perm 755 -print Find files with permission 755 in the current directory, and output
find `pwd` -user root -print Find files whose owner is root in the current directory, and output
find ./ -group sunwill -print Find files in the current directory whose owner is sunwill
find /var -mtime -5 -print Find all files in the /var directory with a change time of 5 days
find /var -mtime +5 -print Find the changes in the /var directory All files older than 5 days
find /var -newer "myfile1" ! -newer "myfile2" -print Find all files in the /var directory that are newer than myfile1 but older than myfile2.
find /var -type d -print Find all directories in /var directory
find /var -type l -print Find all symbolic link files in /var directory.
find . -size +1000000c -print Find files larger than 1000000 bytes in the current directory
find / -name "con.file" -depth -print Find whether there is "con.file" in the root directory, if not, in its subdirectory Find in
find . -type f -exec ls -l {} \; Find whether there are ordinary files in the current directory, if so, execute ls -l
(4) The xargs command
uses the -exec option of the find command to process the matched files , the find command passes all matching files to exec together. Unfortunately, some systems have a limit on the length of commands 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 xargs is useful, especially when used with the find command, exec will launch multiple processes, and xargs will have multiple, only one
find ./ -perm -7 -print | xargs chmod ow Find files with permissions 7 and Passed to chmod for processing
3. grep introduction (1) The general format of grep is grep [options] Basic regular expression [file]
string parameters are best enclosed in double quotes, one is to prevent misunderstanding as a shell command, the other is Can be used to find strings consisting of multiple words
-c: output only the count of matching lines
-i: case insensitive (only applicable to a single character)
-h: do not display file names when querying multiple files
-H: only show filename
-l: When querying multiple files, only output file names containing matching characters
-n: Only display matching lines and their line numbers
-s: Do not display error messages that do not exist or have no matching text.
-v: Display all lines that do not contain matching text.
(2) For example:
grep ^[^210] myfile matches lines in myfile that start with non-2, 1, 0
grep "[5-8][6-9][0-3]" myfile matches the first line in myfile
grep "4\{2,4\}" myfile for a three-character line with bits 5|6|7|8, second bit 6|7|8|9, and third bit 0|1|2|3 Match lines containing 44, 444 or 4444 in
myfile grep "\?" myfile match lines containing any character in myfile
(3) grep command class name
[[:upper:]] means [AZ]
[[:alnum:]] means [ 0-9a-zA-Z]
[[:lower:]] means [az]
[[:space:]] means space or tab key
[[:digit:]] means [0-9]
[[:alpha:] ] means [a-zA-Z]
such as: grep "5[[:digit:]][[:digit:]]" myfile matches the line in myfile that starts with 5 and the next two digits are all digits.
4.awk introduction
can browse and extract information from files or strings based on specified rules, and is a self-explanatory language.
(1) awk command line mode awk [-F filed-spearator] 'command' input-files
awk script: insert all awk commands into a file, make the awk program executable, and then use the awk command interpreter as the first line of the script, to call it by typing the script name. An awk script is composed of various operations and modes.
The mode part determines when the action statement fires and the event is fired. (BEGIN, END)
action to process the data, put it in {} to indicate (print)
(2) Separator, domain and record
When awk is executed, its browsing domain is marked as $1, $2,...$n. This kind of The method becomes the domain ID. $0 is all domains.
(3) Example:
awk '{print $0}' test.txt |tee test.out Output all lines in test.txt $0 means all domains
awk -F : '{print $1} test.txt |tee test.out' Ditto. . Just the delimiter is ":"
awk 'BEGIN {print "IPDate\n"}{print $1 "\t" $4} END{print "end-of-report"}' test.
2 second
3 third
end-of-report
(4) The matching operator ~ matches, !~ does not match
cat test.txt |awk '$0~/210.34.0.13/' matches the line 210.34.0.13 in test.txt
awk ' $0!~/210.34.0.13' test.txt matches the line in test.txt that is not 210.34.0.13
awk '{if($1=="210.34.0.13") print $0}' test.txt matches the first line in test.txt The line whose domain is 210.34.0.13.
5.sed introduction sed does not deal with the initialization file, it operates only a copy, and then all the changes if not redirected to a file, will be output to the screen.
sed is a very important text filtering tool, using one line command or using pipes combined with grep and awk. is a non-interactive text flow editor.
(1) Three ways to call sed
Use sed command line format: sed [options] sed command input file
Use sed script file format: sed[options] -f sed script file input file
sed script file [options] input file
--Whether you use the shell command line method or the script file method, if no input file is specified, sed accepts input from the standard input, usually the keyboard or redirection result.
(2) The options of the sed command are as follows
-n: do not print
-c: the next command is an editing command
-f: if you are calling a sed script file
(3) the way sed queries text in the file
-- use the line number, which can be a simple number, or a The range of line numbers -- the way to read text
using regular expressions (4) x x is the line number x, y indicates that the line number ranges from x to y /pattern/ The query contains the line of the pattern /pattern/pattern/ The query contains two Pattern lines pattern/,x Query lines containing the pattern x,/pattern/ at the given line number Query matching lines x,y by line number and pattern! Query lines that do not contain the specified line numbers x and y (5) Basic sed editing commands p print matching lines d delete matching lines = show file line number a\ append new text information after positioning line number i \ insert new text information after positioning line number
c\ replace positioned text with new text
s replace corresponding pattern with replacement pattern
r read file from another file
w write text to a file
q eject or exit immediately after first pattern match
l show ASCII equivalent of eight forbidden The control character
{} executes the command group at the positioned line
n reads the next line of text from another file and appends it on the next line
g pastes pattern 2 into /pattern n/
y transmits characters
(6) Example:
sed -n ' 2p' test.txt prints the information of the second line (note: -n is not to print unmatched information, if -n is not added, all information of the file will be printed instead of matching information)
sed -n '1,4p' test .txt prints information from the first to fourth lines
sed -n '/los/p' test.txt pattern matches los and prints it out
sed -n '2,/los/p' test.txt starts from the second line . . Know to match the first los
sed -n '/^$/p' test.txt matches empty lines
sed -n -e '/^$/p' -e '/^$/=' test.txt print blank lines and line numbers
sed -n '/good/a\morning' test.txt after the matched good Append morning
sed -n '/good/i\morning' test.txt Insert morning
sed -n '/good/c\morning' test.txt before the matched good and replace the matched good with morning
sed '1, 2d' test.txt delete lines 1 and 2
sed 's/good/good morning/g' test.txt matches good and replaces it with goodmorning
send 's/good/& hello /p' test.txt matches good right there Add hello
send 's/good/ hello &/p' test.txt after it. If it matches good, add hello in front of it
. 6. Merge and split (sort, uniq, join, cut, paste, split) (1) sot command
sort [options] files many different fields are sorted in different column order
-c test whether the file is already sorted
-m merge two sorted files
-u delete all the same lines
-o output filename to store sort results
-t field separator, start sorting with non-space or tab
+n : n is the column number, use this column number to start sorting
-n specifies that the sorting is a numeric categorical item on the field
-r compares Inverse
sort -c test.txt Test if file is sorted
sort -u test.txt Sort and merge same lines
sort -r test.txt in reverse order
sort -t "/" +2 test.txt with "/ "Separation, the second field starts to classify
(2) uniq command
uniq [options] files Remove or prohibit repeated lines from a text file
-u only display non-repeated lines
-d only display lines with repeated data, each repeated line only show one of the lines
-c print the number of occurrences of each repeated line
-f: n is a number, the first n fields are ignored
uniq -f 2 test.txt ignore the first 2 fields
(3) join command
join [options] file1 file2 used Join lines from two classified text files together
-an, n is a number, used to display unmatched lines from file n when
connecting -onm, connection domain, n is the file number, m is the domain number
-jnm, n is the file number, m is the domain number, use other Domain as concatenated domain-
t, domain separator. Used to set field separators other than spaces or tabs.
(4) The split command
split -output_file_size intput_filename output_filename
is used to split large files into small files.
-bn, the size of each split file n
-C n, each split file has a maximum of n bytes per line
-ln, the number of lines per split file
-n, same as -ln
split -10 test.txt split test.txt Small file into 10 lines
(5) The cut command
cut-c n1-n2 filename displays the text of each line from the beginning of n1 to n2.
cut -c 3-5 test.txt displays the 3rd to 5th characters of each line in test.txt
Find if all files in a directory contain a certain string
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325695972&siteId=291194637
Recommended
Ranking