Linux Command Line: tar
Tar stands for “tape archive” and was created back when tape was the defacto-standard means of archiving data. The tar utility came about in 1988 for POSIX.1-1988. Tar’s original intention was simple sequential access devices in raw form, so it was originally used by a system, not by a user. The more modern iteration of tar is a user application that can merge a collection of files into one larger file. Tar retains file permissions and can do minimal compression.
One of the most wide-spread uses of tar is with source code installations. Many times when you download a package intended to be installed, that package will be archived with tar. If you see a file ending in .tar or .tgz, you know it’s has been rolled together with tar.
Like all other command line tools, tar is used from within a terminal window (such as aterm, gnome-terminal, or konsole). The basic format of the command is:
tar OPTIONS filename
The most useful options are:
c - This is used for creating an archive.
r - This allows you to add a file to the end of an archive.
t - This will list the contents of an archive.
x - This extracts an archive.
p - Preserve file permissions while extracting.
z - Filter the archive through gzip (for compression).
v - Verbosly list files that are processed.
f - Use the file following the options.
First we are going to look at unarchiving a file and then we will look at archiving a file. The example we will use is sourcefile.tgz.
To unpack the sourcefile.tgz archive use the following options:
x - to extract
v - to give verbose output
z - to run tar through gzip
f - to indicate the file we are working with follows the options
The z option must be used in the above case because, as you can see, there is a “z” in the file extension which indicates it is a compressed file. The final command will look like:
tar xvzf sourcefile.tgz
If the sourcefile.tgz archive has been packed correctly there will now be a newly created directory (within the directory tar was run in) called sourcefile_._
Let’s go the opposite direction now. You have sourcefile and you want to package it up for use. For this use the following options:
c - to create the new archive
f - to indicate the file to be used
z - to compress the file with gzip
The new command will look like:
tar cfz sourcefile.tgz sourcefile
Notice for archival creation you have to specify the name of the newly created archive. You can actually name this anything you like, but it is good practice to retain continuity.
What has been covered is truly the meat and potatoes of modern tar usage. From this article you can pack and unpack files. But don’t limit yourself to source files for installation. Remember, tar was created as an archival tool so it shines as a backup utility. With a combination of tar and a few other simple commands (and the help of cron) you can create a quick and dirty automated backup system.
This post is part of the series: Simplify Linux application installation and archiving
If you are new to Linux than you might be baffled as to how to handle two important tasks: Application installation and directory archiving. Never fear, Bright Hub is hear. In this series of articles you will learn how to master the tools to help you tackle these tasks.