1

I'm trying to create tar archive and rename root dir in archive. E.g. I have these files:

$ tree ./repo
./repo
├── 1
├── 2
└── 3
0 directories, 3 files

I want to create an archive repo-v1.0.0.tar.gz with these file structure:

./repo-v1.0.0
├── 1
├── 2
└── 3

Now I'm using ugly workaround:

mv "${repo}" ${repo}-${version}"
tar -cvzf "${repo}-${version}.tar.gz" "${repo}-${version}"
mv "${repo}-${version}" "${repo}"

Is it possible to rename root dir in the archive using tar command itself?

2

tar has a --transform option. You could call

tar --transform "s/repo/repo-v1.0.0/" -cvzf repo.tar.gz repo/
| improve this answer | |
1

Tar will not rename, as far as I know. But you can do a simpler workaround using a symlink:

ln -s "${repo}" ${repo}-${version}"
tar --dereference -cvzf "${repo}-${version}.tar.gz" "${repo}-${version}"

You can then decide to keep the symlink or remove it.

| improve this answer | |

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.