Learning Center
Linux

How to Compress Files in Linux Using tar Command

12 Mar 2025
Saryia Saif
Saryia Saif

The tar command basically functions to put together all files and directories into one archive without altering their structure. The approach simplifies organization, creation of the backup, and the transfer of files. Once packaged, you can compress these archives by using multiple ways such as using gzip, bzip2, or xz, which help optimize storage and enhance transfer speeds.

Modern Linux distributions on Linux VPS Servers come with updated versions of tar, enabling seamless integration with compression tools like gzip for more efficient data handling. This makes tar a valuable asset for users managing large datasets, as it supports both file consolidation and compression in a single command.

Thanks to its flexibility, tar is widely used across different Linux environments. It not only facilitates backup creation but also streamlines software distribution and the management of the important data. With an array of choices available, all users can customize archives according to their requirements, whether by excluding particular directories or files, preserving permissions, or securing sensitive data.

For anyone dealing with extensive information or complex storage requirements, learning everything about the tar command is crucial. This all makes it an important utility to learn for Linux users.

And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS.

Understand the Syntax of tar 
Copy link

The tar command is fundamentally divided into four distinct parts:

  1. tar (keyword)
  2. -flags (options), used to execute a specific action
  3. name of the archive
  4. path to the desired file or directory

It would be written as follows:

tar -flags (archive_name) (path)

Archiving Files and Directories
Copy link

tar used with the flag -cvf has the power to essentially archive the files and also the directories.

For a File:

tar -cvf collectionX.tar snake.txt

Image1

For a Directory:

tar -cvf DRcollection.tar newDir/

Image3

This would essentially archive the file snake.txt to collectionX.tar and the directory newDir to DRcollection.tar respectively. 

If desired outcome is to archive multiple files and directories, then use the following commands.

For Multiple Files:

tar -cvf collectionX.tar snake.txt panther.txt Tiger.txt

Image2

For Multiple Directories:

tar -cvf DRcollection.tar newDir1/ newDir2/ newDir3/

Image5

Compressing Files and Directories
Copy link

tar used with the flag -czvf has the power to compress the files as well as the directories:

For a File:

tar -czvf collectionX.tar.gz snake.txt

Image4

For a Directory: 

tar -czvf DRcollection.tar.gz newDir/

Image7

-c archives the directories and files, -z pushes for gzip compression, -v is verbose which essentially shows what’s going on with compression, and -f allows to name the archive that is going to be compressed.

Add .gz after tar, if you want to compress the files and directories.

For Multiple Files:

tar -cvf collectionX.tar.gz snake.txt panther.txt Tiger.txt 

Image6

For Multiple Directories:

tar -cvf DRcollection.tar.gz newDir1/ newDir2/ newDir3/

Image9

.bz2 used with tar and both used with -cjf allow to archive and compress files and directories. -j applies bzip2 compression.

For a File (with bz2):

tar -cjf collectionX.tar.bz2 snake.txt

Image8

For a Directory (with bz2):

tar -cjf DRcollection.tar.bz2 newDir/

Image12

.xz used with .tar and both used with -cJf allow you to archive and compress files and directories. In -cJf, -J means compress with xz.

For a File (with xz):

tar -cJf DRcollection.tar.xz file1.txt

Image10

For a Directory (with xz):

tar -cJf collectionX.tar.xz newDir/

Image11

Extracting Compressed .tar Files
Copy link

arch1.tar.gz, arch1.tar.bz2 and arch1.tar.xz are three compressed files.

Extract tar.gz:

tar -xvzf arch1.tar.gz

-x stands for file extraction.

Extract tar.bz2:

tar -xvjf arch1.tar.bz2

Extract tar.xz:

tar -xvJf arch1.tar.xz

Image13

Extracting Specific Files Using Wildcards

If you need to extract only a specific type of file out of an archive, do this:

tar -xvf arch1.tar --wildcards '*.sh'

Image14

It will give you only the files with .sh extension.

--wildcards help search that specific type of file and enable pattern matching while *.sh ensures that you only extract the .sh type of files.

Extracting to a Specific Directory

If you need to extract the complete archive to a specific directory, do this:

tar -xvf arch1.tar -C ./destinationDir/pathDir/

Image15

-C changes to the specified directory path and -xvf helps extract the archive there. 

Managing .tar Archives
Copy link

Check Contents without Extracting

If you need to know what's inside an archive but don't want to uncompress files, use commands like this:

tar -tzf arch1.tar.gz
tar -tjf arch1.tar.bz2
tar -tJf arch1.tar.xz

-t gives details about what’s inside the compressed archives without performing extraction on it.

Image16

Appending Files to an Existing Archive

To append a new file to an archive:

tar -rvf arch1.tar new.sh

Image17

new.sh will be added to arch1.tar. That’s how you append a file into an existing archive. 

Removing a Specific File from an Archive

What if you need to delete a file from an archive without having to extract it, it can be done by using --delete.

tar --delete -f arch1.tar new.sh 

This will remove the file new.sh from the archive arch1.tar without extracting it. 

Note that --delete does not work on the compressed files, only on archives. 

Comparing Archive Contents with Current Directory

If you have to examine the contents of your current working directory and compare them with the archive? use:

tar --diff -f arch1.tar

--diff will help compare the contents of arch1.tar with the content available in the present working directory.

Troubleshooting Common .tar Errors
Copy link

  • "tar: Removing leading '/' from member names"

This warning appears when absolute paths are used in an archive:

tar -cvf arch1.tar /home/user/file.txt

Solution: Use -p to preserve absolute paths.

tar -cvpf arch1.tar /home/user/file.txt
  • "tar: Error opening archive: Unrecognized archive format"

This error occurs when the archive is corrupt or the wrong decompression command is used.

Solution: Verify the file type:

file arch1.tar.gz

Use the correct decompression command:

tar -xvzf arch1.tar.gz  # For .tar.gz
tar -xvjf arch1.tar.bz2  # For .tar.bz2
tar -xvJf arch1.tar.xz   # For .tar.xz

If corruption is suspected, check integrity:

gzip -t arch1.tar.gz
bzip2 -tv arch1.tar.bz2

Conclusion
Copy link

The tar utility serves as an important tool for archiving, compression and extraction. It provides efficiency, making it a crucial component of Linux storage management. With a variety of configurations and settings, tar functions as an evergreen solution catering to diverse use scenarios. Options such as -czvf and -xvzf determine the way files are stored and retrieved, granting users complete control over data compression.

Furthermore, tar supports multiple compression tools like gzip, bzip2, and xz, allowing users to optimize both speed and compression ratio based on their specific needs.

For Information Technology professionals, developers, and Linux users, learning everything about tar is invaluable. Whether it’s for managing backups, distribution of data effectively, or optimizing storage, tar is by far one of the most influential archiving tools. By selecting the right configurations and commands, users can significantly enhance their workflow, automate tasks, and efficiently handle large datasets.

You can also use our S3 compatible storage to save all the data you need for your apps and servers.