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

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

tar, short for tape archive, is a Linux command for archiving and compressing files and folders. You must learn how to use the tar command to efficiently manage Linux files.

This article will guide you through practical applications of the Linux tar command. We’ll explore its various options and provide you with tar usage examples.

What Is the tar Command?

The tar command allows you to bundle multiple files and directories into a single archive, making data storage and transfer effortless. Typically, the outcome of a tar command is a TAR file. If you use gzip for further compression, the result becomes a .tar.gz file.

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 the disk space when combined with tools like gzip or bzip2 for different compression formats.
  • Creates backups of entire directories to preserve and easily recover data.
  • Simplifies file transfer between systems, as 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 various options for advanced file compression and decompression. Here are several essential tar options you can use:

  • -c – Create an archive file.
  • -x – Extracts files from an archive.
  • -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 – Update files to an existing archive.
  • -r – Append files to an archive, similar to the -u option.
  • -A – Link two archives into a single archive.

The basic tar command syntax is as follows:

tar [options] [archive-file] [file or folder to be archived]

tar Command Examples in Linux

In this section, you’ll learn some basic tar command operations. But before we proceed, make sure your VPS Hosting is running properly.

Using tar Command to Create an Archive File

Creating a tar archive for files and directories is simple. Here’s an example of how to create an archive:

tar -cvf archive.tar file1 file2

This example employs the -cvf options:

  • -c – creates a new archive.
  • -v – activates verbose mode, providing detailed output throughout the archiving process.
  • -f – specifies the name of the new archive file.

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

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

tar -cvf archive.tar directory1/

Using tar Command to Create a .tar.gz File

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

tar -czvf archive.tar.gz file1 file2 directory1

The -z option signifies gzip compression.

Alternatively, create a TGZ file like a tar.gz archive. Here is the Linux tar example:

tar -czvf archive.tgz file1 file2 directory1

Using tar Command to Create a .tar.bz2 File

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

Here’s how to create a .tar.bz2 archive:

tar -cjvf archive.tar.bz2 file1 file2 directory1

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

tar -czvf archive.tar.tbz file1 file2 directory1
tar -czvf archive.tar.tb2 file1 file2 directory1

Using tar Command to Unzip .tar Files

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

tar -xvf archive.tar

The -C option lets you extract files into a specific directory. Here’s an example:

tar -xvf archive.tar -C /path/to/destination/

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

For .tar.gz files, employ analogous commands:

tar -xvf archive.tar.gz
tar -xvf archive.tar.gz -C /path/to/destination/

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

tar -xjvf archive.tar.bz2
tar -xjvf archive.tar.tbz
tar -xjvf archive.tar.tb2

Using tar Command to List the Contents of an Archive File

Once you’ve made an archive, you can list all the contents by employing the following command:

tar -tvf archive.tar

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

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

tar -ztvf archive.tar.gz

The command also extends to .tar.bz2 files:

tar -jtvf archive.tar.bz2

Using tar Command to Unzip a Single .tar File

You can also extract one file from a compressed archive. Here’s an example of how to do it:

tar -xvf archive.tar file1.sh

In this instance, file1.sh is a single file within archive.tar that will be extracted.

Consider this command for an alternative:

tar -xvf archive.tar --strip-components=1 file1.sh

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

tar -xzvf archive.tar.gz file2.py

Here’s another approach:

tar -xzvf archive.tar.gz --strip-components=1 file2.py

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

tar -xjvf archive.tar.bz2 file3.cpp
tar -xjvf archive.tar.bz2 --strip-components=1 file3.cpp

Using tar Command to Extract Multiple Files from tar Archives

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

tar -xvf archive.tar file1 file2 directory1

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

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

tar -xzvf archive.tar.gz file3 file4 directory2

Likewise, for .tar.bz2 archives:

tar -xjvf archive.tar.bz2 file5 file6 directory3

Using tar Command to Extract Multiple Files With a Pattern

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

tar -xvf archive.tar '*.txt'

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

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

tar -xzvf archive.tar.gz '*.txt'

Similarly, here’s how to extract specific files from .tar.bz2 archives:

tar -xjvf archive.tar.bz2 '*.txt'

Using tar Command to Add Files to a tar Archive

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

tar -rvf archive.tar newfile1 newfile2

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

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

tar -rvf archive.tar newdir1 newdir2

However, you can’t add new files or folders to .tar.gz or .tar.bz2 files.

Using tar Command to Verify a tar Archive File

You can employ the tar command to verify compressed archives. Here’s a command example:

tar -tvf archive.tar > /dev/null

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

This verification method doesn’t apply to .tar.gz or .tar.bz2 files.

Using tar Command to Check the Archive Size

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

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

tar -czvf - archive.tar | wc -c
tar -czvf - archive.tar.gz | wc -c
tar -cjvf - archive.tar.bz2 | wc -c

Using tar Command to Create Backups With Pipes

You can combine the tar command with other commands to simplify complex tasks. One useful tar command technique is to back up and restore files using pipes.

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

tar -czvf - project | gzip > project_backup.tar.gz

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

Conclusion

The tar command remains a vital tool for compressing and archiving files in Linux distributions. Its straightforward yet powerful capabilities let you optimize storage space, simplify file transfers, and improve overall data organization.

By mastering practical applications of the tar command, you’ve gained an essential skill to enhance your Linux-based workflows.

tar Command FAQ

This section will answer the most common questions about the tar command.

Why Use the tar Command in Linux?

The Linux tar command is used to archive and compress files. It lets you consolidate multiple files and folders into a single archive file while preserving the directory structure. This command improves storage efficiency, simplifies data management, and facilitates seamless file transfer.

Can I Use the tar Command to Create Multi-Volume Archives?

Yes, you can use tar to split large compressed archives into multiple volumes. This is especially useful when dealing with storage limitations. By using the -M option, you can create multi-volume tar archives and ensure seamless data distribution across multiple media or systems.

Can I Use the tar Command With Other Linux Commands?

Absolutely. The tar command seamlessly integrates with other Linux commands. You can use pipes (|) to direct the tar output into other commands, like gzip for compression or scp for remote transfers. This flexibility empowers you to enhance your management of archive files, backups, and distribution tasks.

Author
The author

Ariffud Muhammad

Ariffud is a Technical Content Writer with an educational background in Informatics. He has extensive expertise in Linux and VPS, authoring over 200 articles on server management and web development. Follow him on LinkedIn.