Skip to main content

How to Compress a File in the Terminal with gzip

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Text files such as logs and CSV exports take far less space once compressed. gzip compresses a file into a .gz file, and gunzip turns it back into the original. In PowerShell, .NET's GZipStream class writes and reads the same format.

Syntax​

In zsh and bash:

Syntax
gzip [--keep] <file>
gunzip <file>.gz
PartWhat it means
gzip <file>Compresses the file into <file>.gz and deletes the original
--keepKeeps the original file as well
gunzip <file>.gzDecompresses the file back to <file> and deletes the .gz file

The macOS manual page for gzip says that when it compresses, "each file will be replaced with another file with the suffix ... added", and that --keep "prevents gzip from deleting input files after (de)compression."

In PowerShell:

Syntax
[System.IO.Compression.GZipStream]::new(<gz file stream>, [System.IO.Compression.CompressionMode]::<mode>)
PartWhat it means
[System.IO.Compression.GZipStream]::new(...)Creates a .NET gzip stream
<gz file stream>The opened .gz file: the one to write when compressing, or the one to read when decompressing
<mode>Compress or Decompress

Microsoft's documentation says the class "Provides methods and properties used to compress and decompress streams by using the GZip data format specification", and that the .gz files it writes "can be decompressed using many common compression tools". PowerShell's own Compress-Archive makes zip files instead: it "Creates a compressed archive, or zipped file".

The Example​

numbers.txt holds the numbers 1 to 500, one per line. You compress it, check the result, and decompress it again.

Step 1: Create the File​

Terminal
seq 1 500 > numbers.txt

Both commands write the numbers 1 to 500 into numbers.txt, one per line, and print nothing.

Step 2: Compress the File​

Terminal
gzip --verbose numbers.txt
Output
numbers.txt: 51.5% -- replaced with numbers.txt.gz
Terminal
ls numbers.txt*
Output
numbers.txt.gz

The zsh terminal after gzip --verbose numbers.txt, which prints 51.5% -- replaced with numbers.txt.gz, and ls numbers.txt*, which lists only numbers.txt.gz.

gzip wrote numbers.txt.gz and deleted numbers.txt, so the listing shows only the .gz file. --verbose made it print how much smaller the file got: 51.5%. bash printed the same.

The PowerShell commands work like this:

  • Resolve-Path turns numbers.txt into its full path. .NET doesn't use the folder PowerShell is in: about_Methods says ".NET methods use the process working directory. PowerShell cmdlets use the Runspace location."
  • OpenRead opens numbers.txt, and Create makes the empty numbers.txt.gz.
  • The GZipStream sits in front of the .gz file, and $in.CopyTo($gzip) copies the text through it, compressing it on the way.
  • Dispose() closes the three streams when the copy is done, the gzip stream first.

The .gz file is 853 bytes, less than half of the 1,892-byte original, and numbers.txt is still there. This output is from PowerShell 7.6 on macOS.

Step 3: Decompress the File​

Terminal
gunzip numbers.txt.gz
ls numbers.txt*
Output
numbers.txt

gunzip wrote numbers.txt back and deleted numbers.txt.gz. In PowerShell, Remove-Item first deletes the original, so the file you read at the end is the one that came out of the .gz file. $file.Substring(0, $file.Length - 3) is the path without its last three characters, .gz, and the stream now runs the other way: Decompress, with $gzip.CopyTo($out).

Common Mistakes​

MistakeWhat happensDo this instead
Expecting gzip to keep the originalIt deletes the file once the .gz file is writtenAdd --keep: gzip --keep numbers.txt left both numbers.txt and numbers.txt.gz
Compressing the .gz file againgzip: numbers.txt.gz already has .gz suffix -- unchangedCompress the original file, not the .gz file
Compressing when the .gz file already existsgzip asks numbers.txt.gz already exists -- do you wish to overwrite (y or n)?, and n prints not overwritingType y and press Return to replace it, or move the old .gz file away first

Troubleshooting​

SymptomLikely causeFix
The original file is gone after gzipThat's what gzip does by defaultRun gunzip <file>.gz to get it back, and use --keep next time
already has .gz suffix -- unchangedThe file is compressed alreadyNothing to do
gzip stops and waitsIt's asking whether to overwrite an existing .gz fileType y or n and press Return

FAQ​

How do I read a .gz file without decompressing it?​

Use gunzip -c, which writes the text to the terminal and leaves the .gz file as it is. gunzip -c numbers.txt.gz | head -n 3 printed 1, 2 and 3. The manual page says -c "specifies that output will go to the standard output stream, leaving files intact."

How do I see how much smaller a file got?​

Use gzip --list:

Terminal
gzip --list numbers.txt.gz
Output
compressed uncompressed ratio uncompressed_name
917 1892 51.5% numbers.txt

The file went from 1,892 bytes to 917.

Can gunzip open the file PowerShell made?​

Yes. gunzip -t numbers.txt.gz found no errors in the file .NET wrote, and the text it unpacked matched numbers.txt byte for byte. Both tools started from the same 1,892 bytes, but gzip made 917 bytes and .NET 853: they compress a little differently.

Conclusion​

gzip --verbose numbers.txt replaced the file with a .gz file about half its size, and gunzip numbers.txt.gz brought it back. Add --keep to keep the original, and use gunzip -c to read a .gz file without unpacking it. In PowerShell, .NET's GZipStream writes and reads the same format.

Additional Resources​