Linux tutorial on the tar command, covering basic and advanced file archiving and extraction with practical examples.
last modified March 3, 2025
The tar command in Linux is used to create, extract, and manage archive files. It is a powerful tool for bundling multiple files and directories into a single archive file, often compressed with gzip or bzip2. This tutorial covers basic and advanced usage of tar with practical examples.
tar is commonly used for backups, file distribution, and compression. It supports various compression formats like .tar.gz, .tar.bz2, and .tar.xz.
This example demonstrates how to create a tar archive of a directory.
tar -cvf archive.tar directory/
The -c option creates a new archive, -v enables verbose output, and -f specifies the archive file name.
This example shows how to extract files from a tar archive.
tar -xvf archive.tar
The -x option extracts files, -v enables verbose output, and -f specifies the archive file name.
This example demonstrates how to create a compressed tar archive using gzip.
tar -czvf archive.tar.gz directory/
The -z option compresses the archive using gzip.
This example shows how to extract files from a gzip-compressed tar archive.
tar -xzvf archive.tar.gz
The -z option decompresses the archive using gzip.
This example demonstrates how to create a compressed tar archive using bzip2.
tar -cjvf archive.tar.bz2 directory/
The -j option compresses the archive using bzip2.
This example shows how to extract files from a bzip2-compressed tar archive.
tar -xjvf archive.tar.bz2
The -j option decompresses the archive using bzip2.
This example demonstrates how to list the contents of a tar archive.
tar -tvf archive.tar
The -t option lists the contents of the archive.
This example shows how to extract specific files from a tar archive.
tar -xvf archive.tar file1.txt file2.txt
Specify the file names after the archive name to extract only those files.
This example demonstrates how to add files to an existing tar archive.
tar -rvf archive.tar newfile.txt
The -r option appends files to the archive.
Use Compression: Use -z or -j for smaller archive sizes.
Verify Archives: Use -t to list contents before extraction.
Use Verbose Mode: Use -v for detailed output during operations.
Backup Regularly: Use tar for regular backups of important data.
In this article, we have explored various examples of using the tar command for creating, extracting, and managing archive files, including compression and advanced features.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Linux tutorials.