Archiving Directories and Files with tar

Still hanging in there the tar command remains the de facto standard for archiving files and directories in UNIX and UNIX-like operating systems. Here are some tar basics from Easy Linux Commands

There are several reasons you may want to create an archive of a file or directory. Here are some of the most common ones:

  • Archive and compress unused directories to conserve disk space
  • Create an archive of a directory and files before an upgrade allowing you to restore the original contents if there is a problem
  • Archive a directory before making any major changes
  • Create an archive to move several files and folders to another system as one
  • Create an archive as a means of software distribution

One of the most useful utilities for archiving a set of files and directories is tar. The name tar is short for Tape ARchiver because tar was originally written to write data to a tape backup device.

The following is a basic example of archiving a directory into a tar archive file and also extracting the archive into its original structure.

$ tar -cvf examples.tar examples
$ rm –r examples
$ ls
$ tar –xvf examples.tar
$ ls
examples examples.tar

In this example we have demonstrated the two most common uses of tar. The first tar command combines the –c (create) option to create a new archive, the –v (verbose) option to list the files and directories it’s archiving and the –f option to write to a file rather than to tape. Remember that tar was originally written for use with tape drives and it still defaults to tape if you do not use the –f option.

The two arguments for this tar command are the destination file (examples.tar in our example here) and the files which should be added to that file. This can be confusing since most other Linux commands take a source argument before the destination. tar takes them in the order of destination then source so you can list multiple source files to be archived into a single file. Also not that we have to specify the file extension (.tar) if we want our new archive to have an extension. You can name a tar archive (typically called a tarfile or tarball) anything you want, but the .tar extension is a widely accepted convention.

In the second tar command the –v and –f options have the same result and the –x (extract) option tells tar that we want to extract the contents of a tar file rather than create one. We then give tar the name of the archive to be extracted and it goes to work restoring our files.

A Warning about Relative and Absolute Paths in tar

As with other commands tar can be used with either relative or absolute paths. When specifying the tarfile to be created or extracted there is little difference between these two methods; however, if you use an absolute path when listing files to be archived you might get a surprise when you extract them!

If an absolute path is used to tell tar which files to archive, like with the command below the archive will record the absolute path and restore those files to that path, no matter where tar is run from or where the tarfile is.

$ tar -cf examples.tar /home/tclark/examples

If an absolute path is not specified on archiving the files will be extracted into the working directory or the appropriate subfolder of the working directory.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!

linux, unix, system administration, sysadmin, tar, tape archive

One thought on “Archiving Directories and Files with tar”

  1. It’s important to note that tar’s do not employ any compression by default, they are simply a collection of files. Add a z to the arguments to zip a tarball during creation, and again to unzip it. This type of file is generally associated with the tgz extension.

Leave a Reply

Your email address will not be published. Required fields are marked *