Skip to content

Instantly share code, notes, and snippets.

@nestserau
Created April 29, 2014 20:41
Show Gist options
  • Save nestserau/955e609f8c8175c5212d to your computer and use it in GitHub Desktop.
Save nestserau/955e609f8c8175c5212d to your computer and use it in GitHub Desktop.
How to search for a word in entire content of a directory in linux
If your grep supports the -r or -R option for recursive search, use it.
grep -r word .
Otherwise, use the -exec primary of find. This is the usual way of achieving the same effect as xargs, except without constraints on file names. Reasonably recent versions of find allow you to group several files in a single call to the auxiliary command. Passing /dev/null to grep ensures that it will show the file name in front of each match, even if it happens to be called on a single file.
find . -type f -exec grep word /dev/null {} +
Older versions of find (on older systems or OpenBSD, or reduced utilities such as BusyBox) can only call the auxiliary command on one file at a time.
find . -type f -exec grep word /dev/null {} \;
Some versions of find and xargs have extensions that let them communicate correctly, using null characters to separate file names so that no quoting is required. These days, only OpenBSD has this feature without having -exec … {} +.
find . -type f -print0 | xargs -0 grep word /dev/null
http://unix.stackexchange.com/questions/21169/how-to-search-for-a-word-in-entire-content-of-a-directory-in-linux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment