Create a tar archive of the current directory without preceding period

tar

When I do the following:

tar czvf ../myarchive.tar.gz ./

I get a single (annoying) root folder in my tar archive:

annoying

How do I remove this awful period when creating the archive?

Best Answer

You don't.

name=${PWD##*/}
cd ..
tar czf "$name.tar.gz" "$name"

(Note: this doesn't work if your shell has current directory symbolic link tracking turned on and the current directory is accessed via a symbolic link.)

Yes, this isn't what you asked, but this is what you should do. Archives that expand a lot of files in the current directory are annoying: it puts the burden of creating a target directory for the file on each person who unpacks the archive, and if they accidentally unpack them in a non-empty directory, it's hard to clean up. Most of the time, an archive should create a single toplevel directory whose name is the base name of the archive.

Related Question