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…’

How to Install Linux on Windows with Windows Subsystem for Linux

Windows 10 allows you to run a Linux bash shell through the Windows Subsystem for Linux (WSL) software. If you’d like to run Linux scripts and programs from within Windows, this may be a perfect option for you.

What is Windows Subsystem for Linux?

Microsoft has worked with Linux companies and devised a way to bring various distributions of Linux straight to Windows. It’s not quite the same as hosting your own instance of Linux in a virtual machine; however, it proves very convenient when all you need to do is run an occasional script or program.

WSL isn’t exactly the Linux you’re used to. It doesn’t actually have its own kernel (at least not yet), so there are some limitations. You can’t expect to use hard drive encryption with WSL, for example. But with that said, it’s impressive what you can do with WSL and we’ve found it to be very useful.

Before installing WSL

Before you can use WSL at all, you have to enable the component through the Windows Features window. You can open it by typing ‘windows features’ in the Start menu.

In the Windows Features menu, scroll down to find ‘Windows Subsystem for Linux’ and put a check mark in the box next to it, then click OK.

A prompt will appear, saying that Windows needs to restart in order to complete the changes. Click ‘restart now’ and rejoin us afterwards to start installing WSL.

How to install WSL

After your PC reboots, you can install WSL straight from the Microsoft Store. Open the store from the Start menu to begin.

In the Microsoft Store, click on search and type ‘linux.’ On the results page, click on ‘Get the apps’ to bring up the different Linux distributions available for download.

If you’re not sure which one to download, Ubuntu is always a safe bet. It’s the most used Linux distribution and has an excellent track record, but there are a few others available for download if you’d prefer them.

Click on the one you’d like and then hit ‘Get’ to start the download.

Once the download completes, you can access your Linux install from the start menu:

It may take a few minutes to add the finishing touches this first time around, but in the future you’ll have instant access to the terminal.

Once it’s done, the first thing it will ask you to do is create a default user and a password. It’s totally separate from your Windows login and doesn’t need to match.

And that’s it! You now have access to a Linux terminal right from Windows. We’ve also written an article on how to mount and access your hard drives in WSL in case you are wondering how to access some files you already have on your system.

How to Mount and Access Hard Drives in Windows Subsystem for Linux

When you finish installing WSL and open the terminal for the first time, you’ll probably be wondering how to access the files on your C: drive or other hard drives, flash drives, etc.

WSL will mount your hard drives for you automatically under the /mnt directory. The C: drive can always be accessed there, and usually other fixed NTFS drives that you have installed will be mounted there as well. But if you don’t see them, they are easy to mount.

Accessing the C: drive

Your main hard drive should always be accessible under /mnt/c

A simple change directory command should take you to the root of your C: drive…

cd /mnt/c

Accessing other drives and removable media

If you have a second hard drive or removable media such as a flash drive, you can try accessing them through the /mnt directory as well, obviously by appending the appropriate drive letter to /mnt/

If you can’t access your drives/media that way, you’ll just need to mount them first, which Windows has made very simple in WSL. In this example, we’ll mount and access drive D: in Ubuntu on Windows Subsystem for Linux.

Mounting:

sudo mkdir /mnt/d
sudo mount -t drvfs D: /mnt/d

Accessing:

cd /mnt/d && ls

You can mount your media anywhere you’d like; the thing to note here is that you must use Microsoft’s DrvFS when mounting accessible media within the subsystem.

In the case of removable media, you unmount them in the traditional way for safe removal:

sudo umount /mnt/d