I'm creating tar file by running the script. Script is in folder A. I want to append the file into tar. The input file and tar file both are in folder B. When I am appending file to existing tar I have give absolute file path as it is in another folder and when its going to append into tar it should get renamed to the name I want.


  • Script location: /root/test/A
  • File location I want to add in tar: /root/myFolder/B/foo
  • Tar file location: /root/myFolder/B/archive.tar

While appending /root/myFolder/B/foo to archive.tar it should get renamed to bar.

I'm trying this command but no success:

tar --transform='flags=r;s|/root/myFolder/B/foo|bar|' -cf archive.tar /root/myFolder/B/foo

It's adding file as it is with same name /root/myFolder/B/foo.


The cause is tar automatically removing leading /:

tar: Removing leading `/' from member names

It seems it gets removed before the regex execution. So you need to remove the leading / in the pattern too:

tar --transform='flags=r;s|root/myFolder/B/foo|bar|' -cf archive.tar /root/myFolder/B/foo

or use -P switch

 -P, --absolute-names
       don't strip leading '/'s from file names

As an alternative you could switch to the files folder with -C, then you don't need to replace the path:

tar --transform='flags=r;s|foo|bar|' -cf archive.tar -C /root/myFolder/B/ foo
| improve this answer | |
  • @nilesh_101 if the answer helped you, please consider accepting it – steeldriver Feb 22 '18 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.