Linux tar: only --transform certain files

1

I'm writing a bash script that packages my application into a TAR file. The files aren't arranged on disk how they need to be arranged in the TAR file, so I decided to use the --transform option.

However, it seems that the --transform command is applied globally instead of being applied to the files that are included after.

Example:

tar \
--exclude='webmanager/bin/dev' \
--exclude='webmanager/.config.ini' \
--show-transformed-names \
--create --gzip --verbose \
--file 'output.tgz' \
-C '../../../' \
'include' \
'resources' \
'webmanager' \
'rootconfig.php' \
-C 'webmanager/bin/dev' \
'index.php' \
'README.md' \
--transform='s|^|webmanager/|r' \
'.config.ini'

Basically, this code takes a file from webmanager/bin/dev/.config.ini and places it at webmanager/.config.ini in the TAR file. But I only want the .config.ini file to be transformed. Currently all the contents are being transformed and prefixed with webmanager/. I was hoping this command behaved like -C (change current directory) - where it only affects the inputs after it.

I have looked over the TAR man pages and I can't figure out how to do this properly. Please help!

Bradley Odell

Posted 2016-12-07T00:57:36.217

Reputation: 111

Feel free to write a patch. – Ipor Sircer – 2016-12-07T03:34:42.430

Answers

1

It seems the --transform option works on all the files indeed. By the way, I've found this page about tar that might help you somehow.

If I may, I would suggest you a different approach: lay out the files as you need in a temporary directory (cp command will do), modify your .config.in with sed and than use tar to pack everything. You can create a script of even a Makefile, if you are familiar with make.

Alessandro Dotti Contra

Posted 2016-12-07T00:57:36.217

Reputation: 434

2I recommend making hardlinks (cp -l) instead of copying to save time and disk space. – Kamil Maciorowski – 2016-12-07T16:34:43.197

Yes, that's better. Thanks for pointing that out. – Alessandro Dotti Contra – 2016-12-07T22:17:10.213