How to Use Gzip and Keep Original File in Linux?
Last Updated :
23 Oct, 2024
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.
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
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 .
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 .
Gzip recursive compression output
Decompressing Files
To decompress a Gzip file:
gzip -d sample.txt.gz
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.