Manage Files In A tar Archive

Today we’ll have a fairly simple article, albeit a bit archaic, as we learn how to manage files in a tar archive. We often work with modern compression methods but tar files still exist and are in wide use. If this is something you’re interested in, this is the article for you. If this isn’t something you need right now, remember this article and look it up later when you do need to manage files in a tar archive.

Sure, tar now often ends with .gz or .bz, but they still exist. You’ll still see them and you’ll still get regular questions about them. In fact, a lot of folks will ask questions like, “How do I install files from a .tar file?” Alas, that’s a pretty broad question and not something we’ll answer here. However, the answer is, “Manually.” That’s how you install files from a tar file, generally speaking.

What Are tar Files?

So, I’m pretty sure we’ve covered this before, but ‘tar’ stands for ‘tape archive’ and is a pretty old standard. Yes, I am well aware that tape archives still exist. There’s no reason to leave a comment telling me that. I know, but I suspect most tar files never see a tape throughout their entire lives. If you’re curious, the man page describes the tar application as this:

tar – an archiving utility

The format has been around since 1979, though it has undergone some changes along the way. If you’re interested, there’s an excellent Wikipedia article about tar files. It’s well worth skimming it, as I did when I wrote an earlier article.

Basically, tar is a way to organize multiple files into a single file. Tar files are not compressed in and of themselves. We more often see them compressed with .gz (or the like) but the format doesn’t include compression of any kind. It is also meant to work with any file system, as it contains no real file system in and of itself.

Well, eventually you may want to work with tar files. You may want to add, remove, or modify tar files. That’s all easily done within the Linux terminal and this article will show you how.

Manage Files In A tar Archive:

Obviously, you need a terminal for this application. By now, regular readers will know how to open a terminal. In most distros and desktops, you can just press CTRL + ALT + T and, with that, the default terminal will pop open.

With your terminal now open, let’s go ahead and create a new tar file to mess around with…

Now, we’re all set for this exercise…

Add Files To A tar File:

So, it’s easy enough to add files to a tar file. The syntax to do so is quite simple, though you do need a few flags.

Of course, you can add multiple files by just adding their file name. Using the files you created in the first step, you’d add files to a tar file like so:

To avoid any confusion later, we’re now going to clean up after ourselves, leaving just the foo.tar file.

Now, in your terminal you can type the following:

In the list of files, you should now only have the foo.tar file remaining. For neatness sake, this is an important step. You can list the files in a tar file with the following command:

The output should look like this:

See?

Remove A File From A tar File:

Now that you have created a tar file and added files to it, you next need to learn how to remove a file from a tar file. That’s actually a very simple process. The syntax is as follows:

So, using our current example file, we’d do something like this:

You can check again to see that the foo3 file no longer exists.

Still with me?

Well then, the next logical step in this process would be learning how to…

Change Files In A tar File:

If a file already exists in a tar file, you will first need to extract that file. You can do so easily. Use the tar -tvf <file_name>.tar to find the specific file’s name. With the name of that file, you can extract it with this command:

So, using the example file that we’ve worked through, we’d change the file called foo2 by first extracting it, like so:

That will extract the foo2 file from the archive. You can then edit that file, say with nano, to make it say what you want it to say. Feel free to do so, but you must remember to save the file after you’re done editing the file. (To save files in nano, press CTRL + X, then Y, and then ENTER.)

Now, you just replace the file with the file you’ve edited. In this case, it’d be foo2 that you’ve edited. The syntax is pretty easy:

So, in our case, you’d be replacing foo2 with the previous foo2 file. That’s a nice and simple command. To finish the exercise, you can run this command:

Be sure to check the man page to learn what the flags do and to see the many other options that come with your tar application. It should absolutely be installed by default, on even the most bare of distros.

Closure:

So, well, I figured this would be a fun article to write. I was not wrong. If you want, you can clean up after yourself with this command:

I figured it’d be a fun article for folks to follow and wrote it in a way that I hope facilitates that. There’s nothing all that complicated about it. If you want to manage files in a tar archive, this is a good way to do it. You could probably do it with a GUI, but this way is just as good – I think.

Also, this was the article I wanted to write last time. You got a meta article because of my poor internet connection. This was the article I’d hoped to write. It’s quite a bit different than most of my articles, but hopefully, it takes you clearly from the start to the finish – including cleaning up after yourself!

As always…

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Subscribe To Our Newsletter
Get notified when new articles are published! It's free and I won't send you any spam.
Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.