• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Compress and Extract Files and Directories in Linux Using gzip and bzip2

by admin

The post explains provides basic commands to compress files and folders in Linux OS. To compress a file or folder, gzip and bzip2 are two very popular tools.

  1. gzip
  2. bzip2

Gzip Compression

To compress a file:

# gzip file1

A file by name file1.gz will be created in current directory replacing file1. You can compress multiple files in one go by just specifying their names, separated by spaces. For example:

# gzip file1 file2 file3

To expand a gzip compressed file in current directory:

# gunzip file1.gz

To compress a directory, you can create a tar and compress it.

# tar -czvf name-of-archive.tar.gz /path/to/directory

To compress multiple directories in one file:

# tar -czvf name-of-archive.tar.gz /path/directory1 /path/directory2

To extract the a “tar and compressed” directory in your current directory,

# tar -xzvf name-of-archive.tar.gz

To extract the a “tar and compressed” directory in a particular directory,

# tar -xzvf name-of-archive.tar.gz -C /path/to/directory

Bzip2 compression

To compress a file:

# bzip2 file1

A file by name file1.bz2 will be created in current directory replacing file1. Similar to gzip you can compress multiple files in one go. For example

# bzip2 file1 file2 file3

To expand a file in current directory:

# bunzip2 file1.bz2

To compress a directory, you can create a tar and compress it.

# tar -cjvf name-of-archive.tar.bz2 /path/to/directory

To compress multiple directories in one file:

# tar -cjvf name-of-archive.tar.bz2 /path/directory1 /path/directory2

To extract the a “tar and compressed” directory in your current directory,

# tar -xjvf name-of-archive.tar.bz2

To extract the a “tar and compressed” directory in a particular directory,

# tar -xjvf name-of-archive.tar.bz2 -C /path/to/directory
View files using – cat, more, tail, head and wc commands
How to tar, untar files and view contents of tar file under Linux

Filed Under: Linux

Some more articles you might also be interested in …

  1. How to configure Partitioned Block Devices (Non-ASMLIB) And Assign Them To ASM
  2. CentOS / RHEL : How to migrate storage (LVM) with pvmove Command
  3. How to Disable/Enable services in Zimbra Mail Server
  4. How to Check if any of the RPM files were tampered with
  5. MySQL Fails to Start Using systemctl On systemd Linux Distributions
  6. named-checkconf: command not found
  7. How to Check the Size of the Yum Channels in Oracle Linux
  8. How to Check Btrfs FileSystem Usage and Perform Balancing
  9. ifconfig Command Examples in Linux
  10. What are Shell Scripts? How to Create Shell Scripts?

You May Also Like

Primary Sidebar

Recent Posts

  • vgextend Command Examples in Linux
  • setpci command – configure PCI device
  • db_load command – generate db database
  • bsdtar command – Read and write tape archive files

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright