How to grep a string in a directory and all its subdirectories?
If your grep supports -R
, do:
grep -R 'string' dir/
If not, then use find
:
find dir/ -type f -exec grep -H 'string' {} +
How can I use grep to find a word inside a folder?
grep -nr 'yourString*' .
The dot at the end searches the current directory. Meaning for each parameter:
-n Show relative line number in the file
'yourString*' String for search, followed by a wildcard character
-r Recursively search subdirectories listed
. Directory for search (current directory)
grep -nr 'MobileAppSer*' .
(Would find MobileAppServlet.java
or MobileAppServlet.class
or MobileAppServlet.txt
; 'MobileAppASer*.*'
is another way to do the same thing.)
To check more parameters use man grep command.
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 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 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
.
Using grep to search for specific type of files in all subdirectories
You can use the following option of grep:
--include=GLOB
Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).
And for the line number you should use the -n
option.
From within the root of the folders you want to look into, you can use this final command:
grep -nr "Mutual_Values_23.0" --include="gnuout_mutual_*txt"
How do I use grep to search the current directory for all files having the a string hello yet display only .h and .cc files?
grep -r --include=*.{cc,h} "hello" .
This reads: search recursively (in all sub directories also) for all .cc OR .h files that contain "hello" at this .
(current) directory
From another stackoverflow question
grep in all directories
grep -R --include="*.c" --exclude-dir={DEF} writeFile /path/to/XYZ
-R
means recursive, so it will go into subdirectories of the directory you're grepping through--include="*.c"
means "look for files ending in.c
"--exclude-dir={DEF}
means "exclude directories namedDEF
. If you want to exclude multiple directories, do this:--exclude-dir={DEF,GBA,XYZ}
writeFile
is the pattern you're grepping for/path/to/XYZ
is the path to the directory you want to grep through.
Note that these flags apply to GNU grep
, might be different if you're using BSD/SysV/AIX grep
. If you're using Linux/GNU grep utils you should be fine.
Related Topics
What's the Difference Between Event-Driven and Asynchronous? Between Epoll and Aio
How to Break Up an Extremely Long String Literal in Bash
Permission Denied When Trying to Append a File to a Root Owned File with Sudo
Explanation of Convertor of Cidr to Netmask in Linux Shell Netmask2Cdir and Cdir2Netmask
Simple Way to Convert Hh:Mm:Ss (Hours:Minutes:Seconds.Split Seconds) to Seconds
How to Divide in the Linux Console
Where Is the Linux Isr Entry Point
How Does the 'Ls' Command Work in Linux/Unix
How to Disable CPU Cache (L1/L2) on a Linux System
Is It Safe to Delete the Journal File of Mongodb
Abuse Curl to Communicate with Redis
How to Export Dbus_Session_Bus_Address
How to Cut an Existing Variable and Assign to a New Variable in Bash