USD ($)
$
United States Dollar
Euro Member Countries
India Rupee

Compressing and Archiving Files

Lesson 19/49 | Study Time: 20 Min

File compression and archiving are essential techniques in Linux for efficient storage management and file transfer. Compression reduces file size, saving disk space and speeding up transmission, while archiving bundles multiple files and directories into a single file for easier handling.

Linux offers powerful command-line tools like tar, gzip, bzip2, and zip that provide flexible options for creating, compressing, extracting, and managing archives. Understanding these commands enables effective backup strategies and optimized data management.

Understanding Archiving and Compression

Archiving: Combines multiple files and folders into one container file, preserving directory structure and metadata. This does not reduce file size by itself.

Compression: Reduces the file size by encoding data more efficiently, often applied after or during archiving.

The tar Command: Archiving Tool

The tar command allows users to combine and manage files as archives. Following are the most commonly used tar actions.


1. Creating Archives:

text
tar -cvf archive.tar /path/to/directory


  • -c creates a new archive.
  • -v verbose, shows progress.
  • -f specifies the archive filename.


2. Extracting Archives:

text
tar -xvf archive.tar

-x extracts files.


3. Listing Contents:

text
tar -tvf archive.tar

tar alone does not compress but bundles files efficiently.

Compression Utilities Commonly Used with tar

Tar supports multiple compression methods through built-in options. Below is a list of common compression utilities used with tar archives.


1. gzip: Fast compression, produces .gz files.


  • Create compressed archive:
text
tar -czvf archive.tar.gz /path/to/directory

-z option filters archive through gzip compression.


  • Extract:
text
tar -xzvf archive.tar.gz


2. bzip2: Higher compression ratio but slower, produces .bz2 files.


  • Create compressed archive:
text
tar -cjvf archive.tar.bz2 /path/to/directory

-j option filters archive through bzip2 compression.


  • Extract:
text
tar -xjvf archive.tar.bz2


3. xz: Compresses better than gzip and bzip2, producing .xz files.


  • Create archive:
text
tar -cJvf archive.tar.xz /path/to/directory

-J uses xz compression.


  • Extract:
text
tar -xJvf archive.tar.xz

The zip and unzip Commands

zip is a versatile compression tool creating .zip files commonly used on Windows.


  • Create archive:
text
zip -r archive.zip /path/to/directory


  • Extract:
text
unzip archive.zip


zip compresses files individually within the archive and supports encryption.

Checking Integrity and Options

Compression tools provide options to verify archive integrity and control performance. Below are common methods for checking archives and adjusting compression behavior.


1. Verify gzip archive with:

text
gzip -t archive.gz


2. Verbose output during compression facilitates monitoring progress.

3. Set compression levels (1 fastest, 9 maximum compression):

text
gzip -9 filename
tar -czvf -9 archive.tar.gz /path

Practical Tips


1. Combine with -v flags to monitor process.

2. Use --exclude in tar to omit certain files/dirs.

3. For very large data, incremental backups with tar -g are efficient.

4. When restoring, always verify archive contents before extracting.

Samuel Wilson

Samuel Wilson

Product Designer
Profile

Class Sessions

1- What is Linux and Operating System Concepts 2- Linux History and Evolution 3- Linux Distributions and Their Purposes 4- Open Source Software and Licensing 5- Graphical User Interface (GUI) and Desktop Environments 6- Terminal Access and Command-Line Fundamentals 7- Getting Help and Command Documentation 8- File System Hierarchy and Directory Structure 9- Navigating Directories and Listing Contents 10- Creating, Copying, and Moving Files and Directories 11- Deleting Files and Directories 12- Symbolic and Hard Links 13- Understanding File Permissions Model 14- Modifying Permissions and Ownership 15- User and Group Management 16- Sudo and Privilege Escalation 17- Text Searching and Pattern Matching 18- Text Processing and Stream Editing 19- Compressing and Archiving Files 20- Text Editing and File Creation 21- Package Management Systems Overview 22- Installing and Updating Software with APT 23- Installing and Updating Software with YUM/DNF 24- Managing Software from Non-Repository Sources 25- Understanding Processes and Process Management 26- Viewing Running Processes 27- Process Control and Termination 28- Task Scheduling with Cron 29- Networking Concepts and IP Addressing 30- Viewing and Configuring Network Interfaces 31- Basic Network Troubleshooting 32- Shell Script Basics 33- Variables and Data Types 34- Conditional Logic in Scripts 35- Loops and Iteration 36- Functions and Code Reuse 37- Input/Output and User Interaction 38- System Authentication and Access Control 39- File System Security 40- Software Updates and Patching 41- Basic Firewall Concepts 42- System Information and Monitoring 43- Service and Daemon Management 44- System Boot Process and Runlevels 45- System Backup and Disaster Recovery 46- Comprehensive File System Management 47- System Automation Workflows 48- Multi-Concept Troubleshooting Scenarios 49- Continued Learning Pathways

Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.