What is Tar?
Tar refers to a tape archiving file format and the program that creates them. Tar stands for tape archive. It combines multiple files together so that they can be used for programs, applications, and critical system files. While Tar does not directly install files to a user’s hard drive, it is responsible for compiling the files together so that they can be transferred from one computer to another while preserving important information such as directory structures, user permissions, and dates. Tar can be used on its own to archive and/or distribute system files, but is often used in conjunction with a compression utility in order to minimize the size of the total Tar file. Users should note that Tar files are commonly referred to as “tarballs” and usually have the file extension “.tar.”
How Tar Works
Tar files copy data from multiple files and assemble them into one file so that it may be archived, distributed, and installed on a hard drive. They differ from other types of archive files because they are compressed as a single file rather than as multiple pieces. Each file within a Tar file is assigned a file header that an ASCII text file represents and contains information about what the file is, where it should be placed, and how it contributes to the Tar file itself.
Tar files assemble files for programs, specific client-based applications, and server-side databases. They are also used for tape archives, allowing computers to save data to cassette tapes and VHS tapes, although this purpose is obsolete due to tapes’ unpopularity since the invention of technology such as CDs, DVDs, and more recently, Blue-ray discs.
Because Tar files are archived as a single file instead of multiple pieces, and there is no index to serve as a “table of contents,” it can be difficult to find a specific file within a Tar file without extracting the entire archive. This dramatically limits RAM use (Random Access Memory) and, in some cases, makes it completely impossible.
If standard practices are not followed when making Tar files, the result is a so-called “tarbomb” or a Tar file that extracts files into the current working directory instead of its own directory. This possibly overwrites some existing files and compels the user to discern which files belonged to the archive and those that were already there.
To create a Tar archive from a given directory:
tar cvf archive_name.tar directory_name/
c - create an archive
v – show a detailed (verbose) list of files being archived
f – with this file name (archive_name.tar, as specified afterwards)
To create a Tar archive compressed with a gzip compression algorithm, creating a tar.gz file:
tar cvzf archive_name.tar.gz directory_name/
z – zip it, or compress it with gzip
One can also use j instead of z to use bzip2 algorithm, which gives even stronger compression, creating a tar.bz2 file:
tar cvjf archive_name.tar.bz2 directory_name/
To extract simply change c to z and apply it to the Tar file. For example, to extract just the tar archive enter:
tar xvf archive_name.tar
To extract the tar.gz file:
tar xvzf archive_name.tar.gz
And to extract the tar.bz2:
tar xvjf archive_name.tar.bz2
It is also possible to see what is inside of an archive without extracting it by just using t instead of x. So to see what is inside of an archive_name.tar.gz enter:
tar tvzf archive_name.tar.gz
Once a list of what is inside is displayed, just extract a specific file or folder from an archive by simply adding a correct path to it inside of the Tar file (as listed). Users should still use x to extract.
tar xvzf archive_name.tar.gz /path/to/dir (to extract a specific directory within the archive) or
tar xvzf archive_name.tar.gz /path/to/file (to extract a specific file) or
tar xvzf archive_name.tar.gz /path/to/file /path/to/file2 (to extract multiple files).
In order to get all of the advanced information on how to use a tar command, enter
man tar, which will display its manual page. This should be an option on most Linux and Unix systems.