How to Open and Create tar Files on Linux (with Commands and GUI)

A tar file is an archive that contains one or more files. If you’re familiar with zip files, tar files (or “tarballs” as they’re frequently called) work the exact same way. On Linux, tar files are very common. Throughout your time using the operating system, you’re bound to run into a few of them, so let’s take a look at how to open and create these files.

Related: How to Open and Create tar Files on Windows

Note: We’ll be using Ubuntu for the screenshots and examples in this guide, but we’ve generalized everything as much as possible. You should be able to follow these instructions no matter what distribution you happen to be using.

File compression and their corresponding extensions

The base .tar format doesn’t compress files. It’s basically just a file that holds more files. If the tar file is compressed, it will have another extension beyond just .tar. There are many different compression methods, resulting in an equal amount of different extensions. A few of the more popular ones that you’re likely to see include:

  • .tar.gz / .tgz (uses gzip compression)
  • .tar.bz2 / .tar.bz, / .tbz (uses bzip2 compression)
  • .tar.xz (uses xz compression)
  • .tar.7z (uses 7-Zip compression)

There are others, too, but… you get the idea.

How to archive files/folders with tar on Linux command line

You have a lot of options when creating tar files. If you’re interested in the minutiae of tar switches, you can always check out the man pages with the ‘man tar’ command. Here are a few of the essential commands:

Create a .tar archive file (no compression)

tar -cvf name-of-tar-file.tar /path/to/directory-or-file

What those switches do:

  • -c: Create a new tar file.
  • -v: Verbose mode. Not strictly necessary, but it will display progress in the terminal.
  • -f: Specify the folder(s) and/or file(s) to archive.

Right after the switches, specify the name of your tar file. After the name of your tar file, list the paths to each of the files and/or directories that you’d like to archive in the tar file.

You can specify as many files or directories as you’d like by just separating them with a space. In the screenshot above, we archived file1.txt and file2.txt into MyFiles.tar.

Create a .tar.gz archive file (gzip compression)

To create a compressed archive, the process is the same but with an additional switch and a different file extension:

tar -czvf name-of-tar-file.tar.gz /path/to/directory-or-file

Keep in mind that the order of the switches does matter. Make sure you put the z switch right after c. The switch we added:

  • -z: Compress the tar file with gzip.

Create a .tar.bz2 archive file (bzip2 compression)

Another popular compression method is bzip2. To create a .tar.bz2 file, we add the -j switch (instead of the -z in the gzip command) to our base tar command from above. Don’t forget to also name your file extension correctly:

tar -cjvf name-of-tar-file.tar.bz2 /path/to/directory-or-file

Gzip is the most popular method for compressing tarballs, but you’re likely to also run into bzip2. It compresses better than gzip, but also takes a little longer.

How to open tar files on Linux command line

The command for extracting the contents of tar files is almost the same as it was for creating them; you just need to replace the -c switch with the -x (extract) switch instead.

Extract a .tar file

Note: As long as your Linux installation has an updated version of GNU tar, you can just specify the following command to extract any tar archive. Tar will recognize the type of archive without any extra input from the user. We’ve also included the other switches in case you’re using a different version of tar.

tar -xvf name-of-tar-file.tar

Extract a .tar.gz file

tar -xzvf name-of-tar-file.tar.gz

Extract a .tar.bz2 file

tar -xjvf name-of-tar-file.tar.bz2

Extract a .tar.xz file

tar -xJvf name-of-tar-file.tar.xz

Extract to a different location

You can also specify a different location to extract the files to, instead of your present working directory, with the -C switch. Just make sure that the -C is uppercase.

tar -xzvf name-of-tar-file.tar.gz -C /path/to/directory

How to archive files/folders with tar on Linux GUI

If you’re more comfortable with the graphical user interface of Linux than the command line, don’t fret. Tar archives are rather easy to create this way.

Highlight the files you wish to archive, right-click, and click on ‘Compress’.

On the next menu, you’ll be able to select between .zip, .tar.xz, or .7z:

You’ll notice that the gzip and bzip2 options are missing in this screenshot, as Ubuntu recently removed them from this window. You can still find those options and a plethora of others in Ubuntu’s Archive Manager.

How to open tar files on Linux GUI

To extract the contents of a tar file, compressed or not, you just need to right-click it and either select ‘Extract Here’ or ‘Extract to…’

Leave a Reply

avatar
  Subscribe  
Notify of