Xargs Should Be In Your Command Line Toolbag

Kudos to Distro Tube for coming up with numerous useful commands and utilities. If by any chance you do not yet know how to use xargs, this is a very useful video to watch.

Distro Tube shows us the basics of xargs

3 Likes

I was introduced to xargs about 20 years ago, and could NOT imagine not having that in my toolbox.

xargs is meant for applying a command consecutively to an entire list of items that come via the input stream, such as the output of a
“find -print” command.

In my opinion, the best form is

xargs -I{}  ${command} '{}'

# for example:

xargs -I{}  file  '{}'
xargs -I{}  chown -v -h ${Owner}  '{}'
xargs -I{}  stat --format "%n|%A|%U|%G|%s|%Y|%Z|%F"  '{}'

The single quotes around the last set of braces ensures proper handling of filenames that have spaces.

When performing such “bulk” operations, keep in mind that some filenames may include single quotes. You have to build in a logic to split those items off into a separate list before attempting the xargs, and use the double quotes for those cases. Just make sure those same cases don’t also include double quotes by some strange quirk of circumstances.

:slight_smile: