Archive and compress files — the standard Linux backup and packaging tool.
medium risk
tar bundles files into an archive. Combined with gzip (-z), bzip2 (-j), or xz (-J) it compresses too. The -C flag changes extraction directory. Always inspect archives with -t before extracting to avoid surprises.
| Flag | Meaning |
|---|---|
-c --create | Create a new archive |
-x --extract | Extract files from an archive |
-t --list | List archive contents without extracting |
-z --gzip | Compress/decompress with gzip (.tar.gz) |
-j --bzip2 | Compress/decompress with bzip2 (.tar.bz2) |
-J --xz | Compress/decompress with xz (.tar.xz) — best compression ratio |
-f --file | Specify archive filename |
-v --verbose | Verbosely list files processed |
-C --directory | Change to directory before extracting |
tar -czf backup-$(date +%F).tar.gz /var/www/htmlCreate date-stamped compressed backup of web root · explain & risk-check →tar -xzf archive.tar.gz -C /opt/app/Extract archive into /opt/app/ · explain & risk-check →tar -tzf archive.tar.gz | head -20Preview compressed archive contents before extracting · explain & risk-check →Exclude directories on archive: `tar --exclude='./logs' --exclude='./.git' -czf archive.tar.gz .`
Reference text is hand-written and may be incomplete — check man tar for your version. Test destructive commands on a non-production system first.