I want to search recursively for a certain word, say MyWord, but I want to print the entire contents of all files that contain MyWord. How do I do this?
It turns out that I particularly care about binary sqlite files, both grepping them, and reading the result as a text file, rather than binary.
You can also use
find
withgrep
:This will find files containing the word "MyWord" recursively and quite as soon as first match found then redirect the whole file content to a new file.
I would use:
sudo
if not looking in system directories.'/'
to current directory'.'
or specific directory ie'/home/me/stuff'
mnt
to include Windows files mounted undermnt
.lib
to include Linux source files--exclude=${HISTFILE##*/}
is necessary if you run the command a second time or if the search stringMyWord
has ever been used in a different command before. This prevents 5 thousand history lines ($HISTFILE) from being included.-r
recursive,I
skip binary files,lZ
outputs a zero byte after each file name instead of the usual newline. Null (zero byte) terminators necessary forperl -0
,sort -z
andxargs
used tocat
(print out) the file contents.> MyOutputFile
sends output to file instead of screen. Leave this off for output to screen.Note: Missing from output is the file name, just the file content is listed.
If your folder are not so big try:
if necessary add
-a