Linux File Compression and Backup
📦 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/restore
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
cpio [options] < archivecpio [options] > archive
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
- 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.tar
3. compress
& gzip
– File Compression
These tools compress individual files.
compress
- Produces
.Z
files. - Compress:
compress file.txt
- Decompress:
compress -d file.txt.Z
gzip
- Produces
.gz
files. - Compress:
gzip file.txt
- Decompress:
gzip -d file.txt.gz
- View compressed file without decompressing:
zcat file.txt.gz
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
- Always test restores to ensure backups are valid.
- Use compression for storage efficiency, but note that it increases CPU usage.
- For large datasets, consider
rsync
orborgbackup
for incremental backups. - Store backups offsite or in cloud storage for disaster recovery.