This subchapter looks at tar, a UNIX (and Linux) command.
This command is used to archive directories and extract directories from archives. The tar archives can be used for back-up storage and for transfer to other machines.
tar files are often used to distribute open source programs.
tar is short for Tape ARchive.
Warning: tar does not work the exact same on all versions of UNIX and Linux. Test and confirm local variations before relying on scripts moved from one platform to another.
Creating a new tar archive:
$ tar cvf archivename.tar dirname/
The c indicates create a new archive, the v indicates verbosely list file names, and the f indicates that the following is the archive file name. The v option may be left out to avoid a long list.
Creating a new gzipped tar archive:
$ tar cvzf archivename.tar.gz dirname/
$ tar cvzf archivename.tgz dirname/
The z indicates the use of gzip compression. The file extensions .tgz and .tar.gz mean the same thing and are interchangeable.
Creating a new bzip2 tar archive:
$ tar cvzj archivename.tar.bz2 dirname/
$ tar cvzj archivename.tbz dirname/
The j indicates the use of bzip2 compression. The file extensions .tbz and .tar.bz2 mean the same thing and are interchangeable.
Bzip2 takes longer to compress and decompress than gzip, but also produces a smaller compressed file.
Extracting from an existing tar archive:
$ tar xvf archivename.tar
The x indicates extract files from an archive, the v indicates verbosely list file names, and the f indicates that the following is the archive file name. The v option may be left out to avoid a long list.
Extracting from a gzipped tar archive:
$ tar xvfz archivename.tgz
$ tar xvfz archivename.tar.gz
The z option indicates uncompress a gzip tar archive.
Extracting from a bzipped tar archive:
$ tar xvfj archivename.tbz
$ tar xvfj archivename.tar.bz2
The j option indicates uncompress a bzip2 tar archive.
It is possible to tar a set of directories (and their files and them pipe the tar to an extraction at a new location, making an exact copy of an entire set of directories (including special files) in a new location:
$ tar -c "$srcdir" | tar -C "$destdir" -xv
A malicious tar archive could overwrite existing files. The use of absolute paths can spread files anywhere, including overwriting existing files (even system files) anywhere. The use of parent directory references can also be used to overwrite existing files.
One can examine the tar archive without actually extracting from it.
The bsdtar program (the default tar on Mac OS X v10.6 or later) refuses to follow either absolute path or parent-directory references.
Viewing an existing tar archive (without extracting) with any of the following:
$ tar tvf archivename.tar
$ tar -tf archivename.tar
$ tar tf archivename.tar
$ tar --list --file=archivename.tar
Viewing an existing gzipped tar archive (without extracting) with any of the following:
$ tar tvfz archivename.tar.gz
$ tar tvfz archivename.tgz
Viewing an existing bzipped tar archive (without extracting) with any of the following:
$ tar tvfj archivename.tar.bz2
$ tar tvfj archivename.tbz
You can pipe the output of a tar file to the less command.
You can directly use the less command to open a tar archive. Less will show you the names of the files, the file sizes, the file permissions, the owner, and the group (the equivalent of ls -l).
$ less archivename.tar
extracting a single file
You can extract a specific single file from a large tar archive.
$ tar xvf archivefile.tar /complete/path/to/file
You can extract a specific compressed file from a large tar archive.
$ tar xvf archivefile.tar.gz /complete/path/to/file
$ tar xvf archivefile.tar.bz2 /complete/path/to/file
extracting a single directory
You can extract a specific single directory from a large tar archive.
$ tar xvf archivefile.tar /complete/path/to/dir/
You can extract a specific compressed directory from a large tar archive.
$ tar xvf archivefile.tar.gz /complete/path/to/dir/
$ tar xvf archivefile.tar.bz2 /complete/path/to/dir/
extracting a few directories
You can extract specific directories from a large tar archive by giving the name of each of the directories.
$ tar xvf archivefile.tar /complete/path/to/dir1/ /complete/path/to/dir2/
You can extract specific compressed directories from a large tar archive.
$ tar xvf archivefile.tar.gz /complete/path/to/dir1/ /complete/path/to/dir2/
$ tar xvf archivefile.tar.bz2 /complete/path/to/dir1/ /complete/path/to/dir2/
extracting a group of files
You can extract a group of files from a large tar archive by using globbing. Specify a pattern (the following example finds all .php files):
$ tar xvf archivefile.tar --wildcards '*.php'
You may use the gzip and bzip2 options as well.
adding to an existing archive
You can add an additional file to an existing tar archive with the option -r:
$ tar rvf archivefile.tar newfile
You can add an additional directory to an existing tar archive:
$ tar rvf archivefile.tar newdir/
This does not work for adding a file or directory to a compressed archive. You will get the following error message instead:
tar: Cannot update compressed archives
You can verify a new archive file while creating is by using the -W option:
$ tar cvfW archivefile.tar dir/
If you see something similar to the following output, something has changed:
1/filename: Mod time differs
1/filename: Size differs
If you just see the Verify line by itself, the file or directory is fine:
The -W option does not work for compressed files.
For gzip compressed files, find the difference between the gzip archive and the file system:
$ tar dfz 1/filename.tgz
For bzip2 compressed files, find the difference between the bzip2 archive and the file system:
$ tar dfj 1/filename.tbz
On November 8, 2010, Ramesh Natarajan named this the number one (1) most frequently used UNIX/Linux command at this web page 50 Most Frequently Used UNIX / Linux Commands (With Examples).
In June 2009, Ken Milberg named this command as one of the Top 50 universal UNIX commands at this web page Top 50 Universal INIX commands. Note that this web page requires agreeing to be spammed before you can read it.
comments, suggestions, corrections, criticisms
free music player coding example
Coding example: I am making heavily documented and explained open source code for a method to play music for free almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).
View music player in action: www.musicinpublic.com/.
Create your own copy from the original source code/ (presented for learning programming).
Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.
Names and logos of various OSs are trademarks of their respective owners.