How to use the tar command for archiving and compressing files in Linux

How to use the tar command for archiving and compressing files in Linux

tar stands for tape archive and is a widely used Linux command-line utility for archiving and compressing files. Developed initially to back up data to tape drives, the tar command has evolved significantly over the years.

Now, its primary purpose is to combine multiple files and directories into a single archive file, simplifying storage and transfer. Additionally, the Linux tar command can compress these archives to save disk space.

In this article, we’ll explore the syntax, options, and practical examples of this command. By the end of this guide, you can effectively use tar in Linux systems, making your data management tasks more efficient and organized.

Syntax of the tar command

The basic structure of the tar command is as follows:

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

Here’s a detailed breakdown of each component:

  • [options]. Flags that modify the command’s behavior. These options can specify actions like creating, extracting, or listing the contents of an archive. They also include compression options and verbose output settings.
  • [archive-file]. The resulting archive file’s name. This is where the files and directories specified will be stored. The archive file can have various extensions depending on the compression used, such as .tar, .tar.gz, or .tar.bz2.
  • [file or directory to be archived]. The files or directories to be included in the archive. You can specify multiple file names and directories, which will be combined into a single archive file.

Below is a tar command syntax example that demonstrates how these components work together:

tar -cvf archive_name.tar /path/to/directory_or_file

-cvf are the options used, archive_name.tar is the tar archive file name, and /path/to/directory_or_file specifies the files or directories to be archived.

Suggested Reading

New to Linux? Learn the most used Linux commands to enhance your command-line skills and streamline your workflow.

Commonly used options

Here are some of the most commonly used tar options to perform different tasks:

  • Create (-c). Create a new archive.
  • Extract (-x). Extract files from an archive.
  • Verbose (-v). Show the detailed output of the process.
  • File (-f). Specify the archive file.
  • Compress with gzip (-z). The archive will be compressed using gzip, resulting in a .tar.gz file.
  • Compress with bzip2 (-j). Compress the archive using bzip2, resulting in a .tar.bz2 file.
  • Compress with xz (-J). Compress the archive using xz, resulting in a .tar.xz file.

You can also combine options in one command to perform more complex tasks. For example, to create a gzip-compressed file archive and show detailed output, use:

tar -czvf archive_name.tar.gz /path/to/directory_or_file

Examples of the tar command

Here are some practical tar command examples to help you understand this versatile tool. But before proceeding, make sure to prepare a Linux-based device. If you don’t have one, you can purchase a Linux VPS from Hostinger and get it running correctly.

Creating archives

You can create a tar archive to combine multiple files and directories into a single archive file. Here’s an example of creating an archive named example.tar containing file1.txt, file2.txt, and directory1:

tar -cvf example.tar file1.txt file2.txt directory1

You should see an output similar to the following:

file1.txt

file2.txt

directory1/

directory1/file_in_directory.txt

Meanwhile, to create a bzip2-compressed archive named example.tar.bz2, use the command below:

tar -cvjf example.tar.bz2 file1.txt file2.txt

If you want to create an archive containing all files and subdirectories within my_directory but exclude all .log files, run:

tar -cvf example.tar --exclude='*.log' my_directory/

This command uses –exclude=’pattern’ to exclude files matching the pattern from the archive.

Extracting archives

Extracting an archive allows you to retrieve the files and directories that were combined into a single file. Enter the following command to extract a tar file named example.tar to the current directory:

tar -xvf example.tar

Here’s the expected output listing all the files and directories being extracted:

image1.jpg

image2.jpg

directory1.jpg

directory1/image_in_directory.jpg

To extract the contents of example.tar to a different directory named destination_directory, type the following:

tar -xvf example.tar -C destination_directory/

This command uses the -C option to specify the directory where the files will be extracted.

Adding files to an existing archive

You can add files to an existing archive file without creating a new one. The following command adds file3.txt, file4.txt, and directory2 to the example.tar archive:

tar -rvf example.tar file3.txt file4.txt directory2

It uses the -r option to append files and directories to the end of an archive.

When executed, an output indicating the files being added to the archive would appear:

file3.txt

file4.txt

directory2/

directory2/file_in_directory2.txt

Listing the contents of an archive

Listing the contents of an archive lets you see what files and directories are stored within it. To do so, use the -t option like in the following example:

tar -tvf example.tar

Here’s the expected output:

-rw-r--r-- user/group    12345 2024-07-04 12:34 file1.txt

-rw-r--r-- user/group    67890 2024-07-04 12:34 file2.txt

drwxr-xr-x user/group     4096 2024-07-04 12:34 directory1/

-rw-r--r-- user/group    13579 2024-07-04 12:34 directory1/file_in_directory.txt

Please note that to list the contents of a compressed archive, you need to use additional options. For instance, for gzip-compressed archives, append the -z option, and for bzip2-compressed ones, use -j:

tar -tvzf example.tar.gz

tar -tvjf example.tar.bz2

Extracting specific files from an archive

With tar, you can also extract specific files from an archive file without extracting the entire archive. For example, to extract a file named file1.txt from example.tar:

tar -xvf example.tar file1.txt

If you want to extract multiple files from an archive, list their names in your command like the following:

tar -xvf example.tar file1.txt file2.txt image1.jpg

Updating archives

Use the -u option to update the contents of an existing tar archive by adding newer versions of files.

However, the file names should be the same as the older ones in the archive so the update works correctly. Otherwise, tar will add them as new files instead.

Here’s a command example you can run:

tar -uvf example.tar file1.txt file2.txt

Concatenating archives

Concatenating archives lets you combine multiple tar archives into a single archive. It’s helpful in consolidating backups or organizing files further. The first archive name listed will be the main single archive file.

To concatenate archive2.tar into archive1.tar, type:

tar -Af archive1.tar archive2.tar

This command employs the -A option to append tar files to an archive.

To concatenate multiple archives, simply list them:

tar -Af archive1.tar archive2.tar archive3.tar

When executed, tar appends the contents of archive2.tar and archive3.tar to archive1.tar.

It’s important to note that you can’t concatenate archives of different formats, such as .tar and .tar.gz, directly with the tar command.

Deleting members from an archive

Last but not least, you can use the tar command to remove specific files or directories without having to recreate the entire archive. Here’s an example of deleting a file named file1.txt from example.tar:

tar --delete -f example.tar file1.txt

The above command uses –delete to remove specified files from the archive.

To delete a directory named dir1 and all its contents from the archive, execute:

tar --delete -f example.tar dir1/

Please note that the –delete option may not be available in some older versions of tar or BSD-based systems. In such cases, you need to extract the archive, remove the unwanted files, and create a new archive.

Conclusion

In this article, you’ve learned how to use the tar command for different file archiving tasks, including creating, extracting, and deleting files from an archive. You can also employ this command to compress folders in various formats, such as gzip and bzip2.

Mastering the tar command is invaluable for backups and system administration. It enhances file management capabilities, streamlines tasks, and improves organization in Linux environments.

tar command FAQ

This section answers the most common questions about the tar command.

What is the tar command used for in Linux?

The tar command in Linux creates, maintains, modifies, and extracts archive files. It combines multiple files into a single file for easier distribution or backup and supports various compression formats.

How can I create a gzip-compressed archive?

To create a gzip-compressed archive, append the -cvzf options in your tar command, like the following:
tar -cvzf archive_name.tar.gz file1 file2
It compresses file1 and file2 into archive_name.tar.gz.

What is the difference between the -z, -j, and -J options?

The -z option compresses archives using gzip, -j uses bzip2, and -J uses xz compression. Each offers different compression ratios and speeds, with gzip being the fastest and xz providing the highest compression.

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.