How to use the tar command in Linux

The tar command in Linux simplifies file management by enabling efficient archiving and compression.
Originally designed for tape archives, it has become an essential tool for grouping multiple files and directories into a single .tar file, often called a tarball.
With this command, you can create, extract, update, delete, and compress files – all directly from the command line.
Scroll down to learn the tar command’s syntax and the most common options. We’ll cover everything from creating and extracting Linux archive files to advanced tasks like updating and deleting their contents.
What is the tar command in Linux?
The tar (tape archive) command is a command-line utility to create and manage archive files. Its main function is to combine multiple files and directories into a single archive, making storage and transfer more efficient.
A tar archive is commonly used for:
- Backup. Automate daily server or website backups.
- Transfer. Bundle project files for easy sharing.
- Storage. Compress large directories to save disk space.
Tar command syntax
The tar command follows simple syntax. Understanding each component is the first step to using it effectively.
tar [options] [archive-file] [file/directory...]
- [options]. Single-letter flags that define the action, such as create (-c), extract (-x), or list (-t).
- [archive-file]. The name of the archive, for example backup.tar or project.tar.gz.
- [file/directory…]. The files or directories to include in the archive. You can list multiple items separated by spaces.
For example:
tar -cvf backup.tar /home/user/documents
This command creates an archive named backup.tar in the /home/user/documents directory.
Most common tar options
The tar command’s functionality is controlled by its options. While there are many options available, the most commonly used are listed below.
Option | Long option | Description |
-c | –create | Creates a new archive file. |
-x | –extract | Extracts files from an existing archive. |
-t | –list | Lists the contents of an archive without extracting them. |
-f | –file | Specifies the name of the archive file. This is almost always required. |
-v | –verbose | Displays a detailed list of the files being processed. |
-z | –gzip | Compresses the archive using gzip, creating a .tar.gz file. |
-j | –bzip | Compresses the archive using bzip2, creating a .tar.bz2 file. |
-J | –xz | Compresses the archive using xz, creating a .tar.xz file. |
-r | –append | Adds new files to the end of an existing archive. |
-u | –update | Adds files to an archive only if they are newer than the existing ones. |
–delete | Deletes files from an archive. Note: This doesn’t work on tape drives. |
You can combine multiple options in one command like this:
tar -czvf project.tar.gz project/
This creates a gzip-compressed archive of the project/ directory and displays the files being added.
How to create archives using tar
The tar command uses the -c option to create new archive files. This is particularly useful for creating a single backup file of your website’s project directory before making any changes.
Create uncompressed archives
To create a plain archive without compression (simply bundling files together):
Command:
tar -cvf website-backup.tar file1.html file2.css images/
Expected output:
file1.html file2.css images/ images/logo.png images/banner.jpg
Create compressed archives (gzip, bzip2, xz)
Compression reduces the archive size. tar supports multiple compression algorithms:
- gzip (-z). Fast and widely used.
tar -czvf website-backup.tar.gz file1.html file2.css images/
- bzip2 (-j). Offers better compression than gzip but is slower.
tar -cjvf website-backup.tar.bz2 file1.html file2.css images/
- xz (-J). Provides the highest compression ratio, but is also the slowest.
tar -cJvf website-backup.tar.xz file1.html file2.css images/
💡 When to use gzip vs bzip2 vs xz
Use gzip for quick, everyday tasks, bzip2 when you need a balance of speed and compression, and xz for long-term storage where maximum space savings are important.
How to extract archives with tar
To extract a tar archive in Linux, use the -x option. This unpacks the contents, restoring the original files and directory structure – a crucial step when deploying a web application from a backup.
Extract to current directory
To unpack an archive into your current directory:
Command:
tar -xvf website-backup.tar.gz
Expected output:
file1.html file2.css images/ images/logo.png images/banner.jpg
Extract to a custom directory
To extract the archive’s contents into a specific directory, use the -C option.
Command:
tar -xzvf website-backup.tar.gz -C /var/www/html
How to add files to existing tar archives
You can add files to an existing archive using the -r option. This is useful for adding new log files to a backup without recreating the entire archive. Note that you cannot add files to compressed archives, such as .tar.gz.
Command:
tar -rvf website-backup.tar file3.js fonts/
Expected output:
file3.js fonts/ fonts/roboto.woff
How to list tar archive contents
Use the -t option to view files inside an archive without extracting them. This is perfect for verifying what’s inside a backup before you restore it.
Command:
tar -tvf website-backup.tar.gz
Expected output:
-rw-r--r-- user/group 1234 2025-08-21 10:30 file1.html -rw-r--r-- user/group 5678 2025-08-21 10:30 file2.css drwxr-xr-x user/group 0 2025-08-21 10:30 images/ -rw-r--r-- user/group 15200 2025-08-21 10:30 images/logo.png -rw-r--r-- user/group 45800 2025-08-21 10:30 images/banner.jpg
How to extract specific files using tar
You don’t have to extract everything. tar can restore selected files, saving time and disk space if you only need one or two files from a large backup.
Command:
tar -xzvf website-backup.tar.gz file1.html images/logo.png
How to update existing tar archives
Use the -u option to replace files inside an archive with newer versions. tar checks the file’s modification timestamp and updates it only if the local version is newer than the one in the archive.
Command:
tar -uvf archive.tar updatedfile.txt
How to concatenate multiple tar archives
Concatenating lets you merge several tar files into one using the -A (–catenate) option. This is helpful for merging weekly backups into a single monthly archive.
Command:
tar -Af main-archive.tar week2.tar week3.tar
This appends the contents of week2.tar and week3.tar into main-archive.tar.
Important! You cannot directly merge compressed formats like .tar.gz or .tar.xz with .tar.
How to delete files from tar archives
The tar command can also remove files or entire directories from an uncompressed archive with the –delete option.
Command:
tar --delete -f website-backup.tar images/banner.jpg tar --delete -f website-backup.tar unused-directory/
Note that the –delete option may not be available in older versions of tar. In that instance, you’ll have to extract the archive, remove the unwanted files manually, and then recreate it.
Check our guide on how to remove a directory in Linux, which provides alternative methods for deleting both empty and non-empty directories.
Key takeaways about the tar command
Mastering the tar command is a valuable skill for anyone working in a Linux environment. It provides a powerful and flexible way to manage files efficiently.
- Flexible archiving. The primary strength of tar is its ability to bundle countless files and directories into a single, portable archive file.
- Built-in compression. With support for gzip, bzip2, and xz, tar lets you significantly reduce archive size, saving valuable disk space.
- Efficient file management. From creating backups to packaging project files for sharing, tar streamlines system administration tasks and keeps your workflow organized.
As a next step, practice these command examples in a safe directory. Try creating different types of compressed archives and inspect their contents.
For more advanced options and edge cases, consult the official manual page by running man tar in your terminal.
We also suggest exploring other Linux commands like cp (for copying files) and rsync (for synchronizing backups) to enhance your system administration skills.
All of the tutorial content on this website is subject to Hostinger's rigorous editorial standards and values.
Comments
October 05 2024
This example is wrong as in most places: "tar -xvf example.tar -C destination_directory/" The -C flag is order-sensitive so that would be the correct order: "tar -C destination_directory/ -xvf example.tar" -C, --directory=DIR Change to DIR before performing any operations. This option is order-sensitive, i.e., it affects all options that follow.
October 11 2024
Thanks for spotting that! We’ve made the update to our article ;)