I would use:
sudo grep -rIlZ --exclude=${HISTFILE##*/} --exclude-dir={boot,dev,lib,media,mnt,proc,root,run,sys,/tmp,tmpfs,var} '/' -e 'MyWord' | xargs -0 cat > MyOutputFile
- Drop
sudo
if not looking in system directories.
- Change (root directory)
'/'
to current directory '.'
or specific directory ie '/home/me/stuff'
- Remove
mnt
to include Windows files mounted under mnt
.
- Remove
lib
to include Linux source files
- Excluding directories necessary for speed. See: `grep`ing all files for a string takes a long time
--exclude=${HISTFILE##*/}
is necessary if you run the command a second time or if the search string MyWord
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 for perl -0
, sort -z
and xargs
used to cat
(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.
-A
,-B
,-C
) may be more useful – steeldriver Apr 18 '18 at 01:58