15 Tar command in Linux uses with examples

Tar is mostly used for packaging applications and their binaries for raw installation that would work on any system, rather than base specific formats like DEB, RPM or AUR packages. Other than that, tar is also quite an efficient format for creating backups. Let's check out its usage with practical examples.


Tar is a great utility that is used for collecting multiple files in a single archive. This file is often called a ‘tarball.’ Today, we are going to tell you about the different things you can do with tarballs using the tar command, with practical examples.

Tar is mostly used for packaging applications and their binaries for raw installation that would work on any system, rather than base specific formats like DEB, RPM or AUR packages. Other than that, tar is also quite an efficient format for creating backups.

NOTE: All the -f tags used anywhere are necessary whenever interacting with any ‘files,’ which is almost every case.

Using the Tar command in Linux

1. –list

This flag is used to list the files inside a tarball. The required argument is only the name of the tarball and (as mentioned), the -f flag. The standard syntax looks like:

tar --list -f <Tarball name>

In shorter form:

tar tf <Tarball name>


2. –create


The –create flag is used to create a new tarball. The required arguments are the name of the (future) tarball and the files to be included. The names of directories are also acceptable as an argument. The standard format is:

tar --create -f <Required tarball name> <File names>

This can be used in a short form in the following way:

tar cf <Required tarball name> <File names>


Creating other formats of archive

There are different formats of archiving that are included in tar, which are used for compression. The formats we are going to show are the .tar.xz and .tar.gz formats.

Creating a XZ (.tar.xz) archive

tar cfJ <Required tarball name> <File names>

Creating a GZip (.tar.gz) archive

tar cfz <Required tarball name> <File names>

Creating a bzip2 (.tar.bz2) archive

tar cfj <Required tarball name> <File names>

3. –append

The –append flag is used to add files to an already existing tarball. The syntax is pretty simple, that is:

tar --append -f <Tarball name> <Names of files to be added>


tar rf <Tarball name> <Names of files to be added>


4. –delete

This is used to delete files from a tarball. The syntax requires the name of the tarball and the name of the file in the following order:

tar --delete -f <Name of tarball> <Names of files>


5. –diff

The –diff flag finds the differences between a tarball and certain files that the user has to specify. For example, as mentioned before, tar is extensively used to create backups. So if there is an existing tarball, and you want to check whether or not there are any differences between the current state of the files, compared to the ones that are already in the tarball, you can use –diff.


In this situation, the file test in the tarball test.tar is empty, but the one outside the tarball has the contents as shown. Notice the differences demonstrated by tar.


As you can see here, the error shown between the empty test and the non-empty test is ‘Size differs,’ but even when the size is the same, there is an error given off:

Different content
Different content

Conclusively, the general syntax can be shown as:

tar --diff -f <Name of tarball> <Names of files to be compared>

6. –update

This flag can be used to update a specific file inside the tarball. If a file of the same name is placed in this command, the file with the new content will be appended to the end of the file, rather than the old one being replaced by the latest version. The screenshot below describes this:


Again, this can be used in the case of making backups. The general syntax becomes:

tar --update -f <Tarball name> <Names of files to be updated>

Shorter format:

tar uf <Tarball name> <Names of files to be updated>

7. –extract

This is probably the most used flag of the tar command. It is used to extract files from a tarball, and since it is so used, it also has a lot of configurations. For a change, the arguments except the tarball name are optional. If not specified, tar will extract all the files inside the tarball. So, the syntax to extract specific files:

tar --extract -f <Tarball name> <File names>

For extracting all files:

tar --extract -f <Tarball name>

Or a shorter format:

tar xf <Tarball name> <Optional file names>


Extracting GZip (.tar.gz) archives

tar zxf <Tarball name> <Optional file names>

Extracting XZ (.tar.xz) tar archives

tar xf <Tarball name> <Optional file names>

Extracting bzip2 (.tar.bz2) archives

tar xjf <Tarball name> <Optional file names>

Now, after handling the basics, we can go into some deeper features of tar.

8. Overwrite control

It may just happen that the names of one or more of the files being extracted from a tarball coincide with the name of files already existing in the working directory. Some measures can be taken to do what you desire when that happens. Various scenarios:

To overwrite the files

The first case is that you do want to replace the files existing in the directory. To make sure this happens, issue the command in the following way:

tar --extract -f <Tarball name> <Optional file names> --overwrite

To not overwrite files

This will not overwrite the files, saying that the files already exist. The command can be issued in the following way:

tar --extract -f <Tarball name> <Optional file names> -keep-old-files

To only overwrite older files

This is particularly useful, as it only protects those files that are newer compared to the files in the archive. For example, here, test2 inside the archive has content that has been modified as compared to test2 in the directory:


As you can see, it says ‘… is newer or same age‘ for the rest of the files, but not test2, which means test2 was replaced. Therefore, the structure of the command is:

tar --extract -f <Tarball Name> <Optional file names> --keep-newer-files

9. –remove-files

This flag can be used as a shortcut for removing files from the directory directly after they have been archived. Its a simple enough command, the syntax follows:

tar --create -f < Required tarball name> <File names> --remove-files


10. –exclude

The –exclude flag can be used to exclude certain files from being archived. The pattern has to be specified before anything else, and in inverted commas, as used in the command below:

tar --exclude='PATTERN' --create -f <Tarball name required> <File names>

Example usage:


11. –to-command=’COMMAND’

The tar command has its feature for using output as the input for another command, that is the –to-command flag. For example, I want to use the names of the files from test2.tar and create a directory of the same name as that of the files inside the archive. This can be done as:

tar xf test2.tar --to-command='mkdir $TAR_REALNAME'


This is fairly advanced. The ‘$‘ is necessary to refer to any system variable while using Bash. The name ‘TAR_REALNAME‘ is the variable used to refer to a single file’s name while using the –to-command flag in tar. There are several such variables, which can be found in the man pages of tar, as well as on this webpage.

12. Ownership of files

The ownership of files in the archive can be configured in various ways. First of all, to create an archive in which the files are set to a specific owner, the flag –owner can be used. Example usage:


As seen, user 0, and group 0 corresponds to the user root. Root (as you probably know) is the overlord emperor of the system. You can find the UID (User ID) and GID (Group ID) of other users using the id command in the following way:

id <username>

Now to extract the files while preserving the owners of the file:

tar xf <Tarball name> --same-owner

13. Complete backup solution

As I’ve mentioned a few times already, tar is quite a good backup program. I am going to tell you about some more flags that make it even better. First of all, there are things that we want to include/exclude while making backups. Include the files that links (symbolic links) are pointing towards. Exclude prior backup files and VCS directories (mostly just useful for programmers). It is also important to stay out of the directories that you’re making a backup of.

For example, you’re in the /home directory, and you want make a backup of the user, testuser. The command would look like:

sudo tar --exclude-backups --exclude-vcs --dereference --create -f backup.tar ./testuser
  • ‘sudo’ for two reasons: First, that you’re doing things in the /home directory, which requires privileges. Second, that if any symlinks point to files out of the user’s usual privileges, that will cause an error.
  • –exclude-backups and –exclude-vcs exclude backup and VCS files respectively.
  • –dereference makes tar follow the symlinks to the data they are point to.

14. Modified time

There are a whole lot of things that can be done with the modification time of a file. First of all, to change the modified date as a file is being archived, we can use the –mtime flag. Basic syntax:

tar --create -f <Required tarball name> <File names> --mtime=YYYY-MM-DD


As an antidote to this, the -m flag can be used to obtain the unchanged, modified date. To use this:

tar xf <Tarball name> -m

Only act on files modified after a specific date

This form of the command can be used to act upon files that have been modified after a specific date. The command looks like:

tar xf <Tarball name> --newer-mtime=YYYY-MM-DD

The example usage is shown below.


15. Permissions

Finally, permissions can be configured quite well in the case of tar files. There are two significant variations:

Preserving original permissions

This can be used to preserve the permissions for the file, as mentioned in the archive after being extracted. This is achieved through such a command:

tar xf <Tarball name> --preserve-permissions


Modifying permission as the default value

Secondly, the permissions can be changed as the default umask values. The default value varies by the systems. To do that, enter the following command:

tar xf <Tarball name> --no-same-permissions



The tar command is much more than what it seems. It has a vast spectrum of uses, from packaging applications, creating backups, to storing even entire OSs. We hope the tutorial helped you in learning something new and exciting things about tar. For any questions, feel free to use the comments section.

Pulkit Chandak
Pulkit Chandak is a Linux enthusiast and has been using and experimenting with open source software and hardware too since a long time. He is a huge admirer of open source software and wants to ventilate it to all around him. He is interested in reviewing and writing tutorials on Linux and its many distributions. He believes that freedom in software leads to freedom of the mind from the chains of limits.


Please enter your comment!
Please enter your name here




Top 20 Git Commands with Practical Examples

If you are here reading this post, there is a high probability that you have heard or interacted with Github, and you now want to learn Git. Before we continue with showing you some of the cool Git commands, let's understand the difference between Git and GitHub.

Top 10 New Features in Linux Kernel 5.7

Linus Torvalds has announced the release of Linux Kernel 5.7 after seven weeks of development. The release announcement comes as a piece of exciting news as it brings a host of new features for the hardware manufacturers as well as the developers.

How to install CMake on Ubuntu

CMake is a cross-platform free and open-source software tool designed to build, test, and package the software. CMake uses a simple platform and compiler-independent configuration files to control the software compilation process.

How to install Lightworks on Ubuntu

Even though Linux may not get a native installer of video editing software like Adobe Premiere or Final Cut Pro, that doesn't mean there are no industry standards tools available. Lightworks is non-linear editing (NLE) video mastering app for Windows, Linux, and macOS. Installing it on Ubuntu is simple due to deb package availability.

How to install DaVinci Resolve on Fedora

Davinci Resolve is a professional application used for color correction, video editing, visual effects, and motion graphics. It is one of the extensively used software by movie industries located in Hollywood.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.


Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

5 Best Ways to Free up Hard Disk Space in Ubuntu

Hard disk space can easily get filled up with cached package files, old kernels, and other obsolete files that occupy unwanted hard disk space. Here are top five best and safer ways to clean and free up hard disk space in Ubuntu. We show you Terminal and GUI way of cleaning up system.

5 Best Download Managers for Linux

We often need to download large files that can go corrupt due to various reasons such as slow internet or interrupted download. Using a broken downloaded file is not something one wants. Download managers make sure that the downloaded file maintains its integrity and also presents you with the ability to pause and resume downloads, provided the server supports it. When you are downloading a massive file, it's recommended to use a download manager.

Installing an updatable Manjaro Linux on a USB Flash Drive

In this tutorial, we will show step by step how to install a Manjaro on a USB flash. For those new to this distro, it is a light-weight distro that is based on Arch Linux. Arch Linux is known for its simplistic approach and is typically made for Linux pros, but Manjaro bridges the gap between user-friendliness and rock-solid performance.

10 Best PDF Editors for Linux

In this article, we will take a look at 10 of the best PDF editors and tools out there in 2019 that are available for Linux platforms. The editors are going to be judged on the basis of their functionalities, portability, ease of installation, price, and convenience.