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

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

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

├── 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?


tar has a --transform option. You could call

tar --transform "s/repo/repo-v1.0.0/" -cvzf repo.tar.gz repo/

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.

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.