例如,想要找「/home/test/」資料夾底下所有文件,含有「abc」內容的檔案名稱、在第幾行。
指令:
有出現「abc」的行即匹配。例如:「aaaabcccc」grep -rn '/home/test/' -e 'abc'
加上-w參數,則該行有一完整單詞都符合才匹配。例如:「xxxx abc yyyyy」
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.)
其他:
- 只搜尋「.php」 附檔名的檔案
grep -rnw '/home/test/' -e 'abc' --include=\*.php
grep -rnw '/home/test/' -e 'abc' --include=\*.{php,txt}
grep -rnw '/home/test/' -e 'abc' --exclude-dir=aa
grep -rnw '/home/test/' -e 'abc' --exclude-dir=aa*
grep -rnw '/home/test/' -e 'abc' --exclude-dir={aa,bb}
grep -rnw '/home/test/' -e 'abc' --exclude-dir={aa*,bb}
參考:
- 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
沒有留言:
張貼留言