« Back to Cheat sheets for Linux

tar cheat sheet

The tar command is common on Linux and UNIX based systems. It dates to Version 7 of Unix, and had its introduction in January 1979. Tar allows the system administrator to create file archives, similar to a ZIP file. In this cheat sheet we look at how to use the command, including several useful examples.

Basics

The tar command can be used by just a few options, followed by an archive name, optionally followed by another set of options.

Options

OptionWhat the option does
-cCreate archive
-fDefine file name of archive
-JCompress using xz (may need newer version of tar)
-vVerbose output
-zCompress using gzip
--zstdCompress using zstd (may need newer version of tar)

A few remarks when using tar and the listed examples:

  • The options can be typically used with hyphens or without.
  • Tar on Linux has other options than tar on systems like BSD, so consider this when an archive may be used on other systems.
  • Typically compression is saving storage space, but it may not be needed when archiving already compressed files. In these examples gzip compression is used.
  • Newer tar versions have also the option to compress using more modern compression methods that save more space.

Tips when working with tar

Tar has the option -v to display more verbose output. This may be helpful, but can come at the cost of speed. When using big archives, consider to leave out this option for additional performance.

The order of options is very flexible, so it is possible to put options before or after the archive file name. Important is that the -f option is in front of the file name.

Creating archive

To create an archive using tar, there are multiple options. The first one is that we can go to the directory that we want to archive, and put the archive itself one level higher.

cd my-directory && tar czf ../archive.tar.gz .

Another option is to define the working directory with -C which is then considered as the root of the directory structure.

tar czf /root/mybackup-of-etc-systemd.tar.gz -C /etc systemd

Slightly similar, but with storing the full path to the /etc/systemd directory:

tar czf /root/mybackup-of-etc-systemd.tar.gz /etc/systemd

Show contents of archive

Before taking an action on an archive made by tar, it is wise to inspect it first. Typically the file extension tells you if it is compressed or not. Secondly, we can see the contents of an archive, so that we know the full paths of the files included. This is important when extracting the file, so the directory structure will be in the right place.

tar tzf archive.tar.gz

Extracting archive

To extract the archive in the current directory.

tar xzf archive.tar.gz

Define location where to extract

Sometimes the contents of the archive should be extracted into a different location where archive resides. In that case, go to the (new) directory where you want to extract. Define where the archive is and that the extraction should place in the current directory. This is helpful when the archive is big or located on another file system.

cd /path/to/extract && tar xzf /path/to/archive.tar.gz -C .

Another option is define the location while the current work directory is the same as where the archive is. Short and effective.

tar xzf archive.tar.gz -C /path/to/extract

Depending on how and where the file will be extracted, it may be beneficial to have the full path or just the directory name.

Single file

Extract a single file or directory from the archive

tar xzf archive.tar.gz “file1”

Using wildcard

To retrieve only a specific file type from the archive, a wildcard can be used.

tar tzf archive.tar.gz --wildcards '*.txt'

Do you have other good one-liners that everyone should know?

Relevant articles using tar command

The following articles include an example on how to use tar and might be worth further exploring.

Liked this cheat sheet? There are more!

Related articles

Like to learn more? Here is a list of articles within the same category or having similar tags.

Feedback

Small picture of Michael Boelen

This article has been written by our Linux security expert Michael Boelen. With focus on creating high-quality articles and relevant examples, he wants to improve the field of Linux security. No more web full of copy-pasted blog posts.

Discovered outdated information or have a question? Share your thoughts. Thanks for your contribution!

Mastodon icon