At some point, in your Linux sysadmin days, you are going to come across an archive (compressed or not) that will have been created by tar. If you’re unfamiliar with this command, you will need to be. Why? Not only is tar included with Linux (by default), it is also one of the more command archiving tools you will find. If you’re going to install an application from source, chances are, that source has been rolled together with tar.
Tar stands for tape archive. That’s right, it’s a bit long in the tooth, as it was originally created to work with tape backups. Even still, it is widely used. With it, you can take a collection of files and folders and rip them into a single, compressed archive (called a tarball) or make use of tar to create your own backup scripts. The options are seemingly limitless.
I want to walk you through a few examples of how to work with the tar command; we’ll use it to create and extract different types of archives. Ready to type?
The structure of the tar command looks like:
tar [OPTIONS] [FILENAME ] [PATHNAME]
In other words, you would use tar like so:
tar options filename.tar /path/to/archive
There are quite a lot of options available to you; the options you choose will dictate what you do with the file. The most common options are:
- c – create a new archive
- f – use archive file
- v – show verbose output
- z – compress the archive
- j – work with bz2
- t – list the contents of an archive
- x – extract an archive
Some of the above options cannot be used alone (such as t) and there are quite a number of other options available. However, the above short list will give you everything you need to get started.
Speaking of which…
Creating an archive
The first thing we want to do is create an archive. Say, you have the folder TEST that contains the files test, test2, test3, test4, and test5. You want to be able to roll that folder into a single file called TEST.tar. To that, you would issue the command:
tar cfv TEST.tar TEST
The above command would have to be issued in the directory containing the TEST folder and would result in the TEST.tar file.
View the contents of an archive
Now that we’ve created an archive, we might want to take a look inside that archive to see what’s what. For this, you would use the t option. However, if you simply issue the command tar t TEST.tar, you would be returned an error. In order to make use of the t option, you must use it with (at minimum) the f option. So the new command looks like:
tar tf TEST.tar
The output of the above command would look like that shown in Figure A.
You could add the v option, to get more information on the tar, like so:
tar tvf TEST.tar
The above command will give you file permissions, ownership, size, and create date of each file (Figure B).
Creating a compressed archive
What if you want to add compression to your archive? This is done with the addition of an option and changing the resulting file name. So to compress our example, we’d issue the command:
tar cvzf TEST.tar.gz TEST
The order in which you add the options here is important. The f option must come last, as what follows it will be the name of the file to work on. The result of the command will be a compressed archive with the extension .tar.gz. You can view the contents of the compressed file in the same way you viewed the uncompressed file (just add the .gz to the end of the file name).
You can also create a compressed file using bz2 compression with the command:
tar cvjf TEST.tar.bz2 TEST
Extracting an archive
Let’s extract the archives we just created. This is done using the x option like so:
tar xvf TEST.tar
But what if that archive is compressed? The same command will work (with the addition of the .gz extension):
tar xvf TEST.tar.gz
To extract our compressed bz2 file, the command would be the same:
tar xvf TEST.tar.bz2
Extract a single file from an archive
You know how to list the contents of a tar file now. What if there’s a single file in that tarball you want to pull out? You can do that. Using our example, we’ll extract the file test from TEST.tar. This would be done with the following command:
tar xf TEST.tar TEST/test
The only caveat to the above command is that it will create the TEST folder, with the single file test inside. As far as I know, there is no way around this. You can also extract multiple files from the same archive like so:
tar xf TEST.tar TEST/test TEST/test3 TEST/test5
Learn even more
This is a fairly basic introduction to tar. If you want to learn more, make sure to issue the command man tar and read through the man page, where you can learn about all of the options and other ways to make use of tar.