The tar command creates the tar file type is used to combine multiple files into a single archive. Tar actually means “tape archive,” because tar’s original purpose was to be used on tape backups – that should tell you how old this format is. Linux systems still use the tar format, and it continues to enjoy widespread use to this day.
Tar files, with the extension
.tar, are often called “tarballs.” These files will preserve the Linux file permissions and can combine any number of files into the single archive, but they don’t apply any compression or space savings. However, compression can be easily applied to the tar file, resulting in extensions like
.tar.gz in the case of gzip compression, or
.tar.xz for xz compression.
Tar archives are opened and create with the tar command in Linux. The type of compression you wish to use, and the files that you want to archive will determine what options you need to use with the command.
In this guide, you’ll learn how to use the tar command in Linux through examples. Follow along below to learn about the various options that you can use with this command.
In this tutorial you will learn:
- How to use the tar command on Linux
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Frequently Used Options
The tar command in Linux can be used to create and open many different types of compressed archives. One of the reasons that the tar command is frequently used on Linux is because it can preserve Linux file permissions. Other operating systems, such as Windows, cannot always open tar files by default, but it’s a ubiquitous standard across all Linux distros. Follow along with our examples below to see how to open and create different types of tar files.
tar command in Linux Basic Examples
- You will need a minimum of two options with the tar command when creating an archive. Those options are
f(file). These options will tell the tar command to create a new archive and allow you to specify a filename for the archive, respectively. In the following example, we will combine three files into a single tar archive with no compression.
$ tar cf archive.tar file1 file2 file3
This command will leave us with an uncompressed tar file named
archive.tar. And inside of it will be our three files. The only advantage of not adding compression to the tar archive is that it will combine the files very quickly and not require much processing power.
- Probably the most common type of compression to use on a tar archive is gzip. To create a tar archive that uses gzip compression, you will need to add the
zoption to your tar command.
$ tar cfz archive.tar.gz file1 file2 file3
- Another very popular type of compression on Linux is bzip2. You can use the
jto create a tar archive that uses bzip2 compression. On most types of files, this will yield a smaller compressed archive than gzip, but will also require more processing power and time.
$ tar cfj archive.tar.bz2 file1 file2 file3
- A more recent type of compression that has risen in popularity over the last few years is xz compression. This type of compression will usually give you the smallest file size but will also take a considerable more amount of processing power and time than the other options we’ve covered so far. You will need to use the
Joption with tar to create this type of archive.
$ tar cfJ archive.tar.xz file1 file2 file3
- To decompress a tar archive, regardless of the type of compression used, you can use the
foption. Simply pass the name of the tar archive to the command to decompress it.
$ tar xf archive.tar
You can always use the man command to read more about the tar command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.
The tar command is pretty simple to understand, but it comes packed with tons of options under the hood. So far, we have seen how to create various types of compressed archives with the tar command and how to open them. In the section below, you’ll see some more advanced options that you can use with the tar command, allowing you even more control over your compressed archives.
tar command in Linux Advanced Examples
- Use the
-Coption if you would like to extract the contents of a tar archive to a different location other than your present working directory.
$ tar xf archive.tar -C /path/to/dir
- Use the
-v(verbose) option to see which files the tar command is currently working on adding to or extracting from an archive.
$ tar cvf archive.tar file1 file2 file3 OR $ tar xvf archive.tar
- To extract a specific file or files from a tar archive, simply specify the names of the files that you’d like to extract.
$ tar xf archive.tar.gz file1 file2 file3
In this guide, we learned all about the tar command on Linux. The tar command is the principle command used on Linux to manage archives, either compressed or uncompressed. We have a lot of options to use to compress our tar archives, making the command very powerful and versatile. After mastering a few of the command options, you’ll be able to quickly compress and extract files from the command line.