Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Centos find out if all files in the directory contain the specified string?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Centos how to find all the files under the directory contains the specified string", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Centos how to find out whether all the files under the directory contain the specified string"!

Linux to find whether all files under the directory contain the specified string

Find. | | xargs grep-ri "IBM" |

Find out if all the files in the directory contain a string and print out only the file name

Find. | | xargs grep-ril "IBM" |

1. Regular expression

(1) regular expressions are generally used to describe the special use of text patterns, consisting of ordinary characters (such as the character amurz) and special characters (called metacharacters, such as /, *,? Composition, etc.

(2) basic metacharacter set and its meaning

^: only the beginning of the line is matched. For example, ^ a matches the line abc,a2e,a12,aaa,. that begins with a

$: only matches the end of the line. For example, ^ a matches a line ending in a bca,12a,aaa,.

*: match 0 or more of this single character. For example, (a) * match empty, apcrum, aapas, pas, etc.

[]: only characters within [] are matched. It can be a single character or a sequence of characters, using "," 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\ *,\',\ ",\ |,\ +,\ ^,\. And so on.

.: (dot) matches only any single character.

Pattern\ {n\}: only used to match the number of previous pattern occurrences. N is the number of times. For example, a\ {2\} matches aa.

Pattern\ {n,\}: the meaning is the same as above, but at least n. For example, a\ {2,\} matches aa,aaa,aaaa,.

Pattern\ {n ·m\}: the meaning is the same as above, but the number is between n and m. Such as a\ {2pr 4\} matches three aa,aaa,aaaa

(3) examples:

^ $: match blank lines

^. $: matches a line that contains one character

\ *\ .pas: matches all characters or files ending in * .pas

[0123456789] or [0-9]: suppose you want to match any number

[amurz]: any lowercase letter

[A-Za-z]: arbitrary uppercase and lowercase letters

[Spentry]: match case S

[0-9]\ {3\}\. [0-9]\ {3\}\. [0-9]\ {3\}\. [0-9]\ {3\}: matches the IP address [0-9]\ {3\} string;\. Match point (note that this point is a special character, so use "\" to mask its meaning)

2.find introduction

(1) find files with certain characteristics of the command, you can traverse the current directory or even the entire file system to view some files or directories, the traversal of a large file system is generally executed in the background.

(2) the general form of find command

Find pathname-options [- print-exec-ok]

-pathname: the directory path that the find command looks for. If you use "." To represent the current directory and / to represent the system root directory

-print: the find command outputs matching files to standard output

-exec: the find command executes the shell command given by this parameter on the matching file, and the corresponding command form is

'command' {}\; (note the space between {} and\)

-ok and-exec serve the same purpose, except that the shell command given by this parameter is executed in a more secure mode, with a prompt for the user to determine whether or not to execute each command.

There are several types of options:

-name: find files by file name

-perm: find files according to file permissions

-user: find the file according to the owner of the file

-group: find files according to the group to which they belong

-mtime-n + n finds the file according to the change time of the file,-n indicates that the file change time is within n days from now, and + n indicates that the file change time was n days ago. The find command also has the-atime and-ctime options, but they are all similar to the-mtime option.

-size n [c] looks for files with n blocks of file length, with c indicating that the file length is in bytes.

-nogroup finds a file that does not have a 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 the file file1 but older than the file file2

-depth first looks for matching files in the specified directory, and then looks for them in the subdirectory.

-type looks for 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 files

(3) examples of find commands

Find-name "* .txt"-print finds the file at the end of txt and outputs it to the screen

Find / cmd ".sh"-print finds all sh files in the / cmd directory and outputs

Find. -perm 755-print looks for files with permissions of 755 in the current directory and outputs

Find `pwd`-user root-print finds the files under the current directory that are mainly root, and outputs

Find. /-group sunwill-print finds files in the current directory that are owned by sunwill

Find / var-mtime-5-print finds all files in the / var directory that were changed within 5 days

Find / var-mtime + 5-print look for all files in the / var directory that were changed 5 days ago

Find / var-newer "myfile1"!-newer "myfile2"-print looks for all files in the / var directory that are newer than myfile1, but older than myfile2.

Find / var-type d-print find all directories under the / var directory

Find / var-type l-print looks for all symbolic link files in the / var directory.

Find. -size + 1000000c-print looks for files larger than 1000000 bytes in the current directory

Find /-name "con.file"-depth-print looks for "con.file" in the root directory, and if not, looks in its subdirectories

Find. -type f-exec ls-l {}\; find out if there are any ordinary files in the current directory, and execute ls-l if so

(4) xargs command

When processing matched files using the-exec option of the find command, the find command passes all matched files together to exec. Unfortunately, some systems have limits on the length of commands that can be passed to exec, so that an overflow error occurs after a few minutes of running the find command. The error message is usually "Parameter column too long" or "Parameter column overflow". That's what xargs is for, especially when used with the find command, where exec initiates multiple processes and xargs has multiple, only one.

Find. /-perm-7-print | xargs chmod Omurw finds files with permission 7 and passes them 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 avoid being misunderstood as a shell command, and the other is to find a string composed of multiple words.

-c: count only the matching rows output

-I: case-insensitive (for single characters only)

-h: do not display file names when querying multiple files

-H: show only the file name

-l: when querying multiple files, only the file names containing matching characters are output.

-n: displays only matching lines and their line numbers

-s: does not display error messages that do not exist or have no matching text.

-v: displays all lines that do not contain matching text.

(2) examples:

Grep ^ [^ 210] myfile matches lines in myfile that start with non-2, 1, 0

Grep "[5-8] [6-9] [0-3]" myfile matches a three-character line in myfile with the first bit 5 | 6 | 7 | 8, the second bit 6 | 7 | 8 | 9, and the third bit 0 | 1 | 2 | 3

Grep "4\ {2 myfile 4\}" myfile matches lines with 44444 or 4444 in the myfile

Grep "?" Myfile matches lines with arbitrary characters in myfile

(3) grep command class name

[[: upper:]] means [Amurz]

[[: alnum:]] means [0-9a-zA-Z]

[[: lower:]] means [amurz]

[[: space:]] indicates a space or tab key

[[: digit:]] indicates [0-9]

[[: alpha:]] indicates [a-zA-Z]

For example: grep "5 [[: digit:]] [[: digit:]]" myfile matches the myfile containing lines with the first two digits beginning with 5 and the next two digits.

4.awk introduction

You can browse and extract information from a file or string based on specified rules, which is a self-interpreting language.

(1) awk command line awk [- F filed-spearator] 'command' input-files

Awk script: all awk commands insert a file and make the awk program executable, and then use the awk command interpreter as the first line of the script to invoke it by typing the name of the script. Awk scripts are made up of a variety of actions and patterns.

The mode part determines when the action statement triggers and triggers the event. (BEGIN,END)

The action processes the data and indicates it in {} (print)

(2) delimiters, fields, and records

When awk executes, its browsing domain is marked as $1, 2, 5, 5, 5, 5, 5, 2, 5, 2, 5, 5, 2, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 This method becomes the domain identity. $0 is all domains.

(3) examples:

Awk'{print $0} 'test.txt | all lines $0 in the tee test.out output test.txt represent all domains

Awk-F:'{print $1} test.txt | tee test.out' ditto. It's just that the separator is ":"

Awk 'BEGIN {print "IPDate\ n"} {print $1 "\ t" $4} END {print "end-of-report"}' test.txt

Print "IPDate" at the beginning and print "end-of-report" intermediate print body information at the end. For example, if you match a total of three pieces of information, the output is as follows:

IPDate

1 first

2 second

3 third

End-of-report

(4) match operator ~ match,! ~ mismatch

Cat test.txt | awk'$0universe Universe 210.34.0.13 'matches the line 210.34.0.13 in test.txt.

Awk'$0benchmark test.txt 210.34.0.13' test.txt matches lines in test.txt that are not 210.34.0.13

Awk'{if ($1percent = "210.34.0.13") print $0} 'test.txt matches the first row in test.txt with domain 210.34.0.13.

5.sed introduction

Sed does not deal with initialization files, it operates on a copy, and then all changes are output to the screen if they are not redirected to a file.

Sed is an important text filtering tool, using one-line commands or using pipes in combination with grep and awk. Is a non-interactive text stream editing.

(1) three ways to call sed

Enter a file using the sed command line format: sed [options] sed command

Use the sed script file format as: sed [options]-f sed script file input file

Sed script file [options] input file

Whether using a shell command line or a script file, if no input file is specified, sed accepts input from standard input, usually as a result of keyboard or redirection.

(2) the options of the sed command is as follows

-n: do not print

-c: the next command is the edit command

-f: if you are calling the sed script file

(3) the way sed queries the text in the file

Use a line number, which can be a simple number or a range of line numbers

-use regular expressions

(4) the way to read the text

X x is a line number

XQuery y indicates that line numbers range from x to y

/ pattern/ query rows containing schemas

/ pattern/pattern/ query contains rows of two schemas

Pattern/,x queries rows containing patterns on a given line number

XBM / query matching rows by line number and pattern

X,y! The query does not contain rows with specified line numbers x and y

(5) basic sed editing commands

P print matching lines

D Delete matching lines

= display file line number

A\ append new text information after locating the line number

I\ insert a new text message after locating the line number

C\ replace positioned text with new text

S replace the corresponding mode with the replacement mode

R read the file from another file

W write text to a file

Q launch or exit immediately after the completion of the first pattern matching

L displays control characters equivalent to the eight-forbidden ASCII code

{} the group of commands executed on the location line

N read the next line of text from another file and append it to the next line

G paste mode 2 to / pattern n /

Y transfer character

(6) examples:

Sed-n '2p' test.txt prints the second line of information (Note:-n does not print mismatched information. If-n is not added, all information of the file is printed instead of matching information).

Sed-n'1 4p 'test.txt prints the information from the first line to the fourth line

Sed-n'/ los/p' test.txt pattern matching los and print it out

The sed-n'2 test.txt test.txt starts at the second line. Know to match the first los

Sed-n'/ ^ $/ p 'test.txt matches blank lines

Sed-n-e'/ ^ $/ p'- e'/ ^ $/ = 'test.txt prints blank lines and line numbers

Sed-n'/ good/a\ morning' test.txt appends morning to the matched good

Sed-n'/ good/i\ morning' test.txt inserts morning before the matching good

Sed-n'/ good/c\ morning' test.txt replaces the matched good with morning

Sed '1jc2d' test.txt deletes lines 1 and 2

Sed 's/good/good morning/g' test.txt matches good and replaces it with goodmorning

Send's hello goodwill & hello / p 'test.txt matches to good and adds hello after it

Send 's/good/ hello & / p' test.txt matches to good and is preceded by hello

6. Merge and split (sort,uniq,join,cut,paste,split)

(1) sot command

Sort [options] files many different fields are sorted by different column order

-c test whether the files have been sorted

-m merges two sort files

-u Delete all the same lines

-o output file name where sort results are stored

-t field delimiter, starting sorting with a non-space or tab

+ n: n is the column number, use this column number to start sorting

-n specifies that the sort is a numeric classification item on the field

-r comparison inversion

Sort-c test.txt test files are classified

Sort-u test.txt sorts and merges the same lines

Sort-r test.txt is arranged in reverse order

Sort-t "/" + 2 test.txt is separated by "/", and the second domain begins to classify.

(2) uniq command

Uniq [options] files removes or prohibits duplicate lines from a text file

-u displays only non-repeating lines

-d displays only duplicate rows, and only one row for each duplicate row

-c print the number of occurrences of each duplicate line

-f: n is a number, the first n fields are ignored

Uniq-f 2 test.txt ignores the first two domains

(3) join command

Join [options] file1 file2 is used to connect lines from two classified text files

-an,n is a number used to display mismatched lines from file n when connecting

-onm, connected to the domain, n is the file number, m is the domain number

-jnm,n is the file number, m is the domain number, and other domains are used as connection domains

-t, domain delimiter. The field delimiter used to set non-spaces or tab keys.

(4) split command

Split-output_file_size intput_filename output_filename

Used to divide large files into small ones.

-b n, the size of each split file n

-C n, a maximum of n bytes per split file

-l n, the number of lines per split file

-n, same as-l n

Split-10 test.txt splits test.txt into 10-line small files

(5) cut command

Cut-c n1-n2 filename displays the text N1 to N2 from the beginning of each line.

Cut-c 3-5 test.txt displays the 3rd to 5th characters per line in test.txt

At this point, I believe that the "Centos how to find all the files under the directory contains the specified string" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report