Dec 02, 2025
Ariffud M.
4min Read
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.
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:
The tar command follows simple syntax. Understanding each component is the first step to using it effectively.
tar [options] [archive-file] [file/directory...]
For example:
tar -cvf backup.tar /home/user/documents
This command creates an archive named backup.tar in the /home/user/documents directory.
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.
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.
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
Compression reduces the archive size. tar supports multiple compression algorithms:
tar -czvf website-backup.tar.gz file1.html file2.css images/
tar -cjvf website-backup.tar.bz2 file1.html file2.css images/
tar -cJvf website-backup.tar.xz file1.html file2.css images/
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.
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.
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
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
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
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
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
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
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.
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.

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.
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.
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 ;)