20

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?

2
  • This Stack Overflow answer might help you. Commented Jul 11, 2014 at 22:41
  • 4
    Or use --transform/--xform, as suggested by Magnus: tar -cf /path/to/output.tar -C /path/to/input-directory . --xform='s!^\./!!' Commented Jul 11, 2014 at 22:46

3 Answers 3

17

The dot appears because tar creates the path structure the way you give in the arguments. So as you have given . as the source, the archive also is created in the same structure.

You can try this (note: this won't match hidden files):

tar -czvf ../myarchive.tar.gz *

Updating with information from the n.st's comment and zhoux's answer

tar --xform s:'^./':: -czvf ../myarchive.tar.gz ./
3
  • 5
    The * glob won't match hidden files. Commented Jul 11, 2014 at 22:47
  • 1
    :D eh..just a beginer..didnt realize. Commented Jul 11, 2014 at 22:49
  • You could use some more bash globs to include hidden files — have a look over here. Commented Jul 11, 2014 at 22:52
7

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.

1
  • Understood. If I recall correctly, it's just really annoying to have to do an extra step to untar an archive into a specific directory (eg tar xzvf archive.tar.gz && mv archive/* /output vs. unzip archive.zip -d /output) Commented Jul 14, 2014 at 16:45
2

better add ^ before the preceding .

tar --xform s:'^./':: -czvf ../myarchive.tar.gz ./

OR, all "/" in the full pathname inside the archive will be removed unless "/" is the end of the full pathname.

1

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.