Re: find | xargs | grep over filenames with spaces

salmobytes <salmobytes@xxxxxxxxxxxx> écrivit:
Let's say I have a directory tree where both filenames and directory
names contain spaces in the names (not a good idea but it
happens I want to deal with it).

I know how to write a recursive script to fix the filenames.
But let's say I choose not to change the names, and I want to grep
for a pattern in all files with suffix *.txt

for file in `find . -name "*\.txt"`
grep "whatever" "$file"

....throws all sorts of errors, because grep tries
look at files that don't exist (parses space delimited
bits and pieces of file and directory names).

find . -name '*.txt' -print0 | xargs -0 grep -h "whatever"
find . -name '*.txt' -exec grep whatever {} \;

The latter supports an unlimited number of file names.

These commands may contain GNU-isms. I'm sorry for inconvenience.
André Gillibert

Relevant Pages