How to escape file output for compatibilty with 'xargs'?

Here is the simpler, faster and most portable way to do it :

find $1 -exec touch {} +

Note the + ending syntax. Unlike the more popular \; exec ending syntax, + is packing arguments the same way xargs does.

Compared to the often suggested find ... | xargs ..., this find only solution is more efficient because:

  • a single process is handling whole task
  • no data piping is involved
  • no extra processing associated with the "\0" hack is required.

Being POSIX compliant, it also works with most if not all current find implementations unlike find -print0 and xargs -0 which are both GNUisms.


find $1 -print0 | xargs -0 touch

That terminates each filename with \000 (character 0) and instructs xargs to expect the filenames terminated by \000

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