EN
Bash - find file name by containing text
9 points
In this example, we would like to show how in Bash find files that contain some specific text.
Quick solution (run in the command line):
xxxxxxxxxx
1
grep -r -F "text to find" /path/to/directory
2
3
# or
4
5
grep -rl -F "text to find" /path/to/directory
Where:
-r
means recursive searching,-l
means listing file names only (without found text),-F
means simple string mode searching,/path/to/directory
indicates searched directory
(can be removed to search current directory, e.g.grep -r -F "text to find"
).
Notes:
- to use case insensitive execute with
-i
parameter:
grep -ri -F "text to find" /path/to/directory
- older
grep
versions may do not support-r
parameter.
As a practical example, we would like to show looking for org
string inside ~/test
directory with line printing that contains the string.
Console preview:
xxxxxxxxxx
1
$ grep -r -F "org" ~/test/
2
/home/john/test/sub-test/test-2.txt:Some organisation ....
3
/home/john/test/sub-test/test-1.txt:https://test.org
4
/home/john/test/search.txt:organic search: 79%
Preety version:
Found file | Found line |
/home/john/test/sub-test/test-2.txt | Some org anisation .... |
/home/john/test/sub-test/test-1.txt | https://test.org |
/home/john/test/search.txt | org anic search: 79% |