10 Useful tar Commands in Linux With Examples

This blog post will show you the ten most used tar commands in Linux with examples.

The tar command in Linux provides archiving functionality for compressing or decompressing archive files and modifying and maintaining them. With the tar command, we can create tar, gz, bz2 and gzip archives. Administrators use these commands when they want to save some disk space on the servers or when they are migrating files over two host machines.

In this tutorial, we are going to execute the tar commands on Ubuntu 22.04 OS, but you can choose any Linux distro. Let’s get started!

Prerequisites

  • Fresh install of Ubuntu 22.04
  • User privileges: root or non-root user with sudo privileges

Update the System

If you have a fresh installation of Ubuntu 22.04, it is recommended to update the system packages to the latest versions available.

sudo apt update -y && sudo apt upgrade -y

Once the system is updated, we are ready to show you the tar commands in Linux with examples.

1. Syntax of the tar command

The tar command have the following syntax:

 tar [options][archive-file] [file or directory for archiving]

2. Tar.gz Archive File Creation

To create a tar.gz archive file, execute the following command:

tar -cvzf wordpress.tar.gz wordpress/

This will create gzip compressed file specified with the z option and the v option to show the progress while the file is being zipped.

3. Archive File Creation

To create an archive file, execute the following command:

tar -cf wordpress.tar wordpress/

This will make a new file, defined with the c option, and the file name type specified with the f option.

4. Tar.bz2 Archive File Creation

To create a tar.bz2 archive file, execute the following command:

tar cvfj wordpress.tar.bz2 wordpress/

This will create a tar.bz2 archive file with highly compressed files in it. That is why is used the j option.

Now, lets check the sizes of each compressed file:

62M     wordpress.tar
18M     wordpress.tar.bz2
21M     wordpress.tar.gz

As you can see, the wordpress.tar.bz2 has the smallest size. This is because we used the j option for highly compressed tar files.

5. List the content of Tar.gz Archived File

To see the files in the compressed tar.gz archive file, execute the following command:

tar -tf wordpress.tar.gz

The option t in this command is for listing the content.

6. List the content of Tar Archived File

To see the files in the compressed tar archived file, execute the following command:

tar -tf wordpress.tar

7. List the content of Tar.bz2 Archived File

To see the files in the compressed tar.bz2 archived file execute the following command:

tar -tf wordpress.tar.bz2

8. Uncompress Tar.gz, Tar, and Tar.bz2 Archived Files

To extract the tar, tar.gz, and tar.bz2 archived files, you need to use option x. Execute the following commands one by one to extract previously archived files. Option v is for progress and is up to you if you want to use it or not.

tar -xvf wordpress.tar.gz

tar -xvf wordpress.tar

tar -xvf wordpress.tar.bz2

9. Untar Single file

To untar the single file index.php from Tar.gz, Tar, and Tar.bz2 archived files execute the following commands respectively:

tar -xvf wordpress.tar.gz wordpress/index.php

tar -xvf wordpress.tar wordpress/index.php

tar -xvf wordpress.tar.bz2 wordpress/index.php

10. The Man Command for tar

There are plenty of tar commands used on a daily basis by system administrators. If you want to know more about the tar command, just execute the man tar command in your terminal, and you will get the output like this:

root@host:~# man tar
TAR(1)                                                                     GNU TAR Manual                                                                    TAR(1)

NAME
       tar - an archiving utility

SYNOPSIS
   Traditional usage
       tar {A|c|d|r|t|u|x}[GnSkUWOmpsMBiajJzZhPlRvwo] [ARG...]

   UNIX-style usage
       tar -A [OPTIONS] ARCHIVE ARCHIVE

			.
			.
			.
			.
			.

We trust that today’s guide on useful tar commands was straightforward and useful.

Now, it’s your turn:

Is there any part you think we didn’t cover, or do you need further clarification on any of the commands? Do you have any recommendations for other topics or tutorials you’d like us to work on?

Your feedback is valuable, so please leave a comment below.

Leave a Comment