How to Compress and Extract Files Using the tar Command on Linux
Last Updated :
13 Sep, 2024
Tar archives are special files that bundle multiple files and directories into a single file, making it easier to store, share, and manage. While tar archives themselves are not compressed, they can be combined with various compression utilities to reduce their size. The tar command is highly versatile, allowing users to create, modify, and extract archives using a wide range of options and parameters.
Types of Tar Archives
- .tar: A basic tar archive that contains multiple files and directories without compression.
- .tar.gz: A tar archive compressed using the gzip tool, offering a good balance between speed and compression ratio.
- .tar.bz2: A tar archive compressed using the bzip2 tool, known for providing better compression but at a slower speed compared to gzip.
Syntax:Â
tar options [archive_name.tar] files_to_archive
The tar command does not create a compressed archive, instead, it uses external utilities like gzip and bzip2.
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 |
Example of using the tar command
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

7. Extract files from a .tar archive
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'.
Conclusion
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.
Similar Reads
How to Compress or Extract files and folders using CMD? One effective technique to handle data without depending on third-party software is to use the Command Prompt (CMD) on Windows to compress and extract files and directories. To use CMD to compress files and directories, you must utilize the integrated 7z command-line tool. If you work with large fil
5 min read
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
11 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
5 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 optimiz
4 min read
How to Copy Files and Directories in Linux | cp Command The cp (copy) command is your go-to tool in Linux for duplicating files and folders quickly. Whether youâre backing up data, organizing files, or sharing content, cp lets you copy items between locations while keeping the original intact. The cp command requires at least two filenames in its argumen
8 min read