Linux commands: gunzip
By Flavio Copes
Learn how the Linux gunzip command decompresses gzipped .gz files, removing the extension, or extracts to a new filename using gunzip -c with redirection.
The gunzip command decompresses files that were compressed in the gzip format, the ones ending in .gz. It is the counterpart of the gzip command: the two are the same program wearing different names, except in gunzip the -d (decompress) option is always enabled by default.
The command can be invoked in this way:
gunzip filename.gz
This will gunzip and will remove the .gz extension, putting the result in the filename file. If that file already exists, gunzip won’t overwrite it quietly: in a terminal it asks you first, and in a script it prints already exists -- skipping and exits with status 1. Add -f to force the overwrite.
Notice the compressed file is gone afterwards. Check with ls:
ls
# filename
If you want to keep the .gz file around too, add the -k option:
gunzip -k filename.gz
# now you have both filename and filename.gz
You can extract to a different filename using output redirection using the -c option:
gunzip -c filename.gz > anotherfilename
-c writes the decompressed data to standard output instead of to a file, and the > redirection saves it wherever you want. The original .gz file stays untouched in this case too.
Before extracting, you can check what you will get with -l, which lists the compressed and uncompressed sizes:
gunzip -l filename.gz
Two failures you will run into sooner or later. First, running gunzip filename on a file without a recognized extension stops with unknown suffix -- ignored: gunzip only processes files it knows it created.
Second, gzip compresses a single file, never a folder. When you see project.tar.gz, that is a tar archive that was gzipped afterwards. Running gunzip project.tar.gz gives you project.tar, and you unpack that with the tar command — or let tar -xzf project.tar.gz do both steps at once.
The gunzip command works on Linux, macOS, WSL, and anywhere you have a UNIX environment
Want me to talk about your product? You can sponsor this site.