Siddharthan Asokan
Siddharthan Asokan

Reputation: 4441

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?

Upvotes: 0

Views: 2359

Answers (2)

Etan Reisner
Etan Reisner

Reputation: 80931

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' ..

Upvotes: 1

Marcus Müller
Marcus Müller

Reputation: 36352

Any version of grep that I personally layed hands on supports the -r switch, recursive.

Just hand it the top directory and grep will do its thing:

grep -r myregex /home/marcus/bunchoffilesindirectories

Upvotes: 0

Related Questions