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:
Verify 1/filename 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:
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.
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).
Work on this project is very slow because I am homeless. I am available for work if someone can provide an indoor place to work in Costa Mesa, California, electricity, internet connections, a flat raised working surface (such as a table or desk), a sitting device (such as a chair or stool), and a fully functional reasonably modern used computer. Im already homeless, so you dont need to pay me (and I understand how much business people hate the minimum wage law). Just give me a chance to work.
Building a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming.
If you like the idea of this project, then please donate some money.
send donations to: Milo
PO Box 1361
Tustin, California 92781
At the time I am homeless. This greatly interferes with my ability to create this project, which can help nearly 20 million U.S. college students and more than 150 million students world-wide. I am looking for 30 rich people or corporations willing to donate $10 a month to my church so that the church can provide a place indoors for me to continue work. If you want to donate, please see help project. Thanks much.
Supporting the entire project:
If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church)
UNIX used as a generic term unless specifically used as a trademark (such as in the phrase UNIX certified). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.
Names and logos of various OSs are trademarks of their respective owners.