Tag Archives: File content search

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

Grep finds all files containing a string in Linux

In projects, it is common to encounter cases such as original table names that have been changed and need to be replaced in batches in scripts. But what tables are involved, looking them up one by one is a bit of a hassle and you might miss them; Replacing directly on Linux may not perform good version synchronization; So, consider using a combination of find and the grep command to find out which scripts need to be modified before unifying the processing.

--递归查找目录下含有该字符串的所有文件
grep -rn "data_chushou_pay_info"  /home/hadoop/nisj/automationDemand/

--查找当前目录下后缀名过滤的文件
grep -Rn "data_chushou_pay_info" *.py

--当前目录及设定子目录下的符合条件的文件
grep -Rn "data_chushou_pay_info" /home/hadoop/nisj/automationDemand/ *.py

--结合find命令过滤目录及文件名后缀
find /home/hadoop/nisj/automationDemand/ -type f -name '*.py'|xargs grep -n 'data_chushou_pay_info'

final: [
find/home/hadoop/nisj/automationDemand/-type f – the name ‘*. Py’ | xargs grep -n ‘data_chushou_pay_info’ 】 compared to meet the requirements of the query.

Grep

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

-r is a recursive lookup

-n is the display line number

-r find all files contain subdirectories

-i ignores case

interesting command-line argument:
grep-i pattern files: search case-insensitive. The default case is case sensitive

grep-l pattern files: only the filenames that match are listed, not the path

grep-l pattern files: lists file names that do not match

grep-w pattern files: match whole words only, not parts of strings (for example, match ‘magic’ rather than ‘magical’)

grep-c number pattern files: the matched context displays [number] lines, respectively

grep pattern1 | pattern2 files : displays rows matching pattern1 or pattern2

grep pattern1 files | grep pattern2 : displays lines matching both pattern1 and pattern2

some special symbols for searching:
\< And \ & gt; Mark the beginning and end of each word.

such as:

grep man * will match ‘Batman’, ‘manic’, ‘man’, etc

grep ‘\< Man ‘* matches’ Manic’ and ‘man’, but not’ Batman ‘.

grep ‘\< man\> ‘matches only’ man ‘and not’ Batman ‘or’ manic ‘and other strings.

‘^’ : refers to the beginning of the matched string line

‘$’ : refers to the end of the matching string