Linux File Compression and Backup
📦 UNIX File Compression & Backup Guide
Section titled “📦 UNIX File Compression & Backup Guide”UNIX systems provide several utilities for archiving and compressing files. This guide covers:
tar– Tape Archivercpio– Copy In/Out Archivercompress&gzip– File Compression Tools- Modern Alternatives –
bzip2,xz,zip, andrsync
1. tar – Tape Archiver
Section titled “1. tar – Tape Archiver”tar is used to bundle multiple files and directories into a single archive file. It preserves file metadata (permissions, ownership, timestamps) but does not compress by default.
Basic Syntax
Section titled “Basic Syntax”tar [options] [archive-file] [files...]Common Options
Section titled “Common Options”| Option | Meaning |
|---|---|
-c | Create a new archive |
-x | Extract files from an archive |
-t | List archive contents |
-v | Verbose output (show processed files) |
-f | Specify archive file name |
-z | Compress with gzip |
-j | Compress with bzip2 |
-J | Compress with xz |
--exclude=PATTERN | Exclude files matching pattern |
Examples
Section titled “Examples”- Create an archive:
tar -cvf backup.tar /home/user/documents- Create and compress with gzip:
tar -czvf backup.tar.gz /home/user/documents- List contents:
tar -tvf backup.tar- Extract archive:
tar -xvf backup.tar- Extract to a specific directory:
tar -xvf backup.tar -C /tmp/restore2. cpio – Copy In/Out Archiver
Section titled “2. cpio – Copy In/Out Archiver”cpio reads and writes archives from standard input/output. It’s often used with find for flexible file selection.
Basic Syntax
Section titled “Basic Syntax”cpio [options] < archivecpio [options] > archiveCommon Options
Section titled “Common Options”| Option | Meaning |
|---|---|
-o | Create archive (copy-out mode) |
-i | Extract archive (copy-in mode) |
-t | List archive contents |
-v | Verbose output |
-d | Create directories as needed |
-u | Overwrite existing files |
-H | Specify archive format (tar, crc, odc, etc.) |
Examples
Section titled “Examples”- Create a tar-format archive from current directory:
find . -depth -print | cpio -ov -Htar > backup.tar- List contents:
cpio -tv < backup.tar- Extract files:
cpio -idv < backup.tar- Extract and overwrite existing files:
cpio -iduv < backup.tar3. compress & gzip – File Compression
Section titled “3. compress & gzip – File Compression”These tools compress individual files.
compress
Section titled “compress”- Produces
.Zfiles. - Compress:
compress file.txt- Decompress:
compress -d file.txt.Z- Produces
.gzfiles. - Compress:
gzip file.txt- Decompress:
gzip -d file.txt.gz- View compressed file without decompressing:
zcat file.txt.gz4. Modern Alternatives & Improvements
Section titled “4. Modern Alternatives & Improvements”While tar, cpio, compress, and gzip are still widely used, modern tools offer better compression ratios and faster performance.
| Tool | Extension | Pros | Example |
|---|---|---|---|
bzip2 | .bz2 | Better compression than gzip | tar -cjvf backup.tar.bz2 dir/ |
xz | .xz | Very high compression ratio | tar -cJvf backup.tar.xz dir/ |
zip | .zip | Cross-platform compatibility | zip -r backup.zip dir/ |
rsync | N/A | Incremental backups over network | rsync -avz /source user@host:/backup |
5. Best Practices
Section titled “5. Best Practices”- Always test restores to ensure backups are valid.
- Use compression for storage efficiency, but note that it increases CPU usage.
- For large datasets, consider
rsyncorborgbackupfor incremental backups. - Store backups offsite or in cloud storage for disaster recovery.