How to use –transform parameter of tar

tar

I tar'd and zip'd the following file:

/opt/abc/xyz/abc.xml

to:

/tmp/backup/abc.xml.tar.gz

I was trying to untar and unzip to /opt/abc/xyz/(same path) directory.

I came across the below command. I executed it as the root user, but didn't find any error or output. I am expecting below command will extract abc.xml from abc.xml.tar.gz and renamed to abc.xml_v9.

tar --force-local --transform='s/abc.xml/abc.xml_v9/' -zxpsf /tmp/backup/abc.xml.tar.gz -C /

output should be

/opt/abc/xyz/abc.xml_v9

man page of tar says below information

File name transformations:      

       --transform=EXPRESSION, --xform=EXPRESSION
              use sed replace EXPRESSION to transform file names

              File name matching options (affect both exclude and include patterns):

Can someone help me understand the above command with some example?

Best Answer

Using your example files source /tmp/backup/abc.xml.tar.gz and destination /opt/abc/xyz/abc.xml_v9. you can use thsi command:

tar --transform='flags=r;s|abc.xml|abc.xml_v9|' -xvf abc.xml.tar.gz -C /opt

Or

tar --transform='flags=r;s/abc.xml/abc.xml_v9/' -xvf abc.xml.tar.gz -C /opt

And you will get

/opt/abc/xyz/abc.xml_v9

See:

https://stackoverflow.com/questions/21790843/how-to-rename-files-you-put-into-a-tar-archive-using-linux-tar

https://www.gnu.org/software/tar/manual/html_section/tar_51.html#transform

Related Question