The Wayback Machine - https://web.archive.org/web/20240712165357/https://www.geeksforgeeks.org/tar-command-linux-examples/
Open In App

How to Compress Files in Linux | Tar Command

Last Updated : 12 Jul, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 ,

  • tar: The command itself.
  • [options]: Optional flags or settings that modify the behavior of the tar command.
  • [archive-file]: The name of the archive file you are creating or working with.
  • [file or directory to be archived]: The file or directory you want to include in the archive.

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. 

Options

Description

-c

Creates an archive by bundling files and directories together.

-x

Extracts files and directories from an existing archive.

-f

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

-t

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

-u

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

-v

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

-A

Concatenates multiple archive files into a single archive.

-z

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

-j

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

-W

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

-r

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
  • ‘-c’: Creates a new archive.
  • ‘-v’: Displays verbose output, showing the progress of the archiving process.
  • ‘-f’: Specifies the filename of the archive

Output : 

os2.c
os3.c
os4.c

2. Extracting files from Archive using option -xvf

This command extracts files from Archives. 

tar xvf file.tar
  • ‘-x’: Extracts files from an archive.
  • ‘-v’: Displays verbose output during the extraction process.
  • ‘-f’: Specifies the filename of the archive.

Output :  

os2.c
os3.c
os4.c

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
  • ‘-z’: Uses gzip compression.
  • ‘-j’: Uses bzip2 compression.
  • ‘-J’: Uses xz compression.

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

Output:  

tar cvfj file.tar.tbz example.cpp
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 
or
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" 
or
tar zxvf file1.tar.gz "fileA" "fileB"
or
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
or
tar czf file1.tar.gz | wc -c
or
tar czf file2.tar.tbz | wc -c

9. Update existing tar file in Linux  

tar rvf file.tar *.c

Output:  

os1.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

Output:  

example.cpp

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" 
or
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

Output:  

-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
  • [options]: Optional flags or settings for the zip command.
  • zipfile: The name of the ZIP archive to be created.
  • files/directories: The files or directories to be included in the ZIP archive.
zip ./bigfile.zip bigfile

This single line of code encapsulates the essence of the `zip` command, compressing the file `bigfile` and generating a new file, `bigfile.zip`, 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
  • [options]: Optional flags or settings you may want to apply.
  • filename: The name of the file you wish to compress.
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
  • [options]: Optional flags or settings you may want to apply.
  • filename: The name of the file you wish to compress.
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.

Conclusion

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.

How to Compress Files in Linux | Tar Command – FAQs

How to Reduce the File Size in Linux

To reduce file size by compressing files, you can use tools like gzip, bzip2, or xz. These tools are great for compressing individual files into smaller sizes. For example:

gzip filename.txt

This command will compress filename.txt into filename.txt.gz and the original file filename.txt will be replaced.

How to Compress All Files in Linux?

To compress multiple files into one archive, you can use tar with compression options:

tar -czvf archive_name.tar.gz /path/to/directory

This command uses gzip (-z) to compress the archive. -c creates a new archive, -v enables verbose mode to show the progress, and -f specifies the filename of the archive.

How to Compress a File to Minimum Size in Linux?

For maximum compression, you might consider using xz, which typically provides a better compression ratio:

xz -z filename.txt

This will compress filename.txt to filename.txt.xz. Alternatively, you can use tar with xz for multiple files:

tar -cJvf archive_name.tar.xz /path/to/directory

Here, -J invokes the xz compression.

How to Compress Old Files in Linux?

To compress older files specifically, you can first find files based on their age using find and then compress them. For example, to find and compress files modified more than 30 days ago:

find /path/to/directory -mtime +30 -type f -exec gzip {} \;

This command searches for files older than 30 days and compresses each one using gzip.

How to Compress in Linux Command?

Basic compression with gzip:

gzip filename

Using bzip2 for better compression:

bzip2 filename

Or using xz for even better compression:

xz filename

To compress an entire directory using tar with gzip:

tar -czf archive.tar.gz /path/to/directory


Previous Article
Next Article

Similar Reads

How to Compress and Extract Files Using the tar Command on Linux
An archive is a special file that contains any number of files inside. It can be restored via special programs, for example, tar.inside. .tar - archive files are usually not compressed..tar.gz - archive file compressed with gzip tool.tar.bz2 - archive file compressed with bzip2 tool Syntax: tar options [archive_name.tar] files_to_archive The tar co
2 min read
Check If File Exist Inside Tar File Using Tar And Grep Command
Linux users are familiar with the popular command-line tool Tar (tape archive), which is used to archive directories and files into a single file called a "tarball." Occasionally, you might need to verify whether a certain file is present in a tar archive without extracting the full archive. This detailed article will show you how to effectively se
4 min read
How to compress file in Linux | Compress Command
Linux, renowned for its powerful command-line utilities, provides users with various tools to streamline file management tasks. One such tool, the compress `command`, is specifically designed to compress individual files, reducing their size for efficient storage and transfer. In this comprehensive guide, we'll explore the ins and outs of the compr
5 min read
How to Exclude Files and Directories When Creating a tar.gz File
GNU tar is a type of archiving program that is designed to store multiple files into a single file and to manipulate such archives. The archive can be either a regular file or a device that can be located either locally or on a remote machine. How to Exclude Files and Directories When Creating a tar.gz File Step 1: First we need to create multiple
2 min read
How to Compress Files Faster with Pigz Tool in Linux
Pigz (parallel implementation of gzip) is a free, open-source multi-threaded compression software for Linux that compresses and uncompresses files. Pigz is pronounced as "pig-zee", it compresses data using the zlib and pthread libraries and takes full advantage of many processors and cores. Pigz can archive larger files significantly quicker than g
3 min read
Optimize and Compress JPEG or PNG Images in Linux Command line
Today there are many GUI tools that compress and optimize the images, which are very useful. But if you are terminal addicted and want to compress and optimize the images in the terminal before uploading to the cloud, then you can do it. There are two tools by using which we can compress and optimize images. These are - jpegoptimOptiPNGjpegoptim jp
4 min read
Create incremental backup in linux using tar
As a system administrator, it's a regular task to backup the data. There are two types of backup, incremental and full backup. In the full backup, the user needs to back up all the data every time. But in incremental backup, the user takes a backup of data that has been modified or newly added since the last backup. How does incremental backup work
3 min read
Linux - Installing locate Command to Find Files
In this article, we will see how to install locate command to find files in Linux. locate is a command-line interface tool used for finding files by name in Linux systems. This works more efficiently than other commands. It uses one or more databases by updatedb. To check whether locate utility is already installed in your system. Open up your term
2 min read
How to List Open Files in Linux | lsof Command
In the world of Linux, understanding and managing open files is crucial for system administrators and users alike. The Linux operating system provides a powerful utility called lsof (List Open Files) that allows users to gain insights into the files currently open on their system. In this article, we will delve into the intricacies of the lsof comm
7 min read
How to sort lines in text files in Linux | sort Command
SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically.  SORT command sorts the contents of a text file, line by line.sort is a standard command-line program that prints the lines
7 min read