Backups, releases and file transfers are almost always tarballs.
tar bundles files and directories, with permissions and timestamps, into one archive; a compressor then shrinks it. The flags read like a sentence:
ccreate,xextract,tlist the contents,f filethe archive file name (put it last in the flag group),zgzip,Jxz,--zstdzstd, orato choose by the file extension,vverbose,-C dirchange into a directory first.
So tar -czf backup.tar.gz dir/ creates, tar -tzf backup.tar.gz lists, and tar -xzf backup.tar.gz -C /restore extracts into a directory. Modern GNU tar detects compression automatically on extraction, so tar -xf is usually enough. List before extracting archives from elsewhere: some contain absolute paths or a pile of files without a top-level directory.
Compressors trade speed for size: gzip is universal, xz is small but slow, zstd is fast with a good ratio and has become the modern default for many projects. Single files can be compressed directly: gzip file replaces it with file.gz (-k keeps the original), gunzip or zcat reverse it.
zip and unzip handle the format common on Windows. For copying directories between machines, rsync -a or tar piped through ssh preserve metadata.
Example
cd "$(mktemp -d)"
mkdir -p site/css && echo '<h1>hi</h1>' > site/index.html && echo 'body{}' > site/css/a.css
tar -czf site.tar.gz site/ # c=create z=gzip f=file
tar -tzf site.tar.gz | sort # t=list: site/ site/css/ site/css/a.css site/index.html
mkdir restore && tar -xf site.tar.gz -C restore # x=extract (compression auto-detected)
ls restore/site # css index.html
tar -caf site.tar.xz site/ # -a: compression chosen from the extension
echo 'log line' > app.log
gzip app.log && ls app.log* # app.log.gz (original replaced)
zcat app.log.gz # log line
gunzip app.log.gz && ls app.log* # app.log
# tar -cf - site/ | ssh backup 'tar -xf - -C /srv' # copy a tree over sshRemember
- tar -czf create, -tzf list, -xf extract, -C target directory
- gzip is universal, zstd fast, xz small
- List untrusted archives before extracting them
Watch out for
- Extracting an unknown archive in your home directory and getting hundreds of loose files.