Skip to content

Compression & Archiving

Commands for compressing and decompressing files with gzip, and creating or extracting archives with tar.


Compress files using the gzip format. By default, replaces each file with a compressed .gz version.

gzip [OPTIONS] FILE...
FlagDescription
-dDecompress (same as gunzip)
-cWrite output to stdout instead of replacing the file
-kKeep the original file (do not delete after compress/decompress)
-fForce overwrite of output files
-vVerbose — show compression ratio for each file
-lList compression statistics instead of compressing
-1 .. -9Compression level (-1 fastest, -9 smallest, default -6)

With -l, each result is a PsBash.GzipListOutput object:

PropertyTypeDescription
CompressedSizeintSize of the compressed file in bytes
UncompressedSizeintOriginal uncompressed size in bytes
RatiostringCompression ratio as a percentage (e.g. "72.5%")
FileNamestringPath to the compressed file
BashTextstringFormatted text output

With -v, returns BashObject lines showing the compression ratio per file.

Terminal window
# Compress a file, keeping the original
gzip -k data.csv
Terminal window
# Compress with maximum compression
gzip -9 largefile.bin
Terminal window
# List compression statistics for a .gz file
gzip -l archive.gz
Terminal window
# Access typed properties from the list output
$info = gzip -l backup.gz
$info.CompressedSize # 14320
$info.UncompressedSize # 52480
$info.Ratio # "72.7%"

Decompress gzip-compressed files. This is an alias for gzip -d — it automatically sets the decompress flag.

gunzip [OPTIONS] FILE...
FlagDescription
-cWrite decompressed output to stdout
-kKeep the .gz file after decompression
-fForce overwrite of output files
-vVerbose — show compression ratio
Terminal window
# Decompress a file, removing the .gz
gunzip data.csv.gz
Terminal window
# Decompress but keep the original .gz file
gunzip -k archive.gz
Terminal window
# Decompress to stdout for piping
gunzip -c logs.gz | grep 'ERROR'

Decompress gzip files and write the content to stdout. This is equivalent to gzip -dc — both decompress and stdout flags are set automatically.

zcat FILE...
Terminal window
# View compressed file contents
zcat readme.txt.gz
Terminal window
# Pipe decompressed content into other commands
zcat access.log.gz | grep '404' | wc -l
Terminal window
# Preview the first few lines of a compressed file
zcat data.csv.gz | head 5

Create, extract, and list archive files. Uses System.IO.Compression.ZipArchive internally, with optional gzip wrapping via the -z flag.

tar [OPTIONS] [FILE...]
FlagDescription
-cCreate a new archive
-xExtract files from an archive
-tList the contents of an archive
-f FILESpecify the archive filename (required)
-zFilter the archive through gzip (compress on create, decompress on extract/list)
-vVerbose — print filenames as they are processed
-C DIRChange to DIR before extracting
--exclude=PATTERNExclude files matching the glob pattern

With -t, each entry is a PsBash.TarListOutput object:

PropertyTypeDescription
NamestringFilename within the archive
SizelongUncompressed size in bytes
ModifiedDateDateTimeLast modification timestamp
BashTextstringEntry path as displayed in the listing
Terminal window
# Create a gzip-compressed archive from a directory
tar -czf project.tar.gz src/
Terminal window
# Extract a gzip-compressed archive to a specific directory
tar -xzf project.tar.gz -C /tmp/output
Terminal window
# List contents of an archive
tar -tzf backup.tar.gz
Terminal window
# Access typed properties from the listing
$entries = tar -tzf project.tar.gz
$entries[0].Name # "app.js"
$entries[0].Size # 2048
$entries[0].ModifiedDate # 4/1/2026 3:22:00 PM
Terminal window
# Create an archive excluding test files
tar -czf release.tar.gz src/ --exclude=*.test.ps1
Terminal window
# Verbose extract to see each file as it is written
tar -xzvf archive.tar.gz