Parent directories in tar archives

Jan 3, 2014
Tested on: Linux Mint 13 Maya (based on Ubuntu 12.04 LTS)

It is common to see full directory tree or . (dot) in tar archives. Sometimes, it may be unnecessary hassle to remove all parent directories after extracting or to look for the extracted file which merged with other files in extracted directory. In some situations, it may be desirable to create a tar archive with top directory (with informative name). There has been many discussion about the same. Here is quick summary with an example for future reference.

Test setup

We will create a test directory with some files in it with following commands (outputs are also shown): $ mkdir -p /tmp/path/to/test/dir_archive $ touch /tmp/path/to/test/dir_archive/file{1..10} $ ls /tmp/path/to/test/dir_archive/ file1 file10 file2 file3 file4 file5 file6 file7 file8 file9 $ tar --version tar (GNU tar) 1.26 Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by John Gilmore and Jay Fenlason.

Full parent directory

Following command creates tar archive with full parent directory structure as shown by --list command. $ tar -caf /tmp/test.tar.gz /tmp/path/to/test/dir_archive $ tar --list -af /tmp/test.tar.gz tmp/path/to/test/dir_archive/ tmp/path/to/test/dir_archive/file3 tmp/path/to/test/dir_archive/file10 tmp/path/to/test/dir_archive/file1 tmp/path/to/test/dir_archive/file8 tmp/path/to/test/dir_archive/file4 tmp/path/to/test/dir_archive/file2 tmp/path/to/test/dir_archive/file7 tmp/path/to/test/dir_archive/file5 tmp/path/to/test/dir_archive/file6 tmp/path/to/test/dir_archive/file9

Dot (.) as parent directory

Following command creates tar archive with dot (.) as parent directory. Note that the tar command ends in a dot (.). $ tar --directory /tmp/path/to/test/dir_archive -caf /tmp/test.tar.gz . $ tar --list -af /tmp/test.tar.gz ./ ./file3 ./file10 ./file1 ./file8 ./file4 ./file2 ./file7 ./file5 ./file6 ./file9

Directory name as parent directory

The trick to get dir_archive as parent directory in tar archive is to use --directory to change directory to one level up as shown below. Note location of dir_archive in the command. $ tar --directory /tmp/path/to/test -caf /tmp/test.tar.gz dir_archive $ tar --list -af /tmp/test.tar.gz dir_archive/ dir_archive/file3 dir_archive/file10 dir_archive/file1 dir_archive/file8 dir_archive/file4 dir_archive/file2 dir_archive/file7 dir_archive/file5 dir_archive/file6 dir_archive/file9
Some more notes about tar command:
  • Above commands can be combined with used along with exclude directive to by using --exclude='dir_archive/dir_exclude' after the --directory option.
  • Do not mix absolute and relative path in tar command for pathname and exclude option. --directory is an exception and can be absolute path while others paths can be defined relative to it.