Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to tell gzip to keep original file?

I would like to compress a text file using gzip command line tool while keeping the original file. By default running the following command
gzip file.txt


results in modifying this file and renaming it file.txt.gz. instead of this behavior I would like to have this new compressed file in addition to the existing one file.txt. For now I am using the following command to do that
gzip -c file.txt > file.txt.gz


It works but I am wondering why there is no easier solution to do such a common task ? Maybe I missed the option doing that ?
by

1 Answer

Kajalsi45d
From the documentation it seems that there is no option to create a copy of the file.

You can define a shell function
gzipkeep() {
if [ -f "$1" ] ; then
gzip -c -- "$1" > "$1.gz"
fi
}


and then
gzipkeep file.txt

Login / Signup to Answer the Question.