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)

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?

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git