I want to find all files which contain a specific string of text. The grep
command works, but I don't know how to use it for every directory (I can only do it for my current directory). I tried reading man grep
, but it didn't yield any help.
I want to find all files which contain a specific string of text. The grep
command works, but I don't know how to use it for every directory (I can only do it for my current directory). I tried reading man grep
, but it didn't yield any help.
It would be better to use
where
-r
(or--recursive
) option is used to traverse also all sub-directories of/path
, whereas-l
(or--files-with-matches
) option is used to only print filenames of matching files, and not the matching lines (this could also improve the speed, given thatgrep
stop reading a file at first match with this option).If you're looking for lines matching in files, my favorite command is:
-H
causes the filename to be printed (implied when multiple files are searched)-r
does a recursive search-n
causes the line number to be printedpath/to/files
can be.
to search in the current directoryFurther options that I find very useful:
-I
ignore binary files (complement:-a
treat all files as text)-F
treatsearch term
as a literal, not a regular expression-i
do a case-insensitive search--color=always
to force colors even when piping throughless
. To makeless
support colors, you need to use the-r
option:--exclude-dir=dir
useful for excluding directories like.svn
and.git
.I believe you can use something like this:
Explanation from comments
find
is a command that lets you find files and other objects like directories and links in subdirectories of a given path. If you don't specify a mask that filesnames should meet, it enumerates all directory objects.-type f
specifies that it should process only files, not directories etc.-exec grep
specifies that for every found file, it should run the grep command, passing its filename as an argument to it, by replacing{}
with the filenameMy default command is
I use a capitol 'R' because
ls
uses it for recursive. Since grep accepts both, no reason to not use it.EDIT: per HVNSweeting, apparently
-R
will follow symlinks where as-r
will not.If you’re willing to try something new, give
ack
a shot. The command to recursively search the current directory forstring
is:Installation is quite simple:
(Provided you’ve already got the directory
~/bin
and it’s preferably in yourPATH
.)The command rgrep is dedicated for such need
If not available, you can get it like this
You can directly set into your default grep options as described above.
I personnaly use
related topic : how to always use rgrep with color
grep
(GNU or BSD)You can use
grep
tool to search recursively the current folder with-r
parameter, like:Note:
-r
- Recursively search subdirectories.To search within specific files, you can use a globbing syntax such as:
Note: By using globbing option (
**
), it scans all the files recursively with specific extension or pattern. To enable this syntax, run:shopt -s globstar
. You may also use**/*.*
for all files (excluding hidden and without extension) or any other pattern.If you've the error that your argument is too long, consider narrowing down your search, or use
find
syntax instead such as:Alternatively use
ripgrep
.ripgrep
If you're working on larger projects or big files, you should use
ripgrep
instead, like:Checkout the docs, installation steps or source code on the GitHub project page.
It's much quicker than any other tool like GNU/BSD
grep
,ucg
,ag
,sift
,ack
,pt
or similar, since it is built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast.It supports ignore patterns specified in
.gitignore
files, so a single file path can be matched against multiple glob patterns simultaneously.You can use the common parameters such as:
-i
- Insensitive searching.-I
- Ignore the binary files.-w
- Search for the whole words (in opposite of partial word matching).-n
- Show the line of your match.-C
/--context
(e.g.-C5
) - Increases context, so you see the surrounding code .--color=auto
- Mark up the matching text.-H
- Displays filename where the text is found.-c
- Displays count of matching lines. Can be combined with-H
.Update 2:
This line of commands using
find
andgrep
fixes the problem:--color=<always or auto>
for colored output:Example:
An example run in the snapshot below:
Update 1:
You can try following code; as a function in your
.bashrc
or.bash_aliases
or in a script:Usage:
wherein /path/to/search/in/ searchkeyword
example:
(Note: As suggested in the comments below by @enzotib, this doesn't work with file/directories including spaces in their names.)
Original post
To search for the string and output just that line with the search string:
e.g.:
To display filename containing the search string:
e.g.:
I do this using xargs, a very underrated command
find ./ gives you a recursive list of all the files in a current folder then you pipe it to xargs that executes the grep command on each one of those files
I know there are plenty of answers here, but here's an alternative if you'd like to add other restrictions when searching the files:
This works because
grep
will return 0 if it found a result, 1 otherwise. For example you can find files 1 MB large and containing something:For multiple requirements you probably want to use the optimizer flag
-O
that exists in GNU grep.