Grep: How to Find All the Files Containing a String in Linux

In the project, we often encounter the situation that the original table name has been replaced and needs to be replaced in batches in the script. However, it is troublesome to find out which tables are involved one by one, and it may be missed; replacing them directly on Linux may not be able to achieve good version synchronization; therefore, we can consider the combination of find and grep commands to find out the scripts that need to be modified before unified processing.

 

--Recursively find all files in the directory that contain this string
grep -rn "data_chushou_pay_info" /home/hadoop/nisj/automationDemand/

--find files in the current directory with filtered suffixes
grep -Rn "data_chushou_pay_info" *.py

--file in the current directory and set subdirectories that match the criteria
grep -Rn "data_chushou_pay_info" /home/hadoop/nisj/automationDemand/ *.py

--combine with the find command to filter directories and file name suffixes

find /home/hadoop/nisj/automationDemand/ -type f -name '*.py'|xargs grep -n 'data_chushou_pay_info'

In the end:

find /home/hadoop/nisj/automationDemand/ -type f -name ‘*.py’|xargs grep -n ‘data_ chushou_ pay_ [Info ‘] to meet the query requirements.

 

Grep options:

*: indicates all files in the current directory, or a file name

-R is a recursive search

-N is the display line number

-R find all files including subdirectories

-I ignore case

 

 

Interesting command line parameters:
grep – I pattern files: case insensitive search. Case sensitive by default

Grep – L pattern files: only the matching file names are listed, not the paths

Grep – L pattern files: lists unmatched file names

Grep – W pattern files: matches only the whole word, not part of the string (for example, matches’ magic ‘, not’ magic ‘)

Grep – C number pattern files: displays the [number] line for the matched context

 

Grep pattern1 | pattern2 files: displays rows that match pattern1 or pattern2

 

Grep pattern1 files | grep pattern2: displays rows that match both pattern1 and pattern2

 

 

Some special symbols used for search:
\ < and \ > mark the beginning and end of words respectively.

For example:

Grep man * will match “Batman”, “manic”, “man” and so on

Grep ‘< man’ * matches’ manic ‘and’ man ‘, but not’ Batman ‘

Grep ‘< man \ & gt;’ only matches’ man ‘, not other strings such as’ Batman’ or ‘manic’.

‘^’: refers to the matching string at the beginning of the line

‘$’: the matching string is at the end of the line

Read More: