Skip to content
Advertisement

Variable as Tar file name in SunOs 5.10

I am trying to get the following result in the

1.files archived as tar with current date as a part of the tar name. 2.files removed once archived.

/usr/sap/ST1/POC/backtest/*.txt has 2 txt files.

(find /usr/sap/ST1/POC/backtest/*.txt )| xargs -I % sh -c 'tar cvf arc_2017-02-28.tar % ; rm -f %'

This works fine as the file name is mentioned as arc_2017-02-28.tar.

But when I tried this:

arc_name="arc_"`date +%F`".tar"
(find /usr/sap/ST1/POC/backtest/*.txt )| xargs -I % sh -c 'tar cvf "$arc_name" % ; rm -f %'

The output is an error:

tar: : No such file or directory

Please help me proceed with this.

Advertisement

Answer

this would work normally :

(find /usr/sap/ST1/POC/backtest/*.txt) | xargs -I {} sh -c 'arc_name="arc_"`date +%F`".tar"; tar cvf "$arc_name" {} ; rm -f {}'

so the issue with your command was that sh -c seems that can’t read your variable , if we can call that is variable scope so to speak ,

so moving the variable arc_name="arc_"date +%F".tar" into the sh makes sense .

but you will have to change the % in xargs because this will produce an issue with your % symbol in date +%F function

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement