Tar Command in Linux: Binding Multiple Files Into a Single Unit

tar command in linuxLinux, unlike Windows, is mostly a command line based operating system. This means you have to explicitly type the commands out at a prompt, unlike the Windows drag and drop. While this does give a much finer level of control to the Linux user, it can take a while to master the Linux command line. In this tutorial we will walk you through the tar command usage. We assume a basic level of familiarity with Linux. If you’re new to Linux, we recommend you first try out this crash course.

The Tar command in Linux (and UNIX) systems can be used for several purposes, the most common of which is to combine several files into a single file. The tar command stands for “tape archiving”, which means the storing of one or more files onto a magnetic tape.

Various Tar Commands

There are several variations of the tar command on Linux. You can create tar files, store, separate, compress and decompress them. A tar file is often confused for a compressed file, but in reality it’s just a collection of uncompressed files. A compressed tar file is known as a tarball (.tgz file), which is a type of file you can create with the tar command.

Let’s take a look at some of the tar commands you can use in Linux. Please note that this is not an exhaustive list, but a collection of the most basic tar commands you’ll need while working with tar files.

Creating a tar file

You can create a tar file from files you have placed inside a directory location. You create a tar file, use the following command:

$ tar cvf name_of_archive.tar dirname/

Let’s say you wanted to create a tar file named package.tar for all the files inside your “home/john” directory. For this, you would use the following command:

$ tar cvf package.tar  home/john

This will place all the files inside the home/john inside a new tar file called package.tar. The package.tar file will, in turn, be placed in the john directory.

What does the “cvf” we typed mean? The “c” tells Linux to create a new archive, “v” to verbosely display files that have been processed and “f” stands for the file name.

Creating a tarball (gzipped) file

You can create a compressed tar file just by including the letter “z” in “cvf”. The syntax to create a tarball is:

$ tar cvzf name_of_archive.tar.gz dirname/

To create a gzipped package.tgz file in your “home/john” directory, just type in the following command:

$ tar cvzf package.tar.gz home/john

Creating a smaller tarball (bzipped) file

You can create a bzipped file instead of a gzipped file by changing your command just a little. What’s the difference between a .bz2 and a .gz file? The former is smaller, but it takes more system resources to create and is consequently slower to process. The syntax to create a bzipped file is:

$ tar cvzf name_of_archive.tar.bz2 dirname/

Separating a tar file into component files

You can separate tar files into its component files by using the “x” command. The syntax for this would be as follows:

$ tar xvf name_of_archive.tar -c dirname/

You don’t need to specify the directory name while separating a tar file into its component files. You can specify the directory name if you want to extract the tar file to directory other than the current one. The following command will extract the tar file to the directory you’ve currently navigated to:

$ tar xvf package.tar

The new “x” we’ve added instructs Linux to extract the tar file.

Separating a gzipped tarball into component files

You can separate a gzipped tarball into its component files through the following syntax:

$ tar xvf name_of_archive.tar.gz -c dirname/

Again, the –c dirname option is optional. You can extract the files to a different directory or to the directory you currently are in.

Seperating a bzipped tarball into component files

Just type in the following to separate a bzipped tarball into its component files:

$ tar xvf name_of_archive.tar.bz2 -c dirname/

Listing the contents of a tar file

Sometimes you may want to know what a tar file contains without extracting it. Extracting a tar file is generally a slow process, depending on the size of a file, so you can use the following command a lot in Linux:

$ tar tvf name_of_archive.tar

The “t” instructs Linux to display the files contained in the package file in a list form.

Listing the contents of a gzipped tarball

You should have figured it out by now, but we’re going to show you how to do it anyway. Just type in the following command:

$ tar tvf name_of_archive.tar.gz

Listing the contents of a bzipped tarball

Type in the following to find the contents of your bzipped tarball:

$ tar tvf name_of_archive.tar.bz2

Find the size of the tar file, gzipped tarball and bzipped tarball

You can find the size of your tar file, gzipped tarball and bzipped tarball before creating them. To find the estimated size of your tar file to be, type in the following command:

$ tar -cf – name_of_archive| wc –c

If you want an estimated size of your gzipped tarball, just change the name of your file accordingly and add the letter “z” to “cf”:

$ tar -czf – name_of_archive| wc –c

The letter “z” asks Linux to filter the file through gzip filter archive.

If you want the estimated size of your bzipped tarball, add the letter “j” to “cf”:

$ tar -cjf – name_of_archive| wc –c

The letter “j” instructs Linux to filter the file through the bzip filter archive

It’s worth exploring the “tar” command in more detail if you work with tar files on a regular basis. If not, knowing these basic commands will suffice. At any time, if you need a refresher, feel free to look up this Linux crash course or if you need further details about command line usage, you can look up our Master Linux Command Line course.