How to Use Gzip and Keep Original File in Linux?
Gzip is a command-line tool that by default comes with any Linux Operating System. Gzip is commonly used to compress and decompress files. It reduces the size of the file and replaces the original file with the extension .gz file. It can restore the compressed file using the simple command. Now let’s see how to use the Gzip to compress and decompress files on Linux
Keep 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 -k option
To keep the original file as it while gzip, the gzip proves the one option -k. Following is the syntax
gzip -k filename
After the compressing file using this command you can see there are new compressed files created by gzip and your original file is also be present there.
Method 2: Using the -c options
By using the -c option gzip writes output on the standard output without deleting the original file. Here is the syntax for using the -c option
gzip -c filename.extension > filename.extension.gz
Method 3: Using shell redirections
We can also use the shell redirections to keep the file as it is while using gzip. We are going to use the following direction operators
- “>” standard output
- “<” standard input
Following is the syntax to use the redirection operator with the gzip:
gzip < filename.extension > myfile.extension.gz
Now let’s see what other things we can do using the gzip:
Replace the original file with .gz extension file.
To replace the target file with compressed file format just simply use the gzip command. Following is the syntax of the command:
Here is one example of the above command:
To gzip all files in the current folder and subfolders we can use the following command:
sudo gzip -kr .
To unzip to decompress the gzip file we can use the -d option with gzip. Following is the syntax for decompressing the file:
gzip -d filename.extension.gz