Using gzip and gunzip in Linux


Introduction

When working in Linux, it's common to encounter large files that need to be compressed for storage or transfer. One popular compression tool available in Linux is gzip, which is used to compress files to save disk space and reduce transfer time. In this article, we'll explore how to use gzip and gunzip in Linux, including how to compress and decompress files, and how to work with compressed archives.

What is gzip?

Gzip is a compression utility tool used in Linux and other Unix-based systems. It's used to compress and decompress files, reducing their size for storage and transfer. Gzip works by replacing repeated strings of data in a file with a shorter representation. When file is decompressed, original data is reconstructed from shorter representation.

How to use gzip

To compress a file using gzip, you can use following command −

gzip file.txt

This command will compress file named file.txt and create a new file named file.txt.gz. original file will be deleted, and compressed file will be saved in its place.

To decompress a file that has been compressed with gzip, you can use following command −

gunzip file.txt.gz

This command will decompress file file.txt.gz and create a new file named file.txt. compressed file will be deleted, and decompressed file will be saved in its place.

How to use gzip with options

Gzip also provides several options that can be used to modify its behavior. Here are some commonly used options −

  • -c − Write output to standard output (stdout), leaving original file unchanged. This is useful when you want to compress a file and send it directly to another program or device without creating a new compressed file.

gzip -c file.txt > compressed_file.txt.gz

This command compresses file.txt and writes compressed output to stdout. > operator redirects output to a new file named compressed_file.txt.gz.

  • -d − Decompress compressed file. This option is same as gunzip command.

gzip -d file.txt.gz

This command decompresses file.txt.gz and creates a new file named file.txt.

  • -f − Force compression or decompression even if file already exists or if it is a symbolic link.

gzip -f file.txt

This command compresses file.txt and overwrites it if it already exists.

  • -r − Compress all files in a directory recursively.

gzip -r directory/

This command compresses all files in directory/ directory and its subdirectories.

Working with compressed archives

In addition to compressing individual files, gzip can also be used to create and extract compressed archives. A compressed archive is a collection of files and directories that have been compressed into a single file for easier storage and transfer.

To create a compressed archive with gzip, you can use following command −

tar czf archive.tar.gz directory/

This command creates a new compressed archive named archive.tar.gz that contains all files and directories in directory/ directory. c option tells tar to create a new archive, z option tells tar to use gzip compression, and f option specifies name of output file.

To extract a compressed archive created with gzip, you can use following command −

tar xzf archive.tar.gz

This command extracts contents of compressed archive archive.tar.gz into current directory. x option tells tar to extract archive, z option tells tar to use gzip decompression, and f option specifies name of archive file.

You can also add -v option to tar command to show progress of archive creation or extraction.

tar czvf archive.tar.gz directory/
tar xzvf archive.tar.gz

In first command, v option adds verbosity to output, showing progress of archive creation. In second command, v option shows progress of archive extraction.

Advanced uses of gzip

Besides basic uses of gzip, there are also some advanced features that can be useful in certain situations.

Compressing multiple files with gzip

To compress multiple files with gzip, you can use *.txt wildcard to select all files with .txt extension in current directory.

gzip *.txt

This command will compress all files with .txt extension in current directory, creating new compressed files with .gz extension.

Compressing files while keeping original file

To compress a file with gzip while keeping original file, you can use -k option.

gzip -k file.txt

This command will compress file.txt and create a new compressed file named file.txt.gz, but original file will remain in place.

Using gzip with pipes

You can use gzip command with pipes to compress data directly from standard input (stdin) and send compressed output to standard output (stdout).

cat file.txt | gzip > file.txt.gz

This command reads contents of file.txt using cat command and pipes it to gzip command, which compresses data and writes it to stdout. > operator redirects compressed output to a new file named file.txt.gz.

Using gunzip

The gunzip command is used to decompress files that have been compressed with gzip. Here are some advanced features of gunzip.

Decompressing multiple files with gunzip

To decompress multiple files with gunzip, you can use *.gz wildcard to select all files with .gz extension in current directory.

gunzip *.gz

This command will decompress all files with .gz extension in current directory, creating new uncompressed files with original filename and extension.

Decompressing files while keeping original file

To decompress a file with gunzip while keeping original file, you can use -k option.

gunzip -k file.txt.gz

This command will decompress file.txt.gz and create a new uncompressed file named file.txt, but original compressed file will remain in place.

Conclusion

Using gzip and gunzip in Linux is a simple and effective way to compress and decompress files and archives. By using these commands and their options, you can reduce size of your files, save disk space, and speed up file transfers. Whether you're working with individual files or compressed archives, gzip provides a reliable and efficient way to compress and decompress your data. With examples and options provided in this article, you can start using gzip and gunzip in your Linux workflow today.

Updated on: 24-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements