Compressing files with gzip and xz
- Version
- unversioned
- Review
- Draft
Summary#
gzip and xz each compress a single file. gzip is fast and widely supported; xz usually produces smaller files and takes longer.
Syntax#
gzip app.log
gzip -k app.log
gunzip app.log.gz
gzip -dc app.log.gz | grep ERROR
zcat app.log.gz
xz -k app.log
By default the compressed file replaces the original; -k keeps it. -d decompresses and -c writes to standard output. Combine several files with tar before compressing them.
Example#
(
set -eu
dir=$(mktemp -d)
trap 'rm -rf -- "$dir"' EXIT
cd "$dir"
printf '%s\n' ready done > app.log
gzip -k app.log
ls
gzip -dc app.log.gz
rm app.log
gunzip app.log.gz
ls
)
Expected output#
app.log
app.log.gz
ready
done
app.log
Common mistakes#
gzip file removes file after writing file.gz; use -k when something else still needs the original. A process that keeps writing to a log does not follow the new compressed file. gzip: file: not in gzip format means the content is something else, whatever the name says; check it with file. Compressing already compressed data such as JPEG images saves little.
Version applicability#
This entry describes GNU gzip 1.14 and XZ Utils 5.8. The -k option of gzip appeared in version 1.6.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0