how to find files containing a string using egrep
Here you are sending the file names (output of the find command
) as input to egrep; you actually want to run egrep on the contents of the files.
Here are a couple of alternatives:
find . -name "*.txt" -exec egrep mystring {} \;
or even better
find . -name "*.txt" -print0 | xargs -0 egrep mystring
Check the find command help to check what the single arguments do.
The first approach will spawn a new process for every file, while the second will pass more than one file as argument to egrep; the -print0 and -0 flags are needed to deal with potentially nasty file names (allowing to separate file names correctly even if a file name contains a space, for example).
How to find all files containing specific text (string) 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 dirsdir1/
,dir2/
and all of them matching*.dst/
:grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
For more options, see man grep
.
How to find files containing a string N times or more often using egrep
Your command
egrep -rl "(CREATE TABLE.*){3}" ./*.sql
looks for 3 CREATE TABLE
's on one line.
When they are on different lines, you need to do something different,
and when you have GNU grep, you are lucky: It has the option -z
.
# minimal change of your command
egrep -zrl "(CREATE TABLE.*){3}" ./*.sql
# moving option E to the options as suggested by @anubhava
grep -zErl "(CREATE TABLE.*){3}" ./*.sql
How to find all files containing specific text (string) 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 dirsdir1/
,dir2/
and all of them matching*.dst/
:grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
For more options, see man grep
.
Grep to find a file that contains a string in a directory
Source: man grep
There's an option for --files-with-matches
or -l
grep -Rl gaq /Users/myname/Desktop/website
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"
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.
How to use grep/egrep to count files in subdirectories containing a String
Use the -l
option to list just the filename when the contents match the pattern. Yse the -r
option to recurse into subdirectories. Use the -F
option to match the string exactly, rather than as a regular expression.
You need to tell it the name of the directory to recurse into; you can use .
for the current directory.
Then pipe this to wc -l
:
grep -rlF "string" . 2>/dev/null | wc -l
Grep files containing two or more occurrence of a specific string
What about this:
grep -o -c Hello * | awk -F: '{if ($2 > 1){print $1}}'
Related Topics
Environment Variables in Docker When Exec Docker Run
Is There Any Posix Way Through Fstat() to Check Whether a File Is a Symbolic Link or Not
Would It Be Possible to Read Out Physical Keyboard Strokes in Node.Js
Bash Redirect to /Dev/Stdout: Not a Directory
Why Do I Get The Information of "Suspended (Tty Input)" When I Run My Script in The Background
What Is an Interface Identifier
What Is The Most Efficient Way to Exchange High Volume Data Between 2 Process
How to Give to Some User Permissions Only to Subfolder
Thread Quantum: How to Compute It
How to Launch a Job in a Shell Which Will Persist Even If The Shell Which Launches It Terminates
Linux - Create Animated Gif with Pan and Zoom
Linking a Static Library into a Shared Library
Run Any Linux Terminal Command from Typescript
How to Find Which Type of System Call Is Used by a Program
Upgrading PHPmyadmin (And Other Packages) on Debian Squeeze
How Linux Scheduler Schedules Processes on Multi-Core Processors