Search code examples
grepfindxargstext-search

grep search on subdirectories


I'm trying to search for a text in a directory among all files. Its expected to access the subfolders too. There are folder & files with whitespace characters in their name. I'm using the following command to search.

find . -type f | xargs grep -rls 'my text'

This doesn't access the folder and files with white space characters. Any suggestion on what to change on the command?


Solution

  • That should handle files with whitespace just fine I think (though not newlines) but you can use find -print0 and xargs -0 to be safer.

    That being said your current use of the -r argument to grep is pointless since it will only ever be given directories to operate on. And additionally your use of find here is pointless since grep -r does what you want here directly.

    Just use grep -rls 'my text' ..