How to Compress and Extract Files Using the tar Command on Linux
Last Updated :
04 Nov, 2025
Tar archives are special files that combine multiple files and directories into a single package, simplifying storage, transfer, and backup. Although tar itself doesn’t compress data, it can be paired with tools like gzip or bzip2 to create compressed and space-efficient archives.
- Tar stands for Tape Archive and is used to group files into a single archive file.
.tar Files are not compressed by default.- It preserves file permissions, timestamps, and directory structure.
- Common use cases include backup, file transfer, and software packaging.
Example: Create a tar archive (uncompressed)
Syntax:
tar -cvf backup.tar home/user/file name
Command:
tar -cvf backup.tar /home/vboxuser/gfg/myfile
Output:
- The command
tar -cvf backup.tar /home/vboxuser/gfg/myfile/ is used to create a tar archive named backup.tar containing all the contents of the myfile directory. - The
ls command confirms successful creation by listing backup.tar among other files and folders in the current direct
Syntax:
tar options [archive_name.tar] files_to_archive
- This syntax defines how the
tar command is used with specific options and file names. - The first part sets the action (like create or extract), followed by the desired archive name and the target files or directories.
Types of Tar Archives
Here’s the complete list of all common types of TAR archives with clear descriptions
Examples:
1. Create a Gzip Compressed Archive (.tar.gz)
tar -czvf archive.tar.gz /home/vboxuser/gfg/
- Uses gzip for fast compression (most common format).
- /home/vboxuser/gfg/ is a path to file
Output:
Note:
tar: Removing leading '/' from member names
is not an error, it's a normal informational message from tar when you create an archive from absolute paths (like /home/vboxuser/gfg/...).
- By default, tar strips the leading / from file paths for security reasons, this prevents overwriting system files if someone extracts the archive as root with absolute paths.
2. Create a TGZ Archive (.tgz)
tar -czvf archive.tgz /home/vboxuser/gfg/
- Same as
.tar.gz, just a shorter file extension often used in software packaging.
3. Create a Bzip2 Compressed Archive (.tar.bz2)
tar -cjvf archive.tar.bz2 /home/vboxuser/gfg/
- Uses bzip2 for better compression, but takes longer.
4. Create an XZ Compressed Archive (.tar.xz)
tar -cJvf archive.tar.xz /home/vboxuser/gfg/
- Uses xz for the highest compression ratio, ideal for minimizing size.
5. Create a Zstandard Compressed Archive (.tar.zst)
tar --zstd -cvf archive.tar.zst /path/to/files/
- Uses Zstandard (zstd) for modern, high-speed compression with excellent efficiency.
Key Tar Command Options and Their Uses
Below are some of the most commonly used tar command options and their corresponding full formats and descriptions:
| Option | Full format | Description |
|---|
| -a | --concatenate | Concentrate two archives |
| -c | --create | Creating a new archive |
| -d | --diff --delete | Showing the difference between archives Delete file from the archive |
| -r | --append | add files at the end of the existing archive |
| -t | --list | Show archive content |
| -u | --update | Update an archive |
| -x | --extract | Extract files from the archive |
Common Tar Command parameters
Here are some useful parameters that enhance the functionality of the tar command:
| Parameter | Full format | Description |
|---|
| -C dir | --directory=DIR | change directory before executing |
| -f | --file=ARCHIVE | Use specified archive file |
| -j | --bzip2 | compress using bzip2 |
| -p | --same-permissions | Save file permissions to file |
| -v | --verbose --total | Show process information Show final result |
| -z | --gzip | compress using gzip |
1. Compress one file using the tar command:
To compress a single file into a .tar.gz archive, use:
tar -czvf one-file-compressed.tar.gz hello_world

2. Compress directory using the tar command
To compress an entire directory, the following command is used:
tar -czvf dir-compressed.tar.gz test_directory/

3. Show the archive content
To see what's inside an archive without extracting it:
tar -tf archive.tar.gz

4. Add content to the existing archive
If you want to append more files or directories to an existing archive:
tar -rvf existing-archive-name.tar file-directory-to-compress/

5. Update content in an archive
To update files in an archive, use the update option '(-u)', which only adds files that are newer than the corresponding ones in the archive.

6. Compress with bzip2
To compress a file with bzip2, resulting in a .tar.bz2 file:
tar -cjvf one-file-compressed.tar.bz2 hello_world

Extracting files from a tar archive, regardless of the compression type (.tar, .tar.gz, .tar.bz2), can be done with:
tar -xf archive.tar.gz

The same with '.tar.gz' and '.tar.bz2'.
The 'tar' command is a powerful tool for managing files and directories in Linux and Unix environments. Its flexibility, combined with external compression utilities, makes it ideal for a wide range of archiving tasks. Understanding the various options and parameters can significantly simplify your data management workflows.
Compressing and Archiving Files in Linux
Explore
Getting Started with Linux
Installation with Linux
Linux Commands
Linux File System
Linux Kernel
Linux Networking Tools
Linux Process
Linux Firewall
Shell Scripting & Bash Scripting
Linux Administrator System