Shell command options you didn't know you needed #3

fleetfootmike

Mike Whitaker

Posted on March 15, 2020

Shell command options you didn't know you needed #3

More well known, perhaps, than the previous two, but very useful for those of us building shell pipelines to clean up and process our messes.

find -print0 <stuff> | xargs -0 <command>

The problem with find which you trip over a lot when processing file trees, is that if you're not careful, piping the results into xargs (which I do a lot 'cause I can never remember how -exec works :D) breaks when find returns filenames with whitespace in.

These two handy options to the rescue!

find -print0 returns a list of filenames, except that the names are terminated with ASCII NUL (\0) instead of a newline. The -0 option to xargs then tells it to expect input in that form, and quote each filename as it gets it. Result, filenames with whitespace in don't get broken up by the shell parser.

And they're not the only commands that can do this. Watch this space!

💖 💪 🙅 🚩
fleetfootmike
Mike Whitaker

Posted on March 15, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related