Tar

From LQWiki
Jump to navigation Jump to search

tar is short for "tape archive", although its use with magnetic tape backups is primarily of historical concern. tar is both a command and a file format used by the command. It is used to collect several files into one archive, which can be more easily managed or compressed as a unit, often with gzip or bzip2.

Using tar

Tar has several uses, but the most commonly used is to extract or to create tar archive (known as "tarballs")

.tar

To unpack a standard .tar file, use the command

tar -xvf filename

The Command options of tar:

  • x: decompress something
  • t: view contents
  • v: verbose mode
  • f: input from a file

If the f flag is omitted, the command will read from stdin. This allows several interesting things: for example, piping output in tar format to tar, to have it untarred.

(Note that, unlike most commands, the '-' before the options may be omitted.)

To create a tarball, you can use

tar cf tarballname.tar files

which can be compressed afterwards.

.tar.gz

Decompressing tarballs that have been gzipped can be done in two ways: first decompressing then extracting, by

gunzip filename.tar.gz && tar xf filename.tar

Alternatively, some versions of tar support inbuilt decompression of gzipped tar files. This requires the z option, transforming the command to:

tar -xvzf filename

tar.bz2

Decompressing Bzip2 tar files can be done in the same manner, bunzip2 first and then tar, but some versions of tar allow you to use the j option:

tar -xvjf filename

If this option is not available to you, the same effect can be achieved by the following:

bunzip2 filename | tar -xvf -

where, as mentioned above, the output of bunzip2 is piped to tar, which is directed to read from stdin with the final '-'.

View contents of an archive

If you just want to view the contents of a tar file instead of extracting it:

tar -tf filename

You can filter it through gzip or bunzip2 respectively:

tar -tzf filename
tar -tjf filename

Using tar to copy files

In addition to archiving files, tar can also be used to copy a group of files and directories using a pipe. To move all the files (including hidden ones) in /olddir to /newdir, for example, the syntax is

tar -c . | (cd /newdir; tar -xv)

Don't worry about the -v option with the pipe; the -v option writes to the stderr stream and will not interfere with the pipe.

Using tar to transmit files

In addition to copying files, tar can also be used to transmit a group of files and directories using a pipe and ssh. To transmit all the files from your directory to the folder /archive on 192.168.0.80, for example, the syntax is

tar cv -f- . | ssh root@192.168.0.80 "(mkdir /archive; cd /archive; tar -xv)"

See also: Cloning

Excluding Directories

Here's an example of including everything but opt and home directories.

tar --exclude=home --exclude=opt --exclude=srv -cvf slash.tar /

This excludes all opt and home directories, not just the ones under the current working directory.

Tar A Mounted Directory With No Leading Paths

tar -cvpjf /path/to/file.tar.bz2 ./*

This will tar any mounted directory and it will contain no nested path folders. This is helpful when you wish to back-up a mounted partition to a disk or another drive, and have the option of extracting it again without the usual leading paths.

See also

External links