How to Compress a File in the Terminal with gzip
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:
gzip [--keep] <file>
gunzip <file>.gz
| Part | What it means |
|---|---|
gzip <file> | Compresses the file into <file>.gz and deletes the original |
--keep | Keeps the original file as well |
gunzip <file>.gz | Decompresses 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:
[System.IO.Compression.GZipStream]::new(<gz file stream>, [System.IO.Compression.CompressionMode]::<mode>)
| Part | What 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
- macOS / Linux
- Windows (PowerShell)
seq 1 500 > numbers.txt
1..500 | Set-Content numbers.txt
Both commands write the numbers 1 to 500 into numbers.txt, one per line, and print nothing.
Step 2: Compress the File
- macOS / Linux
- Windows (PowerShell)
gzip --verbose numbers.txt
numbers.txt: 51.5% -- replaced with numbers.txt.gz
ls numbers.txt*
numbers.txt.gz
$file = (Resolve-Path numbers.txt).Path
$in = [System.IO.File]::OpenRead($file)
$out = [System.IO.File]::Create("$file.gz")
$gzip = [System.IO.Compression.GZipStream]::new($out, [System.IO.Compression.CompressionMode]::Compress)
$in.CopyTo($gzip)
$gzip.Dispose(); $out.Dispose(); $in.Dispose()
Get-ChildItem numbers.txt* | Select-Object Name, Length
Name Length
---- ------
numbers.txt 1892
numbers.txt.gz 853

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-Pathturnsnumbers.txtinto 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."OpenReadopensnumbers.txt, andCreatemakes the emptynumbers.txt.gz.- The
GZipStreamsits in front of the.gzfile, 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
- macOS / Linux
- Windows (PowerShell)
gunzip numbers.txt.gz
ls numbers.txt*
numbers.txt
Remove-Item numbers.txt
$file = (Resolve-Path numbers.txt.gz).Path
$in = [System.IO.File]::OpenRead($file)
$out = [System.IO.File]::Create($file.Substring(0, $file.Length - 3))
$gzip = [System.IO.Compression.GZipStream]::new($in, [System.IO.Compression.CompressionMode]::Decompress)
$gzip.CopyTo($out)
$gzip.Dispose(); $out.Dispose(); $in.Dispose()
Get-Content numbers.txt -TotalCount 3
1
2
3
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
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting gzip to keep the original | It deletes the file once the .gz file is written | Add --keep: gzip --keep numbers.txt left both numbers.txt and numbers.txt.gz |
Compressing the .gz file again | gzip: numbers.txt.gz already has .gz suffix -- unchanged | Compress the original file, not the .gz file |
Compressing when the .gz file already exists | gzip asks numbers.txt.gz already exists -- do you wish to overwrite (y or n)?, and n prints not overwriting | Type y and press Return to replace it, or move the old .gz file away first |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The original file is gone after gzip | That's what gzip does by default | Run gunzip <file>.gz to get it back, and use --keep next time |
already has .gz suffix -- unchanged | The file is compressed already | Nothing to do |
gzip stops and waits | It's asking whether to overwrite an existing .gz file | Type 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:
gzip --list numbers.txt.gz
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
- GZipStream Class — the .NET class the PowerShell commands use.
- about_Methods: Using .NET methods that take filesystem paths — why the commands resolve the path first.
man gzipin your terminal — the manual page on macOS and Linux.
