Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to change xargs arguments to include a flag per argument?

I have some passing experience with xargs, but I don't quite know how to do this:

  • I have a list of archives retrieved from tarsnap using tarsnap --list-archives
  • I want to delete all archives from a certain day (there are 24 made each day)

I can use xargs to accomplish this:

tarsnap --list-archives | grep 2014-06-09 | xargs -n 1 tarsnap -df

However this runs tarsnap over and over again with one argument at a time (which is expected):

tarsnap -df 2014-06-09-00
tarsnap -df 2014-06-09-01
tarsnap -df 2014-06-09-02
... etc ...

The tarsnap documentation states that you can delete multiple archives by passing in multiple -f flags:

tarsnap -d -f 2014-06-09-00 -f 2014-06-09-01 -f 2014-06-09-02 # ... and so on

Is there a way to accomplish this with xargs?

(Aside: It might be pointless to even do this, since I have a feeling running tarsnap with multiple -f flags just causes tarsnap to run itself multiple times, one argument at a time... but I could be wrong)

like image 472
Julian H. Lam Avatar asked Sep 15 '25 06:09

Julian H. Lam


1 Answers

You can actually do this with xargs

$ archivelist=$(tarsnap --list-a|xargs -I {} echo "-f {} ")
$ echo $archivelist
-f 2014-06-09-00 -f 2014-06-09-01 -f 2014-06-09-02 -f 2014-06-09-03 
like image 96
markhellewell Avatar answered Sep 17 '25 23:09

markhellewell