Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To understand xargs better

Tags:

shell

xargs

I want to understand the use of xargs man in Rampion's code:

screen -t man /bin/sh -c 'xargs man || read'

Thanks to Rampion: we do not need cat!

Why do we need xargs in the command?

I understand the xargs -part as follows

  1. cat nothing to xargs
  2. xargs makes a list of man -commands

I have had an idea that xargs makes a list of commands. For instance,

find . -type f -print0 | xargs -0 grep masi 

is the same as a list of commands:

find fileA AND grep masi in it 
find fileB AND grep masi in it 
           and so on for fileC, fileD, ...
like image 536
Léo Léopold Hertz 준영 Avatar asked Nov 21 '25 08:11

Léo Léopold Hertz 준영


2 Answers

No, I don't cat nothing. I cat whatever input I get after I run the command. cat is actually extraneous here, so let's ignore it.

xargs man waits on user input. Which is necessary. Since in the script you grabbed that from, I can't paste in the argument for man until after I create the window. So the command that runs in the window needs to wait for me to give it something, before it tries to run man.

If we just ran screen /bin/sh -d 'man || read', it would always complain "What manual page do you want?" since we never told it.

like image 197
rampion Avatar answered Nov 24 '25 02:11

rampion


xargs gathers arguments from stdin and executes the command given with those arguments.

so cat is waiting for something to be typed, and then xargs is running man with that input.

xargs is useful if you have a lot of files to process, I often use it with output from find. xargs will stuff as many arguments as it can onto the command line.
It's great for doing something like

find . -name '*.o' -print | xargs rm
like image 27
Charlie Avatar answered Nov 24 '25 02:11

Charlie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!