If you have a vast number of files and you want to do something with them, then use something like the following:
grep -l "mystring" * | xargs -I{} echo {}
this will echo
each filename that contains mystring in the current working directory. You could replace echo
linux command with any other command, for instance with rm -f
to remove those files (be careful!). The -I{}
switch to xargs
prevents having problems with filenames that have spaces. If you want to search in a different directory than the working directory replace *
with it.
So lets say you want to find files that contain DNS in /etc. You can do this with:
grep -r -l "DNS" /etc | xargs -I{} echo {}
If you want to find files that contain any form of some string in /home/user. You can do this with:
grep -i -r -l "some string" /home/user | xargs -I{} echo {}
where -i
(or –ignore-case) does a case-insensitive search (i.e. it will match any some string, or Some String, etc.) and -r
stands for recursive.
If you have a complex scenario where you only want to process specific type of files or file patterns, you may want to use find
instead of grep
directly.