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

6 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:

4 Likes

I’ve used xargs on occasion but for the things I usually do it is a bit too limited. Especially when you want to conditionally start each thread.
I often make a liberal usage of ‘&’ instead, although it linguistically lacks some elegance. :wink:

That’s why I wrote a small ‘framework’ for ‘process oriented programming’.
It takes care of the process handling (and IPC) which enables a clean syntax for parallel and multi processing

3 Likes

xargs is definitely a valuable tool to have in your toolbox. The only thing I’d add is that these days I try to default to find 
 -print0 | xargs -0 
 for bulk operations, as it safely handles filenames containing spaces, quotes, and other unusual characters.

It’s one of those classic Unix utilities that remains incredibly useful when used with the right safeguards.

4 Likes

My command line toolbag consists of:
sudo apt-get update
sudo apt-get dist-upgrade
pkill
sudo apt --fix-broken install (I copy and paste this one, but find it the most essential command in case an upgrade gets interrupted).

The rest I use a GUI for, and if in really serious trouble I have a cheat sheet I can copy and paste from. LOL

From a DistroWatch Poll:
How many command line shells do you use regularly?

|Zero:|20 (4%)|
|One:|343 (73%)|
|Two:|67 (14%)|
|Three:|14 (3%)|
|Four:|5 (1%)|
|Five:|2 (0%)|
|More than five:|22 (5%)|

More proof you don’t have to be a tech geek to use Linux!

6 Likes

I don’t need xargs very often any more, but it has definitely come in handy many times over the course of my career. Who knows when it may come in handy again though? It’s still in my personal “toolchest”.

4 Likes