Back to the basics, part 2: tar

No Gravatar

One of the most common problems with tar is ending up with unexpected contents of directory structures in your tar archive (sometimes called a tarball).  I’ll show you how to create those nice, neat tarballs containing just the directory you wanted without the unnecessary preceding directory structure.

Problem 1: Unexpected directory trees

First an example to illustrate a few problems.

tar cf /home/chris/tarball.tar /var/www/htdocs

What happens? You end up with a directory structure that creates the following:

var (empty except for www) var/www (empty ecept for htdocs) var/www/htdocs (contains all the files you expected it to)

when really what I intended was htdocs in the root of the tarball without any preceding directory structure.

Say you extracted this to your home directory, /home/chris, with the commands <code>cd /home/chris
tar xf /home/chris/tarball.tar

You’d end up with

/home/chris/var (empty, except for www) /home/chris/var/www (empty, except for htdocs) /home/chris/var/www/htdocs (containing all the files you expected it to)

The better way? Change directory to the one that contains the first directory level that you want in your tarball. For example, if you wanted htdocs to be the first directory in your tarball:

cd /var/www tar cf /home/chris/tarball.tar ./htdocs

Success! Now your tarball was created in the directory you wanted (/home/chris/tarball.tar) and only contains htdocs and the files inside htdocs! Now when you extract it

cd /home/chris
tar xf tarball.tar

you end up with just

/home/chris/htdocs (and all the files inside htdocs)

Problem 2: Contents of a directory without hidden files

Mind the slashes! If you add a trailing slash to the end of your create command, it will add all files and directories to your tarball except the hidden files or directories (those starting with a dot, .htaccess for example).

tar cf /home/chris/tarball.tar /var/www/htdocs/       #hidden files and directories Will not be included in the tarball
tar cf /home/chris/tarball.tar /var/www/htdocs         #hidden files and directories WILL be included in the tarball

Problem 3: Where did it extract to/What did I extract?

Another common problem is confusing arguments on extraction commands. Any arguments following the path to the tarball is used to determine which files you want to extract from the tarball, it’s not used to determine where to put the extracted contents of your archive!

tar xf /home/chris/tarball.tar /var/www/htdocs/      #it will try to extract /var/www/htdocs from the archive and place it in the #current directory. Since /var/www/htdocs probably doesn't exist (it would #be ./htdocs inside the archive if you created it like above), it will probably #fail for you cd /var/www/ tar xf /home/chris/tarball.tar                                           #it will extract the complete contents of your tarball into the current directory # which is currently /var/www/ # Success!

I hope this helps clear up a few common points of confusion with tar. See the man entry for tar for more details about some cool things you can do with tar, like excluding files (just type “man tar”).

Happy Archiving!

Leave a Reply