Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Jun 242010

xargs when the filename contains a newline


It is always handy to combine the power of find and xargs for a lot of tasks, such as

# find /tmp -type f | xargs rm -rf

However, the command will failed if the a file contains a newline character. To solve this, you can do like below.

# find /tmp -type f -print0 | xargs -0 rm -rf

Now the file list returned by the find command is terminated by a null character, so it will not mixed with the newline character if they are part of the filename. Also, the xargs -0 option will treat the null character as the delimiter (rather than newline).

  One Response to “xargs when the filename contains a newline”

  1. Linux is very line oriented. xargs is an odd child in this respect as it deals badly with white space, ' and ".

    GNU Parallel may be a better choice as it is designed to be line oriented.

    Watch the intro video to GNU Parallel:

 Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>