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?

3 Answers 3



find ... -print0 | xargs -0 ...


find /path/to -name "*.html"  -print0 | xargs -0  grep -l "rumpus"

from the find man page

          True; print the full file name on the standard  output,  followed
          by  a  null  character  (instead  of  the  newline character that
          ‘-print’ uses).  This allows file names that contain newlines  or
          other  types  of  white space to be correctly interpreted by pro-
          grams that process the find output.  This option  corresponds  to
          the ‘-0’ option of xargs.

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.

find /path/to -name "*.html" -exec grep -l "rumpus" '{}' +

from the find man page

-exec command {} +
This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines. Only one instance of `{}' is allowed within the command. The command is executed in the starting directory.

  • I would upvote this but I have non left for today - I'll do it tomorrow.
    – user9517
    May 10, 2011 at 21:37
  • 1
    @Iain - there you go (I agree, by the way). May 10, 2011 at 21:41
  • Just using find you still miss out on features of xargs, plus you have to deal with silly quoting rules. See the -P argument to xargs if you have more than one core / CPU. May 11, 2011 at 3:31
  • What do the single quotes do in '{}' ? I don’t see any difference in results when I exclude them. May 2, 2020 at 3:11

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:

find /your/path -name "*.html" | sed 's/ /\\ /g' | xargs grep -l "rumpus"

Here sed will take care of escaping the spaces for xargs.

  • This helped me because I had a long list of file names that was difficult to produce that I used several times. I can't just go find them all again.
    – Scott M.
    Jan 2, 2016 at 19:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .