How to open tar files in Linux

Linux Commands You Need to know Part 3: Working with Tarballs

In this article, we'll learn all about tarballs. How to create them, how to extract files from them and more.

Author: Jeremy Morgan


We are streaming LIVE now on Twitch. Join us!

In the last article in our series, Linux Commands You Need to know Part 2: Working with Files we learned how to work with files on our filesystem. Now we’re going to look at something that trips up many beginners: tarballs.

Today we’ll learn:

  • What are tarballs?
  • How do we add files to a tarball?
  • How do we extract files from a tarball?
  • How do we install software from tarballs?

So let’s get started.

What are tarballs?

What are tarballs? They’re a group of files bundled together in a single file. Most folks are familiar with .ZIP files as archives. It’s the same concept. You take a set of files and bundle them together, and compress them into a .tar, tar.gz, or .tgz file.

When the files are uncompressed, they’re usually named something like filename.tar. If they’re bundled and compressed, they’re named filename.tar.gz.

Another extension you may see is filename.tgz, which is just shorthand for filename.tar.gz. They are identical.

How do we create a tarball?

Let’s create a tarball just for fun. Open up your terminal.

We’ll create four empty files:

touch file1.txt file2.txt file3.txt file4.txt

type in ls -la to confirm they are in your directory:

How to open tar files

Now, let’s create a tarball named ourfiles.tar. This will be uncompressed. We’ll tell it to

  • create an archive (c and f argument)
  • be verbose (v argument)
  • specify the files to add

We have the tar command and add our arguments (-cvf), the name of the archive to create, and *.txt for all text files.

Type in this command:

tar -cvf ourfiles.tar *.txt

You should then see the files that were added to your tarball.

How to open tar files

Also, if you type in ls -la again, you’ll see your new file:

How to open tar files

Great! This is a bundle of your files, uncompressed. Let’s create a compressed archive. You can compress files by gzipping them.

To do this, we just add an argument to the tar command (z), and we change the file extension to .tar.gz:

tar -cvzf ourfiles.tar.gz *.txt

Once again, we see the files that were added listed here.

How to open tar files

Now run ls -la to view the archive:

How to open tar files

Notice how much smaller the .tar.gz file is than the .tar? It’s because it’s been compressed.

Let’s delete the text files in the folder now.

rm *.txt

Now we’re left with our two archives.

How do we extract files from a tarball?

We have two tarballs here, one compressed and one uncompressed. How do we retrieve these files?

We’ll start with the .tar.

tar -xvf ourfiles.tar

How to open tar files

We see our files again are extracted so we can access them. What if we want to put them in a specific folder?

Let’s create a folder to zip them to.

mkdir myfolder

Next, we’ll perform the same extraction, but we’ll add another parameter (-C) to specify where to extract it. We’ll tell it to extract to our new folder:

tar -xvf ourfiles.tar -C myfolder/

How to open tar files

And the files are now in that folder.

If you’re working with a gzipped tar (.tar.gz) you add the “z” flag to it:

tar -xvzf ourfiles.tar -C myfolder/

How to open tar files

and it performs the same function. Easy!

How do we install software from tarballs?

Let’s expand on this a bit. Sometimes in Linux, you’ll want or need to install software from a tarball. You can do this using the same techniques we just learned. It’s all about where you extract it.

In this example, we will install the Go language runtime on our Linux Machine.

First, browse to the Go Downloads Page and find the latest version.

In this case, you can see the latest Linux version on the front page. Notice it’s in a .tar.gz format.

How to open tar files

Right click on the link, and select “copy link”

How to open tar files

In your terminal, type in the following:

wget https://go.dev/dl/go1.18.linux-amd64.tar.gz

Note the version number might have changed by the time you read this.

This will download the archive to your machine. Wget is installed by default on many Linux distributions. If you get “command not found” install it for your distribution.

Now we have the tarball on our machine.

How to open tar files

We need to install it into a folder where our applications are stored. We’ll do it by using an extract function to extract the files into /usr/local. Since we’re extracting into /usr/local, these files will be in our path where you can call them from the command line. Different distributions of Linux will have other areas for your applications. But to install software you place the files in a folder where your applications live.

Let’s extract Go into /usr/local:

sudo tar -xvzf go1.18.linux-amd64.tar.gz -C /usr/local

once again, you’ll see the files displayed (the -v flag does this)

How to open tar files

And now Go is installed and ready!

How to open tar files

It’s easy once you get the hang of it. You can install any software that is archived in .tar, .tar.gz, or .tgz format. Many software packages use this format to be portable. Packaging formats like .deb or .rpm are specific to a distribution, but .tar files work with everything.

Conclusion

In this article, we learned

  • What a tarball is
  • How to create one and add files to it
  • How to extract files from a tarball
  • How to install software from a tarball.

I hope I’ve demystified these files and how they work. Once you get good with .tar files you can download and extract things from the command line, and you can set up automation that backs up your files regularly, among other cool projects.

Please keep checking back to this site as we add to this series. We’ll get into some advanced command line topics that will have you feeling like a Linux wizard in no time.

You can test your Linux skills to see where you’re at:

“How to open tar files”

I scored a 203, so I still have some room to grow. What’s your score? Take your SkillIQ now.

Have any questions? Comments? Let me know!

–Jeremy



Related tags:

linux   it-ops  
About the author

Jeremy Morgan is a tech blogger, speaker and author. He has been a developer for nearly two decades and has worked with a variety of companies from the Fortune 100 to shoestring startups.

Jeremy loves to teach and learn, writing here on and on his Tech Blog as well as building Pluralsight Courses.

10-day free trial

Sign Up Now