Open In App

How to Use Gzip and Keep Original File in Linux?

Last Updated : 23 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Gzip is a pretty powerful command-line tool that comes pre-installed with most Linux operating systems. Users often use it to compress files, thereby turning them into much smaller file sizes. Gzip, by default, overwrites the original file with a compressed copy of it having the .gz extension name. However, there are ways of keeping the original file intact while making a compressed copy. This tutorial shows how you can use Gzip effectively, as well as some cool ways to keep your original file intact.

Syntax

gzip [options] filename

Basic example

Generate a test file and then compress it with Gzip:

# Generate a sample file
echo "This is a sample file for Gzip compression" > sample.txt
# Compress the file
gzip sample.txt

When you issue the above commands you will notice how the original file was substituted with a compressed one, which also carried the name of the original sample.txt file in itself, with an additional part .gz attached.

Basic example

Important Options and usage

Options

Description

-k, –keep

Keep the original file

-c, –stdout

Write output to standard output and keep input files unchanged

-d, –decompress

Decompress files

-r, –recursive

Compress files recursively in directories

Keeping the Original File

As we know the gzip deletes the original file and creates the gzip file. But we can create the gzip file without losing the original file We are going to see three methods of creating the gzip file without losing the original file:

Method 1: Using the -k option

gzip -k sample.txt

This will produce a compressed file sample.txt.gz while the original sample.txt unaltered

Method 1: Using the -k option

Gzip -k option output

Method 2: Using the -c option

gzip -c sample.txt > sample.txt.gz

This operation outputs the compressed result to standard output, which we redirect to a new file .

Method 2: Using the -c option

Gzip -c option output

Method 3: Utilizing shell redirection

gzip  sample.txt.gz

In this method, we obtain the same result from Method 2 through the use of input and output redirection.

Compressing Multiple Files

All files in the current directory and its subdirectories can be compressed using the following command:

gzip -r .
Compressing Multiple Files

Gzip recursive compression output

Decompressing Files

To decompress a Gzip file:

gzip -d sample.txt.gz
Decompressing Files

Gzip decompression output

Conclusion

Gzip is a utility that assists in compressing files in Linux. Though usually it overwrites the original by replacing with the compressed one, we have explored more than a few ways to keep the original intact. Options such as -k and -c or redirects with shell give an enabling capability to compress the files without losing the originals. Choose the option that best fits your workflow and needs.

How to Use Gzip and Keep Original File in Linux – FAQs

What is Gzip used for?

Gzip compresses files in Linux systems, thus reducing their size.

How do I compress a file with Gzip while keeping the original?

gzip -k option compresses the file leaving the original file intact.

What is the -k option for Gzip?

The -k option for Gzip means to keep the original file during the compression of the file.

Can I gzip more than one file from the list ?

Yes. Perform the following command:

gzip -k file1 file2 file3

How do I find the size of a Gzip compressed file?

Use ls -lh to get its size.


Master DevOps and also get 90% Course fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

The most sought-after Three 90 challenge has started and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


Next Article

Similar Reads

How to Solve gzip: stdin: Not in gzip Format Error
If you’ve encountered the error message gzip: stdin: not in gzip format, you’re not alone. This error occurs when users try to extract a file using the gzip option with tar, but the file they are extracting is not actually in the gzip format. In this article, we'll break down the cause of this error, how to resolve it, and also look into the differ
3 min read
Gzip Command in Linux
The gzip command in Linux is an essential tool used for compressing files and reducing file sizes. The gzip command utilizes the DEFLATE compression algorithm, making it efficient for file compression tasks, especially when managing large files or transferring data over networks. Whether you’re working as a Linux system administrator or just gettin
5 min read
How to Send a File Using Netcat and Then Keep the Connection Alive
Netcat (NC) is a popular networking utility tool for reading and writing data across network connections using TCP or UDP protocols. It can be used for both attack and security, in the case of attacking. It helps us to debug the network along with investigating it. It runs on all operating systems. Netcat (NC) is a well-known systems administration
6 min read
How to Keep Android Apps Running Smoothly?
This article is all about creating a better Android app (no more lags!) and building a smooth-running interface. We will discover how to increase the performance of an Android application. First and foremost, we must determine one big question: Why does the Android App slow? or Why does an Android app lag? We will begin with the themed culprit behi
4 min read
5 Ways to Keep Your Ubuntu System Clean
Linux has it's own data management system that cleans your computer under the belt but still, sometimes we need to know how to manually initiate disk cleaning and control processes to enhance your computer's performance. Below are 5 ways to keep the ubuntu system clean. 1. Uninstalling and Removing Unnecessary Applications: To uninstall the applica
2 min read
How to Fix - Cp: Cannot Create Regular File 'File': File Exists
In Linux, while performing copying operations using the cp command, the error "Cp: Cannot Create Regular File 'File': File Exists" may occur when attempting to copy a file to a destination where a file with the same name already exists. This error is a safeguard to prevent accidental overwriting of files and potential data loss. To resolve this iss
4 min read
Use the Lines of a File as Arguments of a Linux Command
Handling multiple arguments for a command can be difficult, especially when there are a lot of parameters. One way to make this easier is to put the arguments in a text file and use each line of the file as an argument for the command. This can simplify the process and save time. For example, let's say you have a text file with a list of 100 client
6 min read
Difference Between a Script file and a Binary file
In this tutorial, we will learn what a script file and a binary file are and what's the difference between a Script file and a Binary file. Binary File A binary file is a file in which the content of the file is in binary format, the file data is not human-readable. Binary files contain formatted information that only certain applications or proces
4 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
File Timestamps - mtime, ctime and atime in Linux
Timestamps are records for the times in which actions are performed on files. A timestamp is useful because it keeps records of when a file was accessed, modified, or added. Linux's files have 3 timestamps recorded by the computer: Access timestamp (atime): which indicates the last time a file was accessed.Modified timestamp (mtime): which is the
4 min read
  翻译: