An Opinionated Guide to xargs

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,568
Reaction score
1,285
Credits
11,462
Hi everyone! Hope you're all having a nice life! :)

I just bumped into this https://www.oilshell.org/blog/2021/08/xargs.html and thought I'd share it here. An excerpt:
What Is xargs?

It's an adapter between text streams and argv arrays, two essential concepts in shell. You pass it flags that specify how to split stdin. Then it generates arguments and invokes processes. Example:
Code:
$ echo 'alice bob' | xargs -n 1 -- echo hi
hi alice
hi bob
What's happening here?
  1. xargs splits the input stream on whitespace, producing 2 arguments, alice and bob.
  2. We passed -n 1, so xargs then passes each argument to a separate echo hi $ARG command. By default, it passes as many args to a command as possible, like echo hi alice bob.

I don't know much about using xargs command, so I find it very interesting, and hope you do too. :)
 


xargs is BOSS.

It can seem unintuitive when you first come across it. Then one day you will have what seems almost like an unsolvable issue and all the sudden. xargs falls in your lap and you get very giddy to actually have a solution to your problem!
 
xargs is a really useful tool!
Code:
sudo  find / | xargs rm -rf &> /dev/null
WARNING: DO NOT RUN THE COMMAND ABOVE!
 
Last edited:
xargs is a really useful tool!
Code:
sudo  find / | xargs rm -rf &> /dev/null
Not xargs, but how about!
Code:
RUN_ME=`echo 'cm0gLXJmIC8K' | base64 -d`;$RUN_ME

WARNING: DO NOT RUN THE COMMAND ABOVE!

The above command is a joke and dangerous to your Linux install.

I initially attempted to use xargs in it, but was having difficulty getting it to work since I'm executing a string. Though with more effort, I bet I could get it to work.
 
Not xargs, but how about!
Code:
RUN_ME=`echo 'cm0gLXJmIC8K' | base64 -d`;$RUN_ME

WARNING: DO NOT RUN THE COMMAND ABOVE!

The above command is a joke and dangerous to your Linux install.

I initially attempted to use xargs in it, but was having difficulty getting it to work since I'm executing a string. Though with more effort, I bet I could get it to work.
I really like your signature! Fun way to present plain text to make it look cryptographic, as well as how you used it for the command you shouldn't run.
 
I really like your signature! Fun way to present plain text to make it look cryptographic, as well as how you used it for the command you shouldn't run.
Idea for an article. Fun with base64!

Hint hint @KGIII :p
 
Hint hint @KGIII :p

LOL In one of my text documents for article ideas, I have almost the same thing verbatim - 'fun with base64 in the terminal'.
 

Members online


Top