[SunHELP] xargs ?

sunhelp at sunhelp.org sunhelp at sunhelp.org
Mon Jan 7 22:50:53 CST 2002


Heh.  xargs is one of my favorite commands --- and one very often overlooked, from what I've seen.

xargs effectively runs a command for each thing passed to it from a pipe.  For instance:
$ find / -name core | xargs rm
will remove all of your core files (barring permissions, etc.  And I know that `find / -name core -exec rm {} \;' is cleaner, but this served as an example...)

One of the best uses I've found for xargs is for killing processes:
# ps -ef | grep baduser | awk '{print $2}' | xargs kill -TERM
This will kill (nicely) all of baduser's processes.  In scripting, I'd probably sleep for a couple of seconds, and do the same with -KILL instead of -TERM to make sure I've gotten the stubborn ones.

Wyatt

> 
> Dear guru
> Someone please distinguish me about the following things:
>  Pipe '|'
>  Redirect '>'
>  xargs
> 
> I understand '|' and '>' but not 'xargs'. Please give me a guide line
> when I should use 'xargs'? how difference between 2 above
> or point me to some source of information about this.
> 
> Thanks in advance
> Patrick.
> _______________________________________________
> SunHELP maillist  -  SunHELP at sunhelp.org
> http://www.sunhelp.org/mailman/listinfo/sunhelp



More information about the SunHELP mailing list