Open In App

How to Compress Files in Linux | Tar Command

File compression is a fundamental task in managing and transferring data efficiently on a Linux system. The Tar command, short for Tape Archive, is a powerful tool that allows users to create compressed and archived files. In this comprehensive guide, we will explore the various options and examples of using the Tar command to compress files on a Linux system.

tar Command to Compress Files in Linux

The Linux ‘tar’ stands for tape archive, which is used to create Archive and extract the Archive files. tar command in Linux is one of the important commands that provides archiving functionality in Linux. We can use the Linux tar command to create compressed or uncompressed Archive files and also maintain and modify them. 

Syntax of `tar` command in Linux

tar [options] [archive-file] [file or directory to be archived]

Here ,

An Archive file is a file that is composed of one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easier portability and storage, or simply to compress files to use less storage space. 




Creates an archive by bundling files and directories together.


Extracts files and directories from an existing archive.


Specifies the filename of the archive to be created or extracted.


Displays or lists the files and directories contained within an archive.


Archives and adds new files or directories to an existing archive.


Displays verbose information, providing detailed output during the archiving or extraction process.


Concatenates multiple archive files into a single archive.


Uses gzip compression when creating a tar file, resulting in a compressed archive with the ‘.tar.gz’ extension.


Uses bzip2 compression when creating a tar file, resulting in a compressed archive with the ‘.tar.bz2’ extension.


Verifies the integrity of an archive file, ensuring its contents are not corrupted.


Updates or adds files or directories to an already existing archive without recreating the entire archive.

Examples of tar Command to Compress Files in Linux

1. Creating an uncompressed tar Archive using option -cvf

This command creates a tar file called file.tar which is the Archive of all .c files in the current directory. 

tar cvf file.tar *.c

Output : 


2. Extracting files from Archive using option -xvf

This command extracts files from Archives. 

tar xvf file.tar

Output :  


3. gzip compression on the tar Archive, using option -z

This command creates a tar file called file.tar.gz which is the Archive of .c files.  

tar cvzf file.tar.gz *.c

4. Extracting a gzip tar Archive *.tar.gz using option -xvzf :

This command extracts files from tar archived file.tar.gz files.  

tar xvzf file.tar.gz

5. Creating compressed tar archive file in Linux using option -j

This command compresses and creates archive files less than the size of the gzip. Both compress and decompress take more time than gzip.  

tar cvfj file.tar.tbz example.cpp


tar cvfj file.tar.tbz example.cpp

tar tvf file.tar.tbz
-rwxrwxrwx root/root 94 2017-09-17 02:47 example.cpp

6. Untar single tar file or specified directory in Linux:

This command will Untar a file in current directory or in a specified directory using -C option.  

tar xvfj file.tar 
tar xvfj file.tar -C path of file in directory

7. Untar multiple .tar, .tar.gz, .tar.tbz file in Linux:

This command will extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example, the above command will extract “fileA” “fileB” from the archive files.  

tar xvf file.tar "fileA" "fileB" 
tar zxvf file1.tar.gz "fileA" "fileB"
tar jxvf file2.tar.tbz "fileA" "fileB"

8. Check size of existing tar, tar.gz, tar.tbz file in Linux:

The above command will display the size of archive file in Kilobytes (KB).  

tar czf file.tar | wc -c
tar czf file1.tar.gz | wc -c
tar czf file2.tar.tbz | wc -c

9. Update existing tar file in Linux  

tar rvf file.tar *.c



10. List the contents and specify the tarfile using option -tf

This command will list the entire list of archived files. We can also list for specific content in a tarfile  

tar tf file.tar



11. Applying pipe to through ‘grep command’ to find what we are looking for

This command will list only for the mentioned text or image in grep from archived file.  

tar tvf file.tar | grep "text to find" 
tar tvf file.tar | grep "filename.file extension"

12. We can pass a file name as an argument to search a tarfile:

This command views the archived files along with their details.  

tar tvf file.tar filename 

13. Viewing the Archive using option -tvf  

tar tvf file.tar


-rwxrwxrwx root/root       191 2017-09-17 02:20 os2.c
-rwxrwxrwx root/root 218 2017-09-17 02:20 os3.c
-rwxrwxrwx root/root 493 2017-09-17 02:20 os4.c

What are wildcards in Linux 

Alternatively referred to as a ‘wild character’ or ‘wildcard character’, a wildcard is a symbol used to replace or represent one or more characters. Wildcards are typically either an asterisk (*), which represents one or more characters or question mark (?), which represents a single character. 

14. To search for an image in .png format:

This will extract only files with the extension .png from the archive file.tar. The -wildcards option tells tar to interpret wildcards in the name of the files to be extracted; the filename (*.png) is enclosed in single-quotes to protect the wildcard (*) from being expanded incorrectly by the shell. 

$ tar tvf file.tar --wildcards '*.png' 

Note: In the above commands ” * ” is used in place of file name to take all the files present in that particular directory.  

Zip Command to Compress Files in Linux

In the realm of Linux file compression, the `zip` command emerges as a powerful and user-friendly tool. Distinct from the `tar` command, `zip` specializes in creating compressed files while preserving the integrity of the original content. The straightforward syntax of the `zip` command simplifies the compression process:

Basic Syntax:

zip [options] zipfile files/directories
zip ./ bigfile

This single line of code encapsulates the essence of the `zip` command, compressing the file `bigfile` and generating a new file, ``, which holds the compressed version of the original content. Notably, the original file remains untouched, ensuring data safety during the compression process.

The user-friendly nature of the `zip` command makes it an accessible option for those seeking a straightforward solution for creating compressed archives. Whether you’re compressing a single file or multiple files, the simplicity and efficiency of the` zip` command contribute to its popularity among Linux users. As a versatile tool in the compression toolkit, `zip` offers a seamless balance between ease of use and effective file compression.

Gzip Command to Compress Files in Linux

In the landscape of Linux compression utilities, the `gzip` command stands out as a straightforward and highly efficient tool. Its usage is elegantly simple—provide the filename, and `gzip` seamlessly compresses it in place. This can be achieved with a single command

Basic Syntax:

gzip [options] filename
gzip bigfile

Unlike certain compression commands that generate a separate compressed file, `gzip` operates “in place.” In other words, it encrypts the original file, replacing it with the compressed version. This intrinsic simplicity is a key attribute that positions `gzip` as an excellent choice for users seeking a rapid and effective file compression solution.

The in-place compression feature not only reduces the need for managing multiple files but also ensures a smooth integration into various workflows. This makes `gzip` particularly well-suited for scenarios where simplicity and speed are paramount. Whether you are compressing a single file or multiple files, the straightforward nature of the `gzip` command contributes to its popularity among Linux users, making it a reliable and efficient compression tool in diverse contexts.

Bzip2 Command to Compress Files in Linux

In the intricate landscape of Linux file compression, the `bzip2` command presents itself as a potent and versatile tool, bearing similarities to its counterpart, `gzip`. Functioning in a manner akin to `gzip`, the `bzip2` command compresses files in place, preserving only the original file. The simplicity of its application is illustrated by the following example:

Basic Syntax :

bzip2 [options] filename

Here, `options` represent any additional flags or settings you may want to apply, and `filename` is the name of the file you wish to compress. The basic command without options compresses the file in place, replacing the original file with the compressed version.

bzip2 bigfile

This concise command initiates the compression process, resulting in the creation of `bigfile.bz2`—the compressed iteration of the original file. Much like the decision between `gzip` and `bzip2` often depends on specific use cases, the choice between these two commands is nuanced and influenced by factors such as the nature of the data being compressed and the desired compression ratio.

While both commands share the fundamental concept of in-place compression, `bzip2` is known for its distinctive compression algorithm, which often achieves higher compression ratios compared to `gzip`. Consequently, users may opt for `bzip2` when prioritizing file size reduction and are willing to trade off some compression speed. This nuanced decision-making process underscores the flexibility of the `bzip2` command in catering to diverse compression requirements within the Linux ecosystem.

XZ Command to Compress Files in Linux

A relative newcomer to the compression scene, the `xz` command is recognized for its impressive compression capabilities. While it might take longer for large files, the compression results are noteworthy:

Basic syntax:

xz [options] filename
xz bigfile

The `bigfile.xz` showcases the compressed version of the file. `xz` has gained popularity for its ability to achieve significant compression ratios, making it a top choice for those prioritizing file size reduction.


This article is all about making files smaller in Linux, and it’s like unlocking cool tricks with the Tape Archive (Tar) command. We learned how Tar can bundle files together and also squeeze them down using things like gzip, bzip2, and xz. We didn’t stop there – we also talked about friendly tools like zip and easy commands like gzip, bzip2, and xz. This helps you choose what’s best for you, whether you want things to be super simple, work quickly, save lots of space, or do some impressive stuff. So, if you’re using Linux, this guide helps you do smart things with your files, making them easy to handle.

Article Tags :