How do I recursively grep all directories and subdirectories?
grep -r "texthere" .
The first parameter represents the regular expression to search for, while the second one represents the directory that should be searched. In this case, .
means the current directory.
Note: This works for GNU grep, and on some platforms like Solaris you must specifically use GNU grep as opposed to legacy implementation. For Solaris this is the ggrep
command.
How to grep recursively and ignore subdirectories of subdirectories?
This will exclude the (sub)directories with names idc
and otherStuffIdc
.
grep -r pattern --exclude-dir=idc,otherStuffIdc
Using grep to recursively search through subdirectories for specific keyword inside specific filename
Continuing from my comment, you can use find
to locate the file vsim.log
if you do not know its exact location and then use the -execdir
option to find
to grep
the file for the term Elapsed time
, e.g.
find path -type f -name "vsim.log" -execdir grep -H 'Elapsed time' '{}' +
That will return the filename along with the matched text which you can simply parse to isolate the filename if desired. You can process all files that match if you anticipate more than one by feeding the results of the find
command into a while read -r
loop, e.g.
while read -r match; do
# process "$match" as desired
echo "Term 'Elapsed time' found in file ${match%:*}"
done < <(find path -type f -name "vsim.log" -execdir grep -H 'Elapsed time' '{}' +)
Where:
find
is the swiss-army knife for finding files on your systempath
can be any relative or absolute path to search (e.g.$HOME
or/home/dorojevets
) to search all files in your home directorythe option
-type f
tells find to only locate files (seeman find
for link handling)the option
-name "foo"
tell find to only locate files namedfoo
(wildcards allowed)the
-exec
and-execdir
options allow you to execute the command that follows on each file (represented by'{}'
)the
grep -H 'Elapsed time' '{}'
being the command to execute on each filenamethe
+
being what tellsfind
it has reached the end of the command (\;
used with-exec
)finally, the
${match%:*}
parameter expansion on the variable$match
is used to parse thefilename
fromfilename:Elapsed time
returned bygrep -H
(the%:*
simply being used to trim everything to the first:
from the right of$match
)
Give that a try and compare the execution time to a recursive grep
of the file tree. What you may be missing in this discussion, is that you use find
if you know some part of the filename (or file mod time, or set of permissions, etc) that contains the information you need. It can search millions of files in a file tree vastly quicker than you can recursively grep
every single file. If you have no clue what file may contain the needed info -- then use grep
and just wait...
How do I grep recursively in files with a certain extension?
find
allows you to run a program on each file it finds using the -exec
option:
find -name '*.out' -exec grep -H pattern {} \;
{}
indicates the file name, and ;
tells find
that that's the end of the arguments to grep
. -H
tells grep
to always print the file name, which it normally does only when there are multiple files to process.
How can I recursively find all files in current and subfolders based on wildcard matching?
Use find
:
find . -name "foo*"
find
needs a starting point, so the .
(dot) points to the current directory.
How do I recursively grep files in multiple folders? I'm unable to pipe the recursive cat output
You can use awk
to mathematically add all scores and print sum
:
cd files # parent directory of set1, set2, set3, set4
awk -F= '$1 == "score" { sum += $2 } END { print sum }' */result.txt
251
recursively grep through a directory, and extract the contents between the tags
If it has to be grep
, use that command:
grep -PzoHnr "(?s)< start >.*< / start >" .
Explanation:
-P
: Activate perl regular expressions-z
: Treat the input as a set of lines, each terminated by a zero byte-o
: Print only matches-H
: Add the filename in front of the match-n
: Add the line number in front of the match-r
: Read all files under each directory, recursively.(?s)
: ActivatesPCRE_DOTALL
, which means that.
finds any character or newline< start >.*< / start >
is the regular expression
Alternatively, here is an awk
solution as well:
awk '/\<\ start\ \>/,/\<\ \/\ start\ \>/{print FILENAME ":" FNR ":" $0}' $(find . -type f)
Explanation:
/\<\ start\ \>/,/\<\ \/\ start\ \>/
: Finds all between< start >
and< / start >
{print FILENAME ":" FNR ":" $0}
: Prints the filename, the line number and the line$(find . -type f)
lists only the files in the directory recusively
Related Topics
How to Send a HTML Email With the Bash Command "Sendmail"
Multiple Glibc Libraries on a Single Host
Cannot Connect to the Docker Daemon At Unix:/Var/Run/Docker.Sock. Is the Docker Daemon Running
Linking Openssl Libraries to a Program
How to Remove ^[, and All of the Escape Sequences in a File Using Linux Shell Scripting
Bash Function to Find Newest File Matching Pattern
How to Access the System Call from User-Space
Extract File Basename Without Path and Extension in Bash
Convert Xlsx to CSV in Linux With Command Line
Print a File, Skipping the First X Lines, in Bash
Looping Through the Content of a File in Bash
Finding Which Process Was Killed by Linux Oom Killer
How to Set the Working Directory of the Parent Process
How to Escape Single Quotes in Bash/Grep
Bluetooth Low Energy: Listening For Notifications/Indications in Linux
How to Deal With a Filename That Starts With the Hyphen (-) Character