Linux TarBall Create and Extract
Create a TarBall File
tar -zcvf archive-name.tar.gz directory-name
Where,
- -z : Compress archive using gzip program
- -c: Create archive
- -v: Verbose i.e display progress while creating archive
- -f: Archive File name
For example, say you have a directory called /home/jerry/prog and you would like to compress this directory then you can type tar command as follows:
$ tar -zcvf prog-1-jan-2005.tar.gz /home/jerry/prog
Above command will create an archive file called prog-1-jan-2005.tar.gz in current directory. If you wish to restore your archive then you need to use the following command (it will extract all files in current directory):
$ tar -zxvf prog-1-jan-2005.tar.gz
Where,
- -x: Extract files
If you wish to extract files in particular directory, for example in /tmp then you need to use the following command:
$ tar -zxvf prog-1-jan-2005.tar.gz -C /tmp
$ cd /tmp
$ ls -
A note about non gnu/tar command
The above syntax use GNU tar command for compressing and uncompressing tar files. If your system does not use GNU tar, you can still create a compressed tar file, via the following syntax:
tar -cvf - file1 file2 dir3 | gzip > archive.tar.gz
Extract or Unpack a TarBall File
To unpack or extract a tar file, type:
tar -xvf file.tar |
To save disk space and bandwidth over the network all files are saved using compression program such as gzip or bzip2. To extract / unpack a .tar.gz (gzip) file, enter (note -z option):
tar -xzvf file.tar.gz |
To extract / unpack a .tar.bz2 (bzip2) file, enter (note -j option):
tar -xjvf file.tar.bz2 |
Where,
- -x : Extract a tar ball.
- -v : Verbose output or show progress while extracting files.
- -f : Specify an archive or a tarball filename.
- -j : Decompress and extract the contents of the compressed archive created by bzip2 program (tar.bz2 extension).
- -z : Decompress and extract the contents of the compressed archive created by gzip program (tar.gz extension).
How Do I Extract A Single File Called foo.txt?
To extract a single file called foo.txt, enter:
tar -xvf file.tar foo.txt tar -xzvf file.tar.gz foo.txt tar -xjvf file.tar.bz2 foo.txt |
You can also specify path such as etc/resolv.conf, enter:
tar -xvf file.tar etc/resolv.conf tar -xzvf file.tar.gz etc/resolv.conf tar -xjvf file.tar.bz2 etc/resolv.conf |
How Do I Extract a Single Directory Called etc?
To extract a single directory called etc, enter:
tar -xvf file.tar etc tar -xzvf file.tar.gz etc tar -xjvf file.tar.bz2 etc |
Sample outputs:
etc/ etc/pulse/ etc/pulse/default.pa etc/pulse/client.conf etc/pulse/daemon.conf etc/pulse/system.pa etc/xml/ etc/xml/docbook-xml.xml.old etc/xml/xml-core.xml etc/xml/catalog etc/xml/catalog.old etc/xml/docbook-xml.xml etc/xml/rarian-compat.xml etc/xml/sgml-data.xml etc/xml/xml-core.xml.old etc/xml/sgml-data.xml.old etc/mail.rc etc/Wireless/ etc/Wireless/RT2870STA/ etc/Wireless/RT2870STA/RT2870STA.dat etc/logrotate.conf etc/compizconfig/ etc/compizconfig/config ..... ... .... etc/python/ etc/python/debian_config etc/ConsoleKit/ etc/ConsoleKit/seats.d/ etc/ConsoleKit/seats.d/00-primary.seat etc/ConsoleKit/run-session.d/ etc/ConsoleKit/run-seat.d/ etc/opt/