On this page 12 sections
tar vs gzip
tar groups multiple files and directories into a single archive. gzip compresses data. A .tar.gz file therefore combines both operations: tar creates the archive and gzip compresses it.
Create a tar archive
The common tar flags are c for create, v for verbose and f for archive filename.
tar -cvf backup.tar /opt/sam0xtar -cf backup.tar /opt/sam0xCreate a compressed tar.gz archive
The z option enables gzip compression while creating or extracting a tar archive.
tar -czvf backup.tar.gz /opt/sam0xtar -czf backup.tar.gz /opt/sam0xExtract archives
The x option extracts an archive. gzip-compressed tar archives use z as well.
tar -xvf backup.tartar -xzvf backup.tar.gztar -xzf backup.tar.gz -C /tmp/restoreList contents without extracting
The t option lists files inside the archive. This is useful before extracting unknown archives.
tar -tf backup.tartar -tzf backup.tar.gzExtract a single file
A specific path inside the archive can be extracted without unpacking everything.
tar -xzf backup.tar.gz opt/sam0x/config.confExclude files or directories
--exclude prevents selected paths or patterns from being included in the archive.
tar -czf backup.tar.gz --exclude="*.log" /opt/sam0xtar -czf backup.tar.gz --exclude="/opt/sam0x/cache" /opt/sam0xgzip individual files
gzip compresses a single file and normally replaces it with a .gz version.
gzip access.loggunzip access.log.gzKeep original file
-k keeps the original file instead of replacing it.
gzip -k access.logCompression level
gzip supports compression levels from -1 to -9. Lower values are faster while higher values spend more time attempting better compression.
gzip -1 large.loggzip -9 large.logRead compressed text without extracting
zcat, zgrep and related tools can inspect compressed files directly.
zcat access.log.gzzgrep -i "error" access.log.gzUseful backup example
tar is frequently used to create quick backups of application directories before making changes.
tar -czf sam0x-backup-$(date +%F).tar.gz /opt/sam0x