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

Read More: