One of my common practices is to perform greps on all files of a certain type, e.g., find all the HTML files that have the word "rumpus" in them. To do it, I use
find /path/to -name "*.html" | xargs grep -l "rumpus"
Occasionally, find
will return a file with a space in its name such as my new file.html
. When xargs
passed this to grep
, however, I get these errors:
grep: /path/to/bad/file/my: No such file or directory
grep: new: No such file or directory
grep: file.html: No such file or directory
I can see what's going on here: either the pipe or the xargs
is treating the spaces as delimiters between files. For the life of me, though, I can't figure out how to prevent this behavior. Can it be done with find
+ xargs
? Or do I have to use an entirely different command?
Use
e.g.
from the find man page
You do not need to use xargs, because find can execute commands itself. When doing this, you do do not have to worry about the shell interpreting characters in the name.
from the find man page
If find and xarg versions on your system doesn't support
-print0
and-0
switches (for example AIX find and xargs) you can use this:Here sed will take care of escaping the spaces for xargs.