I am creating a tar.gz file containing some files and folders as follows:

/bin/tar cfz /usr/local/backups/mybackup-${DATENAME}.tar.gz 

But the paths are stored as usr/local that is without /. tar also gives message to remove first / but file is created.

Now when I transfer this file to some other system and want to uncompress it:

tar -zxvf mybackup-2011-APR-18.tar.gz

It creates usr directory in current directory.

How can I compress and uncompress the archive and get my files back into original folders (in /usr tree instead of ./usr)?

  • You should consider using cpio instead. The advantage of cpio is it takes the file list from stdin. That way you can customize the file list when creating the archive (for example with find) to control the base directory. – Phil Hollenback Apr 17 '11 at 21:23

This will do it:

(cd / && tar -zxvf mybackup-2011-APR-18.tar.gz )

Alternatively you could create the archive with -P:

/bin/tar -P cfz /usr/local/backups/mybackup-${DATENAME}.tar.gz 

This will not remove the initial / from the filenames in the archive.

  • thanks I think -P is a better option – TheVillageIdiot Apr 17 '11 at 20:59
  • got this error: /bin/tar: You must specify one of the -Acdtrux' options` – TheVillageIdiot Apr 17 '11 at 21:37
  • @TheVillageIdiot - it disables absolute paths for a reason – symcbean Apr 17 '11 at 22:40
  • tar -P -czf worked for me (note the flipped z and f). @symcbean I totally agree, creating tar files with absolute paths is a bad idea. But that's just my opinion. If it were me, I'd use the first command on the untar.. – toppledwagon Apr 18 '11 at 1:19

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.