1

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

/bin/tar cfz /usr/local/backups/mybackup-${DATENAME}.tar.gz 
                 /usr/local/backups/db-${DATENAME}.sql.gz  
                 /usr/local/tomcat/webapps/MyApp/myapp_folder 
                 /usr/local/tomcat/myapp.properties

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)?

TheVillageIdiot
  • 161
  • 1
  • 9
  • 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

1 Answers1

3

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 
                 /usr/local/backups/db-${DATENAME}.sql.gz  
                 /usr/local/tomcat/webapps/MyApp/myapp_folder 
                 /usr/local/tomcat/myapp.properties

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

toppledwagon
  • 4,245
  • 25
  • 15