How do I find all files containing specific text on Linux?
Do the following:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r
or-R
is recursive,-n
is line number, and-w
stands for match the whole word.-l
(lower-case L) can be added to just give the file name of matching files.-e
is the pattern used during the search
Along with these, --exclude
, --include
, --exclude-dir
flags could be used for efficient searching:
- This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
- This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
- For directories it's possible to exclude one or more directories using the
--exclude-dir
parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
For more options, see man grep
.
How do I find all files containing specific text on Linux?
Do the following:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r
or-R
is recursive,-n
is line number, and-w
stands for match the whole word.-l
(lower-case L) can be added to just give the file name of matching files.-e
is the pattern used during the search
Along with these, --exclude
, --include
, --exclude-dir
flags could be used for efficient searching:
- This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
- This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
- For directories it's possible to exclude one or more directories using the
--exclude-dir
parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
For more options, see man grep
.
How do I find all files containing specific text on Linux but not in subdirectories?
You can use:
grep 'text' /search/path/*
# ^
The *
will expand to all the elements in the directory and the absence of -R
will avoid going deeper into subdirectories.
The drawback of this approach is that it will exclude the hidden files, since .files
do not expand with *
alone. If you also want to grep those, you can use:
grep 'text' /search/path/{*,.*}
# \____/
This way you will get all elements matching *
and .*
which is everything.
Finally, if you do not want to get the error message:
search/path/ Is a directory
You can redirect it to /dev/null as follows:
grep 'text' /search/path/{*,.*} 2>/dev/null
# \_________/
grep -rnw: search for a string in all files
explainshell helpfully explains your command, and gives an excerpt from man grep
:
-w, --word-regexp
Select only those lines containing matches that form whole words.
So just remove -w
since that explicitly does what you don't want:
grep -rn '/path/to/somewhere/' -e "pattern"
Linux command line: Find all files with a certain extension in a directory tree containing specific text
After looking through a few extra posts including this one, I found a command which works.
I am not exactly sure what xargs
does but the post I mentioned explains a bit more:
find ./ -name *.py | xargs grep "text-to-find"
Find files containing string among recently modified files in linux
Your other answers so far all suggest using find
's -exec
feature to run a grep
command for each candidate file identified. That's viable, but launching hundreds or thousands of separate grep
commands would be costly. It would be more efficient to combine find
with xargs
to reduce the number of separate grep
commands to a minimum:
find / -type f -mtime -2 -print0 |
xargs -r0 grep -Fnw 'search string'
xargs
will group the file names read from its standard input to form argument lists for grep
commands starting with the given words, yielding a huge reduction in the number of separate grep
commands.
Note also that:
- The example command uses extensions provided by GNU
find
and GNUxargs
. Removing the two0
s from the example command would fix that, but leave you open to issues involving file names containing newlines. - The
-F
option, as shown, will makegrep
slightly more efficient for the case you describe, where the search term is a fixed string. It will also protect you against the possibility of the search term being misinterpreted in the event that it contains any regex metacharacters. find
can use all sorts of additional information to be more selective about which files are passed on togrep
, if you can glean any such details. For example, if you can determine what user will own the file, or anything about its mode (permissions), or a lower or upper bound on the file size. Also, if you can limit the search to less than the whole filesystem then of course that will improve the elapsed time, too.- For a large filesystem, it will take a fairly long time, no matter what you do, just to traverse all the files, even without reading any of their contents.
Identify all files or folder which contains a specific keyword
You have to give the "keyword" before the path to be searched. which it not the case in your script.
grep -l -r "keyword" path/to/folder
Try the above script.
-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match. (-l is specified by
POSIX.)
-R, -r, --recursive
Read all files under each directory, recursively; this is
equivalent to the -d recurse option.
for more info refer
Related Topics
How to Grep For Contents After Pattern
How to Run a Shell Script At Startup
What Does the Number in Parentheses Shown After Unix Command Names in Manpages Mean
Ld Cannot Find an Existing Library
How to Escape Single Quotes in Bash/Grep
How to Create Docker Overlay Network Between Multi Hosts
Imagemagick Security Policy 'Pdf' Blocking Conversion
Why Are the Backslash and Semicolon Required With the Find Command'S -Exec Option
How to Compare Two Datetime Strings and Return Difference in Hours? (Bash Shell)
How to Get Gcc to Output Raw Binary
Can You Run Gui Applications in a Linux Docker Container
How to Generate a Core Dump in Linux on a Segmentation Fault
Hello, World in Assembly Language With Linux System Calls
Why Is a Tilde in a Path Not Expanded in a Shell Script
How to Set the Gopath Environment Variable on Ubuntu? What File Must I Edit
Exit Code of Variable Assignment to Command Substitution in Bash