General

How to Use the tar Command: Useful Examples for Archiving and Compressing Files in Linux

General·March 22, 2026·37 min read

How to Use the tar Command: Useful Examples for Archiving and Compressing Files in Linuxtar, short for tape archive, is a Linux command for archiving and compressing files. This article covers the most common examples and best practices of the tar command for effective file management in Linux.

What Is the tar Command?

The tar command allows you to group multiple files and directories into a single archive file, making data storage and transfer easier. Typically, the result of a tar command is a file called a tarball, which uses the .tar extension.

When working on Linux systems, such as a virtual private server (VPS), the tar command offers several advantages:

Reduces the number of managed files, helping you organize storage more efficiently.

Minimizes file sizes and optimizes disk space when combined with tools like gzip or bzip2 for different compression formats.

Creates backups of entire directories to easily preserve and recover data.

Simplifies file transfer between systems, since you deal with a single file instead of many.

Helps with incremental backup strategies, archiving only the changes since the last backup.

tar Command Options

The tar command has several options for advanced file compression and decompression. Here are several essential tar options you can use:

-c: Creates an archive file.

-x: Extracts files from an archive file.

-z: Adds gzip compression when creating or extracting an archive file.

-j: Adds bzip2 compression when creating or extracting an archive file.

-t: Lists the contents of an archive file.

-u: Updates files in an existing archive file.

-r: Appends files to an archive file, similar to the -u option.

-A: Links two files into a single archive file.

The basic syntax of the tar command is as follows:

```

tar [opciones] [archivo-de-archivo] [archivo o carpeta a archivar]

```

tar Command Examples in Linux

In this section, you will learn some basic operations of the tar command. But before continuing, make sure your VPS Hosting is running properly.

Using the tar Command to Create an Archive File Creating an archive file for files and directories is simple. Here is an example of how to create an archive file:

```

tar -cvf archivo-de-archivo.tar archivo1 archivo2

```

This example uses the -cvf options:

-c: creates a new archive file.

-v: enables verbose mode, providing detailed output throughout the archiving process.

-f: specifies the name of the new archive file.

Replace archive-file.tar with the desired name for your archive file and file1 and file2 with the names of the files you want to include in the archive.

To perform recursive archiving of a directory and all its contents, use the following command:

```

tar -cvf archivo-de-archivo.tar directorio1/

```

Using the tar Command to Create a .tar.gz File

To minimize file sizes and optimize storage, create .tar.gz files using the tar command and gzip compression. Follow the command below to generate this type of archive file:

```

tar -czvf archivo-de-archivo.tar.gz archivo1 archivo2 directorio1

```

The -z option stands for gzip compression.

Alternatively, create a TGZ file as a tar.gz file. Here is the tar example in Linux:

```

tar -czvf archivo.tgz archivo1 archivo2 directorio1

```

Using the tar Command to Create a .tar.bz2 File

Use the BZ2 archive format for higher compression levels than gzip. However, this format takes longer to compress and decompress.

Here is how to create a .tar.bz2 file:

```

tar -cjvf archivo-de-archivo.tar.bz2 archivo1 archivo2 directorio1

```

Similarly, use .tar.tbz or .tar.tb2 as alternatives:

```

tar -czvf archivo-de-archivo.tar.tbz archivo1 archivo2 directorio1

tar -czvf archivo-de-archivo.tar.tb2 archivo1 archivo2 directorio1

```

Using the tar Command to Decompress .tar Files

The tar command can also be used to extract files. To extract files in the current directory, enter the following command:

```

tar -xvf archivo-de-archivo.tar

```

The -C option allows you to extract files to a specific directory. Here is an example:

```

tar -xvf archivo-de-archivo.tar -C /ruta/de/destino/

```

Remember to replace /destination/path/ with the full path of the desired folder

.

For .tar.gz files, use analogous commands:

```

tar -xvf archivo-de-archivo.tar.gz

tar -xvf archivo-de-archivo.tar.gz -C /ruta/de/destino/

```

The extraction process for .tar.bz2, .tar.tbz, or .tar.tb2 files follows the same principles:

```

tar -xjvf archivo-de-archivo.tar.bz2

tar -xjvf archivo-de-archivo.tar.tbz

tar -xjvf archivo-de-archivo.tar.tb2

```

Using the tar Command to List the Contents of an Archive File

Once you have created an archive file, you can list all the contents using the following command:

```

tar -tvf archivo-de-archivo.tar

```

It will display a complete list of files, along with timestamps and permissions.

For .tar.gz archive files, use the following command format:

```

tar -ztvf archivo-de-archivo.tar.gz

```

The command also extends to .tar.bz2 files:

```

tar -jtvf archivo-de-archivo.tar.bz2

```

Using the tar Command to Decompress a Single .tar File

You can also extract a single file from a compressed archive. Here is an example of how to do it:

```

tar -xvf archivo-de-archivo.tar archivo1.sh

```

In this case, archivo1.sh is a single file inside archive-file.tar that will be extracted.

Consider this command as an alternative:

```

tar -xvf archivo-de-archivo.tar --strip-components=1 archivo1.sh

```

Extracting a file from a .tar.gz archive follows a similar pattern:

```

tar -xzvf archivo-de-archivo.tar.gz archivo2.py

```

Here is another approach:

```

tar -xzvf archivo-de-archivo.tar.gz --strip-components=1 archivo2.py

```

For .tar.bz2 files, use one of these tar commands:

```

tar -xjvf archivo-de-archivo.tar.bz2 archivo3.cpp

tar -xjvf archivo-de-archivo.tar.bz2 --strip-components=1 archivo3.cpp

```

Using the tar Command to Extract Multiple Files from tar Archives

To extract several files from a tar archive, use the following command:

```

tar -xvf archivo-de-archivo.tar archivo1 archivo2 directorio1

```

The command above extracts file1, file2, and directory1 from archive-file.tar.

Take a similar approach to extract files from .tar.gz archives:

```

tar -xzvf archivo-de-archivo.tar.gz archivo3 archivo4 directorio2

```

Similarly, for .tar.bz2 files:

```

tar -xjvf archivo-de-archivo.tar.bz2 archivo5 archivo6 directorio3

```

Using the tar Command to Extract Multiple Files with a Pattern

You can use wildcards when extracting files with specific patterns. Here is an example of how to run this command:

```

tar -xvf archivo-de-archivo.tar '*.txt'

```

The command above only extracts TXT files from archive-file.tar.

The command is almost the same for .tar.gz archive files:

```

tar -xzvf archivo-de-archivo.tar.gz '*.txt'

```

Similarly, this is how to extract specific files from .tar.bz2 archives:

```

tar -xjvf archivo-de-archivo.tar.bz2 '*.txt'

```

Using the tar Command to Add Files to a tar Archive

The tar command also allows you to easily add files and directories to an existing archive file. To add files to a tar archive, enter the following command:

```

tar -rvf archivo-de-archivo.tar nuevoarchivo1 nuevoarchivo2

```

The -r option allows you to append files to an existing archive file, while -v enables verbose mode for detailed output.

You can also add directories to a tar archive using this command:

```

tar -rvf archivo-de-archivo.tar nuevodir1 nuevodir2

```

However, you cannot add new files or folders to .tar.gz or .tar.bz2 archives.

Using the tar Command to Verify an Archive File

You can use the tar command to verify compressed files. Here is an example command:

```

tar -tvf archivo-de-archivo.tar > /dev/null

```

The > /dev/null part directs any output to be discarded, creating a clean verification process.

This verification method does not apply to .tar.gz or .tar.bz2 files.

Using the tar Command to Check the Archive File Size

Checking the size of an archive file can be useful. The archive file size is typically displayed in kilobytes (KB).

Below, you will find tar command examples to check the sizes of different compressed archive files:

```

tar -czvf - archivo-de-archivo.tar | wc -c

tar -czvf - archivo-de-archivo.tar.gz | wc -c

tar -cjvf - archivo-de-archivo.tar.bz2 | wc -c

```

Using the tar Command to Create Backups with Pipes

You can combine the tar command with other commands to simplify complex tasks.

A useful tar command technique is backing up and restoring files using pipes.

For example, to compress and back up a directory called project into a .tar.gz archive file, enter:

```

tar -czvf - proyecto | gzip > respaldo_proyecto.tar.gz

```

The gzip command compresses the output and directs it to the project_backup.tar.gz file.

Conclusion

The tar command remains a vital tool for compressing and archiving files in Linux distributions. Its straightforward yet powerful capabilities allow you to optimize storage space, simplify file transfers, and create reliable backups.

By mastering practical applications of the tar command, you have acquired an essential skill to improve your Linux-based workflows.

Still have questions?

Come chat with us and we will get back to you as soon as possible!

Contact Support