Use the ‘tar’ Command To Create Archives In Linux

When sharing a number of documents, it’s nice to be able to combine those documents into a single file. Using ‘zip’ is one way to do this. For Linux users, the ‘tar’ command has long been the go-to archive utility. With the advent of more inclusive GUI archive managers such as File Roller, it’s incredibly easy to use the ‘tar’ command, but for those users wanting to use the Terminal, it’s not that hard. Here are some basic commands to get you started using ‘tar’ to create archive back-ups for all your documents and folders.

For this tutorial, we’ll need a few things. First is the Terminal, so go ahead and open that up.

Once your Terminal is up and running, we’ll need a second thing: a folder of documents to practice our archiving skills on. In this article, we’ve created a folder called Files and have placed it on our Desktop.

Later, we’ll be using two additional documents, called Test.txt and Sample.txt, but we won’t get to those for a bit. To start with, we’ll change directories to the Desktop by using the ‘cd’ command.

Now we’ll start using ‘tar’ to create an archive. The simplest ‘tar’ command we can use is to simply create an archive called Files.tar, using the Files folder on our Desktop as the source. That command looks like this:

What the above command does is tell tar to create an archive called Files.tar, using the files in the Files folder. The ‘c’ in ‘-cf’ tells tar to create an archive, while the ‘f’ tells it to use the files (or folders) we list. After this command, the Files.tar archive should be sitting on your Desktop.

What if, later on, you decide you want to add additional files to your already created archive? You could use File Roller or another GUI program to do the job, of course. Or you could extract your existing archive, add the files, and then create the archive from scratch.

Or, if you’d prefer the simple way, you could simply append your files to the existing archive. Once again, we’ll use our existing Files.tar archive, and this time we’ll add our freestanding Test.txt document from earlier.

To use Tar to append our document to the existing archive, we’ll use this command:

For proof that this worked as expected, use File Roller to open the archive and check its new contents.

It worked! Okay, one last tip. What if you have two separate archives already, but want to combine them into a single archive? That’s also easy; using the catenate/concatenate fuction we can add the contents of one archive to an already existing archive.

So, let’s create another archive really quickly so we can test out this function. We’ll do things just like we did when we first create our Files.tar archive. This time however, wel’l use a different name, like Archive.tar, so you’ll have two to work with. And we won’t use our same Files folder, since that would just create an archive with duplicate items. So, we’ll create an archive called Archive.tar with the Sample.txt document mentioned earlier.

We now have two archives (Files.tar and Archive.tar). If you’ve followed all the steps so far, the Files.tar archive includes the Files folder and the Test.txt document, while the Archive.tar archive (just created) includes our Sample.txt document and nothing else. What we want to end up with here is everything stored in our Files.tar archive, so let’s combine them.

The command looks like this:

Note: the order of operations is important. Formatted the way it is, our command puts the contents of Archive.tar inside Files.tar, but if the order were reversed, the files would end up in the other archive.

One last look at our Files.tar shows us exactly what we want. A folder called Files, along with two documents (Test.txt and Sample.txt), all inside our Files.tar archive.

The above handful of commands only scratch the surface of what the ‘tar’ program is actually capable of. Beyond what we’ve done, we can use ‘tar’ to compare an archive to its original source folder, only updating those files that are newer in the folder than in the archive.

It can use the ‘tar’ command to extract archives (with different functions there as well), although that’s probably another article. But even with the few commands we’ve shown, ‘tar’ is a powerful program and one that should give the flexibility necessary for any level of user.

Comments [3]

So -f doesn’t mean ‘use the files (or folders) we list’, but rather means ‘output to this file’ (which means the archive file you specify). This is because tar was used primarily as a Tape ARchiver back in the day, and outputting to a file was just one of its options.

“The ‘c’ in ‘-cf’ tells tar to create an archive, while the ‘f’ tells it to use the files (or folders) we list.”

Actually the ‘c’ does tell tar to create the archive, but the ‘f’ SPECIFIES the output file name for the archive, in the example above, “Files.tar”. After the archive name, THEN you list the files/folders to add to the archive.

You will rarely see *.tar files these days. The additional feature of tar is to compress the tar file into a compressed file using gzip (‘z’ option), or bzip2 (‘j’ option). The first example above would be:

Most Commented

Aseem Kishore

Founder of Help Desk Geek and managing editor. He began blogging in 2007 and quit his job in 2010 to blog full-time.

SIGN UP FOR DAILY EMAIL NEWSLETTER

CONNECT WITH US

About Help Desk Geek

Welcome to Help Desk Geek- a blog full of help desk tips for IT Professionals. My name is Aseem Kishore and I work as a Systems Analyst in Dallas, TX. I graduated from Emory University in Atlanta, GA in 2002 with a degree in Computer Science and Mathematics.