Gzip command by default delete the original file and replace it with a new compressed file with .gz extension. In this article, I am going to show you how to gzip file and keep the original ones intact.
Gzip and keep the original file
We can use three options to keep the original file while using gzip:
Method 1: Using -k option
$ sudo gzip -k picture.img
$ sudo gzip --keep picture.img
Method 2: Using -c option
$ sudo gzip -c picture.img > picture.img.gz
For the gunzip command you can the same
-c option to keep the original file.
Method 3: Using shell redirections
$ sudo gzip < picture.img > picture.img.gz
Note: You might have noticed I have used
sudo sh -c command, this is because redirection is done by shell and sudo don't have permission
To gzip all files
To gzip all files in the current folder and subfolders, use:
$ sudo gzip -kr .
To gzip all files and subfolders
Unlike gzip command, tar command by default will keep the original directories or files, use:
$ sudo tar -zcvf compressednewfile1.tar.gz directorytocompress
$ sudo tar -zcvf allfilesdirectories.tar.gz *
Choose a selected number of directories to gzip:
$ sudo tar -zcvf directory1and2.tar.gz dir1 dir2
As you have seen in this guide, gzip allows you not only to compress a file but also gives you the privilege to retain the original file after compression. For users who prefer keeping the original files, this comes as a very handy method and saves you time and energy. We do hope that you found this helpful. Your feedback is much appreciated.