I'm trying to find a way to scan my entire Linux system for all files containing a specific string of text. Just to clarify, I'm looking for text within the file, not in the file name.
When I was looking up how to do this, I came across this solution twice:
find / -type f -exec grep -H 'text-to-find-here' {} \;
However, it doesn't work. It seems to display every single file in the system.
Is this close to the proper way to do it? If not, how should I? This ability to find text strings in files would be extraordinarily useful for some programming projects I'm doing.
-H
with -l
(and maybe grep
with fgrep
). To exclude files with certain patterns of names you would use find
in a more advanced way. It's worthwile to learn to use find
, though. Just man find
- Walter Tross 2013-10-28 12:01
find … -exec <cmd> +
is easier to type and faster than find … -exec <cmd> \;
. It works only if <cmd>
accepts any number of file name arguments. The saving in execution time is especially big if <cmd>
is slow to start like Python or Ruby scripts - hagello 2016-01-28 05:16
grep "pattern" path/*.txt
fedorqui 2016-12-02 13:13
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.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 a particular directory(ies) through --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 check man grep
.
grep -rnw --include=*.java . -e "whatever I'm looking for - Lucas A. 2013-11-14 15:43
r
option is lazy (traverses depth-first, than stops after the first directory), while R
is greedy (will traverse the entire tree correctly) - Eliran Malka 2015-03-24 15:09
-r
, though, if the argument is a directory. grep pattern -r path/to/somewhere
mkdrive2 2016-02-09 22:12
grep -rnw --colour . -e "terminal"
mattbell87 2016-03-07 05:27
R
en r
will both traverse directories correctly, but R
will follow symbolic links - bzeaman 2016-07-05 08:36
grep -rnwl
54l3d 2016-12-16 09:13
"/usr/bin/grep: 0403-027 The parameter list is too long. - VIPIN KUMAR 2017-01-27 14:30
-i
option very useful too to "ignore case". Maybe throw this in your list of options - Gabriel Staples 2017-03-25 01:37
-w
parameter. I found that it will not match arbitrary text - entpnerd 2017-05-03 17:24
findin(){
grep -rnw "$2" -e "$1"
- talsibony 2017-09-15 11:12
--include-dir
option. Was wondering when this was included, on my first version? Can't remember. Would be nice if it was possible to see the answer change log like git - rakib_ 2018-01-04 03:53
s
for some amazing "ignore error and warnings" goodies! Great for big searches - GigaBass 2018-06-07 04:47
You can use grep -ilR
:
grep -Ril "text-to-find-here" /
i
stands for ignore case (optional in your case). R
stands for recursive. l
stands for "show the file name, not the result itself"./
stands for starting at the root of your machine.Do you think using a regular expression with grep would make it go faster - Nathan 2013-06-06 08:12
-i
makes it slow down a lot, so don't use it if not necessary. Test it in a certain dir and then generalise. It should be completed within few minutes. I think a regular expression would make it slower. But my comments are based on suppositions, I suggest you to test it with time
in front of the line - fedorqui 2013-06-06 08:14
/*
stands for that. Anyway I just tested it and noticed that just /
works - fedorqui 2013-06-06 08:15
It seems like this is capturing a lot of files that I can't even open up in a text editor. Is it perhaps interpreting all formats at text, and so randomly finding results in binary "noise" in an executable file for instance - Nathan 2013-06-06 08:20
2>/dev/null | grep -v "Binary file"
fedorqui 2013-06-06 08:28
fgrep is the same as grep -F -> Interpret PATTERN as a list of fixed strings
- fedorqui 2013-09-30 08:23
-i
it is definitely slower, but it is complicated to know how much: if you run the test with and without, probably the results of the first are cached and used while running the second. Also, it is more "expensive" to check a long word (aBcDEFghIj
) than a short one (aBc
), because there are way more possible combinations of upper/lowercase - fedorqui 2015-01-05 09:13
ack 'search text'
command, it works really nicely for what I needed it for and is very fast. Its nice because of the nice highlighting it uses - wired00 2015-01-05 21:16
grep -Ril "text-to-find-here" ~/sites/
or use . for current directory grep -Ril "text-to-find-here" .
Black 2016-01-28 12:19
-I
(capital i) to exclude binary files. I think this was the key point here, instead of exclude
or include
. Better late than never : - fedorqui 2016-03-24 23:47
grep -rl 'pattern' .
Recursive works with lowercase -r
as well.
Search Scope dot .
for current directory is more intutive AFAIK rathan than /
nitinr708 2016-07-08 09:43
-R
and -r
are different. From man grep
: Read all files under each directory, recursively. Follow all links, unlike -r.fedorqui 2016-07-08 09:45
grep
slows down on -i
and long words, as I should have thought that the Boyer-Moore “generated-skip-length-table-driven” string search algorithm would be just as fast on -i
and faster on longer words — presumably Linux grep
is using something simpler! But https://en.wikipedia.org/wiki/Boyer%E2%80%93Moorestringsearch_algorithm#Implementations says it is used - PJTraill 2017-11-01 21:18
.py
and .txt
files - Cleb 2017-11-28 10:13
find -name filename.txt -exec grep 'x' {} \;
fedorqui 2018-01-22 07:36
-I
for binary files - fedorqui 2018-05-30 10:22
You can use ack. It is like grep for source code. You can scan your entire file system with it.
Just do:
ack 'text-to-find-here'
In your root directory.
You can also use regular expressions, specify the filetype, etc.
UPDATE
I just discovered The Silver Searcher, which is like ack but 3-5x faster than it and even ignores patterns from a .gitignore
file.
You can use:
grep -r "string to be searched" /path/to/dir
The r
stands for recursive and so will search in the path specified and also its sub-directories. This will tell you the file name as well as print out the line in the file where the string appears.
Or a command similar to the one you are trying (example: ) for searching in all javascript files (*.js):
find . -name '*.js' -exec grep -i 'string to search for' {} \; -print
This will print the lines in the files where the text appears, but it does not print the file name.
In addition to this command, we can write this too: grep -rn "String to search" /path/to/directory/or/file -r: recursive search n: line number will be shown for matches
You can use this:
grep -inr "Text" folder/to/be/searched/
First of all, I believe you have used -H
instead of -l
. Also you can try adding the text inside quotes followed by {} \
.
find / -type f -exec grep -l "text-to-find-here" {} \;
Let's say you are searching for files containing specific text "Apache License" inside your directory. It will display results somewhat similar to below (output will be different based on your directory content).
bash-4.1$ find . -type f -exec grep -l "Apache License" {} \;
./net/java/jvnet-parent/5/jvnet-parent-5.pom
./commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.pom
./io/swagger/swagger-project/1.5.10/swagger-project-1.5.10.pom
./io/netty/netty-transport/4.1.7.Final/netty-transport-4.1.7.Final.pom
./commons-codec/commons-codec/1.9/commons-codec-1.9.pom
./commons-io/commons-io/2.4/commons-io-2.4.pom
bash-4.1$
Even if you are not use about the case like "text" vs "TEXT", you can use the -i
switch to ignore case. You can read further details here.
Hope this helps you.
find
will pass all the paths it finds to the command grep -l "text-to-find-here" <file found>"
. You may add restrictions to the file name, e.g. find / -iname "*.txt"
to search only in files which name ends in .txt
Mene 2017-04-20 13:46
-iname
is case-insensitive which means it would also find .TXT files, for example, as well as TxT and TXt and so on - Pryftan 2018-05-01 23:04
If your grep
doesn't support recursive search, you can combine find
with xargs
:
find / -type f | xargs grep 'text-to-find-here'
I find this easier to remember than the format for find -exec
.
This will output the filename and the content of the matched line, e.g.
/home/rob/file:text-to-find-here
Optional flags you may want to add to grep
:
-i
- case insensitive search-l
- only output the filename where the match was found-h
- only output the line which matched (not the filename)grep 'text-to-find-here'
without file name if find
does not find anything. This will hang and wait for user input! Add --no-run-if-empty
as an option to xargs
- hagello 2016-01-28 05:46
find … -exec grep … +
. If you insist on using find together with xargs, use -print0
and -0
- hagello 2016-01-28 05:50
grep
(GNU or BSD)You can use grep
tool to search recursively the current folder, like:
grep -r "class foo" .
Note: -r
- Recursively search subdirectories.
You can also use globbing syntax to search within specific files such as:
grep "class foo" **/*.c
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:
find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'
Alternatively use ripgrep
.
ripgrep
If you're working on larger projects or big files, you should use ripgrep
instead, like:
rg "class foo" .
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
.grep -insr "pattern" *
i
: Ignore case distinctions in both the PATTERN and the input files.n
: Prefix each line of output with the 1-based line number within its input file.s
: Suppress error messages about nonexistent or unreadable files.r
: Read all files under each directory, recursively.Try:
find . -name "*.txt" | xargs grep -i "text_pattern"
xargs
like that .. consider this. echo "file bar.txt has bar" > bar.txt; echo "file foo bar.txt has foo bar" > "foo bar.txt"; echo "You should never see this foo" > foo; find . -name "*.txt" | xargs grep -i foo #
./foo:You should never see this foo
. The xargs
here matched the WRONG file and did NOT match the intended file. Either use a find .. -print0 | xargs -0 ...
but that's a useless use of a pipe or better find ... -exec grep ... {} +
shalomb 2016-10-11 20:10
Use pwd
to search from any directory you are in, recursing downward
grep -rnw `pwd` -e "pattern"
Update
Depending on the version of grep you are using, you can omit pwd
. On newer versions .
seems to be the default case for grep if no directory is given
thus:
grep -rnw -e "pattern"
or
grep -rnw "pattern"
will do the same thing as above!
pwd
is not necessary at all, since it is the default. grep -rnw "pattern"
suffices - fedorqui 2016-12-02 13:17
grep -rnw
and similar is what was answered like three years ago, I don't see how this answer is adding value - fedorqui 2016-12-02 14:03
grep -rnw '/path/to/somewhere/' -e "pattern"
which is what you have here. 5 votes after 2.3M visits does not mean that much - fedorqui 2016-12-14 08:45
grep "pattern"
needs something to check (a dir, a file...) while grep -R "pattern"
works standalone. Then probably an addition to the answer would benefit more people (last posts are rarely noticed). But I am glad you learned from it :) I have the 2nd most upvoted answer and I see there are many, many variants of doing this - fedorqui 2016-12-14 14:39
pwd
I was trying to find an easy hack to not type in the full path, but I am sure .
would suffice as well, as current directory, but leaving it out altogether is of course the leanest - mahatmanich 2016-12-14 15:07
pwd
is needed @fedorqu - mahatmanich 2017-02-02 10:09
cd /tmp; mkdir mytest; cd mytest; mkdir a{1..3}; seq 10 > a1/a1; seq 10 > a1/a2; seq 10 > a2/a1
once you have all of this, do write grep -rnw 5
. This works fine to me on GNU grep 2.16 - fedorqui 2017-02-02 10:13
There's a new utility called The Silversearcher
sudo apt install silversearcher-ag
It works closely with Git and other VCS. So you won't get anything in a .git or another directory.
You can simply use
ag -ia "Search query"
And it will do the task for you!
How do I find all files containing specific text on Linux? (...)
I came across this solution twice:
find / -type f -exec grep -H 'text-to-find-here' {} \;
If using find like in your example, better add -s
(--no-messages
) to grep
, and 2>/dev/null
at the end of the command to avoid lots of Permission denied messages issued by grep
and find
:
find / -type f -exec grep -sH 'text-to-find-here' {} \; 2>/dev/null
find is the standard tool for searching files - combined with grep when looking for specific text - on Unix-like platforms. The find command is often combined with xargs, by the way.
Faster and easier tools exist for the same purpose - see below. Better try them, provided they're available on your platform, of course:
RipGrep - fastest search tool around:
rg 'text-to-find-here' / -l
ag 'text-to-find-here' / -l
ack:
ack 'text-to-find-here' / -l
Note: You can add 2>/dev/null
to these commands as well, to hide many error messages.
Warning: unless you really can't avoid it, don't search from '/' (the root directory) to avoid a long and inefficient search! So in the examples above, you'd better replace '/' by a sub-directory name, e.g. "/home" depending where you actually want to search...
grep
find
doesn't directly search the inside of files for text. And maybe those additional tools are useful to some but old timers and those whoa are well accustomed to e.g. grep
wouldn't give them any time at all (well I certainly won't). Not saying they're useless though - Pryftan 2018-05-01 23:36
fol
? No of course not; it was called dir
(and I believe it still is). Folder is a thing contrived for (I guess) user friendliness though in this case it's maybe dumbing it down for less 'advanced' users - Pryftan 2018-06-02 00:15
-
you might be wise to first pass to grep --
. I was also pretty sure that {}
should be quoted or escaped but I could be remembering wrong. Also I find it instructive that you included alternative tools (not that I would ever use them but then I don't need help with this task in the question anyway). Of course depending on what's wanted you could just as well use recursion with grep
. Just some additional thoughts for whatever they might be worth - Pryftan 2018-06-02 20:04
grep
can be used even if we're not looking for a string.
Simply running,
grep -RIl "" .
will print out the path to all text files, i.e. those containing only printable characters.
ls
or find
(for the recursive - fedorqui 2016-12-02 13:15
Here are the several list of commands that can be used to search file.
grep "text string to search” directory-path
grep [option] "text string to search” directory-path
grep -r "text string to search” directory-path
grep -r -H "text string to search” directory-path
egrep -R "word-1|word-2” directory-path
egrep -w -R "word-1|word-2” directory-path
egrep
is equivalent to grep -E
and it means --extended-regexp
you can find details here https://unix.stackexchange.com/a/17951/19607 - omerhakanbilici 2018-07-25 11:00
find /path -type f -exec grep -l "string" {} \;
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 proceed only files, not directories etc.
-exec grep specifies that for every found file, it should run grep command, passing its filename as an argument to it, by replacing {} with the filename
Hope this is of assistance...
Expanding the grep
a bit to give more information in the output, for example, to get the line number in the file where the text is can be done as follows:
find . -type f -name "*.*" -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"
And if you have an idea what the file type is you can narrow your search down by specifying file type extensions to search for, in this case .pas
OR .dfm
files:
find . -type f \( -name "*.pas" -o -name "*.dfm" \) -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searchtext"
Short explanation of the options:
.
in the find
specifies from the current directory.-name
"*.*
" : for all files
( -name "*.pas
" -o -name "*.dfm
" ) : Only the *.pas
OR *.dfm
files, OR specified with -o
-type f
specifies that you are looking for files-print0
and --null
on the other side of the |
(pipe) are the crucial ones, passing the filename from the find
to the grep
embedded in the xargs
, allowing for the passing of filenames WITH spaces in the filenames, allowing grep to treat the path and filename as one string, and not break it up on each space.-name '*.*'
isn't what you say; it wouldn't pick up on a file called 'file' because the pattern doesn't equate to that (no .ext); *
would however (well . files aside). But there's another thing: if you want all files why bother specifying a file name in the first place? No other comment - except that it's nice to know that there still are people who don't use the MS terminology 'folder' (which really after saying it enough I wouldn't add but I wanted to point out the slightly incorrect statement you made with file names - as well as the redundancy/uselessness in the case of 'all') - Pryftan 2018-05-01 23:25
Try:
find / -type f -exec grep -H 'text-to-find-here' {} \;
which will search all file systems, because /
is the root folder.
For home folder use:
find ~/ -type f -exec grep -H 'text-to-find-here' {} \;
For current folder use:
find ./ -type f -exec grep -H 'text-to-find-here' {} \;
Silver Searcher is a terrific tool, but ripgrep may be even better.
It works on Linux, Mac and Windows, and was written up on Hacker News a couple of months ago (this has a link to Andrew Gallant's Blog which has a GitHub link):
A Simple find
can work handy. alias it in your ~/.bashrc
file:
alias ffind find / -type f | xargs grep
Start a new terminal and issue:
ffind 'text-to-find-here'
I wrote a Python script which does something similar. This is how one should use this script.
./sniff.py path pattern_to_search [file_pattern]
The first argument, path
, is the directory in which we will search recursively. The second argument, pattern_to_search
, is a regular expression which we want to search in a file. We use the regular expression format defined in the Python re
library. In this script, the .
also matches newline.
The third argument, file_pattern
, is optional. This is another regular expression which works on a filename. Only those files which matches this regular expression will be considered.
For example, if I want to search Python files with the extension py
containing Pool(
followed by word Adaptor
, I do the following,
./sniff.py . "Pool(.*?Adaptor" .*py
./Demos/snippets/cubeMeshSigNeur.py:146
./Demos/snippets/testSigNeur.py:259
./python/moose/multiscale/core/mumbl.py:206
./Demos/snippets/multiComptSigNeur.py:268
And voila, it generates the path of matched files and line number at which the match was found. If more than one match was found, then each line number will be appended to the filename.
Use:
grep -c Your_Pattern *
This will report how many copies of your pattern are there in each of the files in the current directory.
To search for the string and output just that line with the search string:
for i in $(find /path/of/target/directory -type f); do grep -i "the string to look for" "$i"; done
e.g.:
for i in $(find /usr/share/applications -type f); \
do grep -i "web browser" "$i"; done
To display filename containing the search string:
for i in $(find /path/of/target/directory -type f); do if grep -i "the string to look for" "$i" > /dev/null; then echo "$i"; fi; done;
e.g.:
for i in $(find /usr/share/applications -type f); \
do if grep -i "web browser" "$i" > /dev/null; then echo "$i"; \
fi; done;
find … -exec grep 'str' {} \;
(if you have to use find
at all) - phk 2016-10-07 16:14
find
contained spaces .. you could end up grepping
the wrong files and/or missing the right files altogether. Just use find ... -exec grep ...
if you have a need to use find
.. but in this case a grep -r ...
suffices - shalomb 2016-10-11 20:19
There is an ack
tool that would do exactly what you are looking for.
http://linux.die.net/man/1/ack
ack -i search_string folder_path/*
You may ignore -i
for case sensitive search
grep is your good friend to achieve this.
grep -r <text_fo_find> <directory>
if you don't care about the case of the text to find then use
grep -ir <text_to_find> <directory>
-
s in the search string you'll want to pass in --
to grep first; that can cause interesting side effects otherwise - Pryftan 2018-06-02 00:25
You can use below command as you don't want file name but you want to search from all the files. Here are i am capturing "TEXT" form All the log files making sure that file name is not printed
grep -e TEXT *.log | cut -d' ' --complement -s -f1
grep with -e option is quite quick compared to other option as it is for PATTERN match
#
because other than comments that typically implies something - and you shouldn't be root unless you absolutely have to be. Even so you needn't have the prompt surely? Call this petty but I have seen people many times over the years simply copy and paste and do things without truly understanding it. Not saying any will here but still.. Just a thought - Pryftan 2018-06-02 01:30
The below command will work fine for this approach:
find ./ -name "file_pattern_name" -exec grep -r "pattern" {} \;
find
and then grep -r
? They are meant for the same, so this is redundant - fedorqui 2015-12-23 17:02
All previous answers suggest grep and find. But there is another way: Use midnight commander
It is a free utility (30 years old, proven by time) which is visual without being GUI. Has tons of functions, finding files is just one of them.
I am fascinated by how simple grep makes it with 'rl'
grep -rl 'pattern_to_find' /path/where/to/find
-r to find recursively file / directory inside directories..
-l to list files matching the 'pattern'
Use '-r' without 'l' to see the file names followed by text in which the pattern is found!
grep -r 'pattern_to_find' /path/where/to/find
Works just perfect..
Hope it helps!
Try this:
find . | xargs grep 'word' -sl
grep
directly it pipes all the files find
finds to xargs running grep
on it. I'm sure you understand that but just to add to those who might not. The command here is .. I can't atm think of a good analogy but it's adding a lot of unnecessary and harmless overhead - Pryftan 2018-06-02 01:34
Avoid the hassle and install ack-grep. It eliminates a lot of permission and quotation issues.
apt-get install ack-grep
Then go to the directory you want to search and run the command below
cd /
ack-grep "find my keyword"
If you have a set of files that you will always be checking you can alias their paths, for example:
alias fd='find . -type f -regex ".*\.\(inc\|info\|module\|php\|test\|install\|uninstall\)"'
Then you can simply filter the list like this:
grep -U -l $'\015' $(fd)
Which filters out the list fd to files that contain the CR pattern.
I find that aliasing the files that I am interested in helps me create easier scripts then always trying to remember how to get all those files. The recursive stuff works as well but sooner or later you are going to have to contend with weeding out specific file types. Which is is why I just find all the file types I'm interested in to begin with.
grep -Erni + "text you wanna search"
The command will search recursivly in all files and directories of the current directory and print the result.
Note: if your grep output isn't colored, you can change it by using the grep='grep --color=always' alias in your shell src file
-i
makes the search case-insensitive; by default it doesn't have that - nor should it as Unix (etc.) isn't a case-insensitive OS. You might also want to specify what the other options are for too - Pryftan 2018-06-02 01:31
Try this:
find / -type f -name "*" -exec grep -il "String_to_search" {} \;
Or
for i in /*;do grep -Ril "String_to_search" $i;done 2> /dev/null
grep -rnw '/path/to/somewhere/' -e "pattern" >>> got the error "/usr/bin/grep: 0403-027 The parameter list is too long."
grep -Ril "text-to-find-here" / >>> got the error "/usr/bin/grep: 0403-027 The parameter list is too long."
ack 'text-to-find-here' >>> got the error "Segmentation fault(coredump) - VIPIN KUMAR 2016-12-02 17:07
find / -type f -exec grep -H 'text-to-find-here' {} \; >>> It will produce the result with filename only.
for i in /*;do grep -Ril "Stringtosearch" $i;done 2> /dev/null >>> It will work like grep -Ril "text-to-find-here" / but support large number of file - VIPIN KUMAR 2016-12-02 17:07
xargs
is for. Unsure on AIX if it has that though; no comment on your actual commands - Pryftan 2018-06-02 01:36
You can use the following commands to find particular text from a file:
cat file | grep 'abc' | cut -d':' -f2
As Peter in the previous answer mentioned, all previous answers suggest grep and find.
But there is more sophisticated way using Gnome Commander with perfect GUI and with tons of options since 2001, finding files is just one of them. It is a free utility as well proven by time.
find
with xarg
s is preferred when there are many potential matches to sift through. It runs more slowly than other options, but it always works. As some have discovered,xargs
does not handle files with embedded spaces by default. You can overcome this by specifying the -d
option.
Here is @RobEarl's answer, enhanced so it handles files with spaces:
find / -type f | xargs -d '\n' grep 'text-to-find-here'
Here is @venkat's answer, similarly enhanced:
find . -name "*.txt" | xargs -d '\n' grep -i "text_pattern"
Here is @Gert van Biljon's answer, similarly enhanced:
find . -type f -name "*.*" -print0 | xargs -d '\n' --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"
Here is @LetalProgrammer's answer, similarly enhanced:
alias ffind find / -type f | xargs -d '\n' grep
Here is @Tayab Hussain's answer, similarly enhanced:
find . | xargs -d '\n' grep 'word' -sl
If you strictly want to use find
then:
find /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;
find + grep
1.Use find
to search files,
2.Then execute grep
on all of them.
Can be combined in one command as below:
find /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;
Use -name Pattern
if you want to grep
only certain files:
find /path/to/somewhere/ -type f -name \*.cpp -exec grep -nw 'textPattern' {} \;
This can give you the power of find
to find files. You can play with it and use different options of find
to improve or narrow down your file search.
Try this
find . -type f -name some_file_name.xml -exec grep -H PUT_YOUR_STRING_HERE {} \;
See also The Platinium Searcher, which is similar to The Silver Searcher and it's written in Go.
Example:
pt -e 'text to search'
Gui Search Alternative - For Desktop Use :
- As the question is not precisely asking for commands
Searchmonkey : Advanced file search tool without having to index your system using regular expressions. Graphical equivalent to find/grep. Available for Linux (Gnome/KDE/Java) & Win (Java) - Open-Source GPLv3
Features :
Download - Links :
.
Screen-shot :
.
as a single-character wildcard, among others. My advice is to alway use either fgrep or egrep - Walter Tross 2013-10-28 11:54