Tagged as: linux

Tar can be difficult to use if you don't know all the options, learn the basics and you will be happy!

Tar is a utility for compressing files into an archive. It stands for Tape ARchive. Tar archives will normally have the file extension .tar. Most tar archives are then compressed with gzip which then normally has the extension .tar.gz. Tar can natively process these files and also compress directly to them.


Basic Commands

Tar commands follow the basic format of

tar option(s) [archivename] [filename(s)]

Archive a set of files.

tar -cvzf archivename.tar.gz file1toarchive file2toarchive ...

This command will archive file1toarchive and file2toarchive into the file archivename.tar. The -cvzf means:

  1. (c) Create an archive.
  2. (v) Produce detailed output of progress. (verbose mode) This is optional and it is recommended to omit this option in shell scripts.
  3. (z) Use gzip to reduce the file size of the output.
  4. (f) The filename is the next argument.

Extract files from an archive.

To unzip a files created using the medhod above and .tar.gz files you find on the internet, use the following command:

tar -xvzf archivename.tar.gz

This command will extract the files from archivename.tar.gz into the current directory. The -xvzf means:

  1. (x) Extract an archive.
  2. (v) Produce detailed output of progress. (verbose mode) If you are using tar in a shell script, you should probably omit this.
  3. (z) Use gzip to extract the archive. This only works for archives that have been passed through gzip already, for example archives created with the z option.
  4. (f) The filename is the next argument.

Back to Top

Modifying Existing Archives

If you forgot to include a file in your archive or need to delete or update one already inside, don't worry about having to delete and recreate the archive - tar offers features to allow you to edit existing archives!

Add a new file to an existing archive.

tar -rf archivename.tar.gz filename

This will add the file called filename to archivename.tar.gz._

Remove a file from an existing archive.

tar -f archivename.tar.gz --delete filename

This will remove filename from archivename.tar.gz.

List an archive.

tar -ft archivename.tar.gz

This will create a list of the files in archivename.tar.gz.

Modify an existing file.

tar -uf archivename.tar.gz filename

This will change filename inside archivename.tar.gz to the filename outside of it.

Back to Top

Advanced Usage

Tar has many more advanced features that can be used for extra compression, better operation in scripts and other things.

Compress archive with bzip2.

As well as plain tar files or even gzipped archives, tar supports passing archives through bzip2 which produces even better compression than gzip. To make a .tar.bz2 archive, do the following:

tar -cvjf archivename.tar.bz2 filetoarchive

And to decompress an archive created in this way:

tar -xvjf archivename.tar.bz2

The -j option is the one that enables bzip2 compression/decompression.

Excluding files.

It is useful to be able to exclude files from being archived by tar because it can save command line length and greatly simplify the setup of automated scripts and backup solutions. For example, if you wanted to backup your whole system into a tarfile located at /backups/backup.tar.gz then you would need to exclude /backups from the archive operation because if you didn't, tar would be stuck endlessly archiving what it had already archived. This could be achieved as such:

tar -cvzf /backups/backup.tar.gz --exclude='/backups/' /

You can have more than one location to exclude:

tar -cvzf /backups/backup.tar.gz --exclude='/backups/' --exclude='/bin/' /

You can also store the files you want to exclude from being archived in a file. This would be useful for a script, so that you don't have such a long command to run and also so that you can change the files to be excluded independently from the script. If you created the file excludes in the same directory you are running tar in, and put inside it the following items:


When you ran:

tar -cvzf archive.tar.gz * -X excludes

It would archive everything in the current directory into archive.tar.gz apart from the files specified in excludes.

Back to Top