例如,想要找「/home/test/」資料夾底下所有文件,含有「abc」內容的檔案名稱、在第幾行。
指令:
grep -rnw '/home/test/' -e 'abc'
說明:
-r, --recursive 遞迴讀取每個目錄下的文件 Read all files under each directory, recursively, following symbolic links only if they are on the command line. This is equivalent to the -d recurse option. -n, --line-number 為文件每行標行號 Prefix each line of output with the 1-based line number within its input file. (-n is specified by POSIX.) -w, --word-regexp 選擇匹配的行 Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore. -e PATTERN, --regexp=PATTERN 匹配文字樣式 Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (-e is specified by POSIX.)
參考:
- https://stackoverflow.com/questions/16956810/how-do-i-find-all-files-containing-specific-text-on-linux
How do I find all files containing specific text on Linux? - Stack Overflow