π¦ 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
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
tar [options] [archive-file] [files...]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
- 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
cpio reads and writes archives from standard input/output. Itβs often used with find for flexible file selection.
Basic Syntax
cpio [options] < archive
cpio [options] > archiveCommon 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
- 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
These tools compress individual files.
compress
- Produces
.Zfiles. - Compress:
compress file.txt- Decompress:
compress -d file.txt.Zgzip
- Produces
.gzfiles. - Compress:
gzip file.txt- Decompress:
gzip -d file.txt.gz- View compressed file without decompressing:
zcat file.txt.gz4. 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
- 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.