The TAR Command In Linux For Archiving Files

Table of Contents

If you have stuck around long enough, then you may have come across the tar command in Linux before. First used on Unix 7 in January 1979, sometimes referred to as a tar-ball since its file structure consists of a bundle of objects. Much like an actual ball of tar, that will collect any object it comes in contact with. This is because they are one step away from tap drive media since .tar files stores data very similar to a tap drive. Yes, we mean the old computer storage that would take up an entire office. The name .tar seems to come from a combination of Tap Archive.

When working with .tar files for the first time, there can be some confusion. Today, we will show how you can store your data using .tar files. We will cover how to use some .tar file friendly programs. This is so you can compress a .tar file, resulting in an extended file extension like .tar.gz or .tar.xz. Notice, unlike .zip files, the .tar format needs other programs to preform compression. We will go over how to use a few programs to view the files while archived or compressed. Encryption is possible, but we will cover GPG (GNU Privacy Guard) in another blog. Check out Archiving files in Linux with zip to see how the two archiving formats compare. You can see what your preference is for yourself.

Get Started With The TAR Command In Linux

Unlike the .zip archiving format, the .tar archiving format is about a decade older in use as it comes with most Linux distributions. This has prompted many packages to be packed in .tar files for distribution. So, we will not waste time installing it today. We will create some test directories and then files to work with.

					mkdir test && cd test && touch document.txt && mkdir docs && cd docs && touch document-a.txt  document-b.txt  document-c.txt && cd ~/test

Now that we have some files and directories to play with, let’s see how to make a .tar file.

					tar cvf document.tar document.txt

As you can see, you need to mention the destination file before the origin file. Next thing you may want to do is archive a directory with many files. The options used here are the same as before. They include c to create the archive, v for verbose, and f to use the file or directory we specified at the end.

					tar cvf docs.tar docs

Compressing Files In A TAR Archive

Compression is an afterthought with .tar files, though the options have integrated some other programs to accomplish just this. We will go over gzip and xz, as they are both seen often in the wild. From my personal experience, you might download a Raspberry Pi image in these compression formats. Let’s see what it looks like to use gzip for compression.

					tar czvf docs.tar.gz docs

You can see that we are using the z option to invoke the use of gzip. We also added .gz to our destination file to specify the type of compression used. Now, we will explore what it looks like to use xz.

					tar cJvf docs.tar.xz docs

You can see similar as before, we are using the J option to invoke the use of xz. Here, we added .xz to the end of our destination file to specify the type of compression.

List TAR Archive Files

Now we will demonstrate how to view the contents of an archive while archived and compressed.

					tar tvf docs.tar.gz

Here, we used the t option to indicate that we would like to see the contents of the .tar file. We are using the f option to find the file mentioned. You can also use less to see the contents of a .tar file.

					less docs.tar.xz

You can see here that we will be able to scroll through the archive. We will need to type q to exit.


Now we will use Vim to see the contents of a directory as well as view the file. Furthermore, you can edit these files in their archived and compress state.

					vi docs.tar.gz

You may select any text-based file by hovering your cursor over it and pressing enter.

Then you will be able to edit the file by typing a. This will insert text after the cursor.


Then you can save changes and close the text document by typing ZZ. Alternatively, you can use :q! to quit without saving.


This will bring you back to the directories and their contents. You will need to type :q to exit back to the command line.


Extracting With The TAR Command

Now that we have everything so neatly archived, we can explore how to extract them. Make sure you only accept .tar files from good sources, specifically when installing a program by extracting it to your system files. The reason being is that hackers can put files in your system that are with bad intention. Leaving your system vulnerable to spying, trackers, or even just scramble your configuration files. Let’s proceed with extracting our .tar file.

					tar xvf document.tar

Notice, we used the x option to extract the file that was archived. Now we will explore how to extract a gzip .tar file.

					tar xzvf docs.tar.gz

Note that we used the x option again, though we also used the z option to decompress the directory that was compressed using gzip. Now we will explore how to extract a xz .tar file.

tar xJvf docs.tar.xz


Lastly, we used the x option once again, then we also used the J option to decompress the directory that was compressed using xz. Now that you have an idea of how to use .tar files, we encourage you to try it out with your files. You can learn to build a package for Linux, then distribute it to your friends. You can distribute a demo tap or important documents with ease. All with this remarkable open-source command line utility. You can just right-click to get a dropdown option when using a GUI to access these files in most desktop environments. You can also set a script to back up your files in a particular directory. Thanks for reading. Send your buddy a tar-ball today.

Meet the Author

Leave a Reply