Introduction to Tar

The following introduction is based on tar, a program that is found in virtually all linux systems. It does not require root privileges and can be run on a virtual machine over a terminal.

Copying a large number of files over ftp or sftp involves a lot of overhead because each file needs to be transferred and recreated separately.

The linux command tar creates archives that condense a large number of files into one. By default, each file is appended to the end of the previous one, with an archive roughly the total size of the original files.

To create an archive of a folder:

$ tar cvf [archive name] [folder 1] [folder 2] ..

The first switch c stands for create, the v stands for verbose output which lets you better see what is going on, and the f stands for filename and f must be the last switch in the list and must be followed by the archive’s name.

The standard file extension for tar files, known as tarballs, is .tar.

To enable compression on a tar archive with gzip, add the z switch. For example:

$ tar czvf backup.tgz public_html

Both the .tgz and the .tar.gz file extensions are used for gzip-compressed tarballs.

To read a preexisting archive, use:

$ tar tvf backup.tgz | less

The t switch stands for list. It is useful to add the | less to the end of this command so that you can scroll up and down the output, and so that it doesn’t crowd up your terminal, but it is not necessary.

You should always examine the contents of a tarball before extracting it to make sure that it contains what you are expecting.

To extract a tarball to the current directory, use:

$ tar xvf backup.tgz

To only extract a certain file or directory from a tarball, use:

$ tar xvf backup.tgz file-name
$ tar xvf backup.tgz folder-name

To extract to a different directory, use:

$ tar xvf backup.tgz -C /tmp

Or combine the previous two options:

$ tar xvf backup.tgz -C /tmp file-name

After you create a tarball, you can send it to any other linux computer and extract it.

It is also possible to tunnel the output of tar over ssh to another server and extract it immediately. Read man tar and man ssh for more information.