DEV Community

Mike Whitaker
Mike Whitaker

Posted on

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!

Top comments (1)

Collapse
 
instinct profile image
Instinct • Edited

Hey, thanks a lot for writing this series.
To be frank, I have some questions on Linux and Bash which only a pro like can solve.
Would mind helping me?