Compressing and Archiving Files
1. Creating tar Archives
| Option | Values | Description |
|---|---|---|
| -c | tar -cf archive.tar file1 file2 |
Create a new tar archive |
| -f | tar -cf archive.tar |
Specify the archive file name |
| -v | tar -cvf archive.tar file1 |
Verbose mode to show progress |
| -z | tar -czf archive.tar.gz file1 |
Compress with gzip |
| -j | tar -cjf archive.tar.bz2 file1 |
Compress with bzip2 |
| -J | tar -cJf archive.tar.xz file1 |
Compress with xz |
2. Extracting tar Archives
| Command | Description | Example |
|---|---|---|
| tar -xzf archive.tar.gz | Extract a gzipped tar archive | tar -xzf myapp.tar.gz |
| tar -xf archive.tar | Extract a tar archive (no compression) | tar -xf documents.tar |
| tar -xzf archive.tar.gz -C /destination | Extract to a specific directory | tar -xzf backup.tar.gz -C /home/user/backup |
| tar -tzf archive.tar.gz | List contents of a gzipped tar archive (without extracting) | tar -tzf logs.tar.gz |
Note: The
-z flag is used for gzip compression, -f specifies the archive file, and -x extracts files.Warning: Be cautious when extracting archives into directories with existing files; use
-k to keep existing files or -i to prompt before overwriting.3. Listing tar Contents
| Option | Values | Description |
|---|---|---|
| -t | tar -tf archive.tar |
List contents of a tar archive |
| -v | tar -tvf archive.tar |
Verbose listing with file details |
| --exclude | tar --exclude='*.log' -tvf archive.tar |
Exclude files matching pattern from listing |
4. Compressing with gzip
| Command | Description | Example |
|---|---|---|
| gzip file.txt | Compresses a file using gzip algorithm | gzip file.txt |
| gzip -c file.txt > file.txt.gz | Compresses to stdout and redirects to a file | gzip -c file.txt > file.txt.gz |
| gzip -d file.txt.gz | Decompresses a .gz file | gzip -d file.txt.gz |
| gzip -l file.txt.gz | Lists information about the compressed file | gzip -l file.txt.gz |
| gzip -r directory/ | Recursively compresses all files in a directory | gzip -r directory/ |
Note: The
.gz extension is commonly used for gzip-compressed files.Warning: Using
gzip without specifying a destination will overwrite the original file.Common gzip Options
-c: Write to stdout-d: Decompress-l: List file statistics-r: Recursively process directories-v: Verbose output
5. Compressing with bzip2
| Command | Description | Example |
|---|---|---|
| bzip2 | Compresses files using the bzip2 algorithm | bzip2 file.txt |
| bunzip2 | Decompresses files compressed with bzip2 | bunzip2 file.txt.bz2 |
| bzcat | Displays the contents of a bzip2 compressed file without decompressing it | bzcat file.txt.bz2 |
| bzip2 -l | Lists information about bzip2 compressed files | bzip2 -l file.txt.bz2 |
Note: bzip2 typically achieves better compression ratios than gzip but at the cost of speed.
6. Compressing with xz
| Command | Description | Example |
|---|---|---|
| xz | Compress a file using xz compression | xz file.txt |
| xz -k | Keep original file after compression | xz -k file.txt |
| xz -v | Verbose output during compression | xz -v file.txt |
| xz -9 | Use maximum compression level (9) | xz -9 file.txt |
| xz --help | Show help information | xz --help |
Note: The xz format provides better compression ratios than gzip, but at the cost of higher CPU usage.
7. Creating zip Archives
| Command | Description | Example |
|---|---|---|
| zip | Create a zip archive from files or directories | zip -r archive.zip folder/ |
| zip -r | Recursively include all subdirectories | zip -r archive.zip folder/ |
| zip -9 | Compress with maximum compression level | zip -9 archive.zip file.txt |
| zip -q | Quiet mode, suppress output | zip -q archive.zip file.txt |
| zip -j | Store files without directory structure | zip -j archive.zip file.txt |
Example: Creating a compressed archive with maximum compression
zip -9 archive.zip /home/user/documents/*.txt
Note: The
zip command is part of the Info-ZIP suite and is available on most Unix-like systems including Linux.
Warning: Using
zip -r on large directories can consume significant disk space and time.
8. Extracting zip Archives
| Command | Description | Options | Example |
|---|---|---|---|
| unzip | Extract files from a ZIP archive | -l list contents-d extract to directory-o overwrite without prompt |
Example: Extracting a ZIP file unzip archive.zip |
| unzip -l | List contents of a ZIP archive without extracting | None | Example: Listing contents of a ZIP file unzip -l archive.zip |
| unzip -d | Extract files to a specific directory | -d destination directory |
Example: Extracting to a specific directory unzip -d /path/to/extract archive.zip |
9. Using 7-Zip
| Command | Description | Example |
|---|---|---|
| 7z a | Create a new archive | 7z a myarchive.7z file1.txt file2.txt |
| 7z x | Extract archive to current directory | 7z x myarchive.7z |
| 7z e | Extract archive without creating directory structure | 7z e myarchive.7z |
| 7z l | List contents of archive | 7z l myarchive.7z |
| 7z u | Update existing archive with new files | 7z u myarchive.7z newfile.txt |
| 7z d | Delete files from archive | 7z d myarchive.7z fileToDelete.txt |
Note: 7-Zip supports various formats including
.7z, .zip, .tar, and more. It offers better compression ratios than standard gzip or bzip2.Warning: Passwords in 7-Zip are not encrypted with strong algorithms by default. For sensitive data, consider using AES-256 encryption explicitly.
10. Compressing with zstd
| Command | Description | Options | Example |
|---|---|---|---|
| zstd | Compress a file using zstd compression | -b (block size), -c (stdout), -f (force overwrite) |
zstd file.txt |
| zstd -d | Decompress a file compressed with zstd | -c (stdout), -f (force overwrite) |
zstd -d file.txt.zst |
| zstd -c | Compress to stdout (useful for piping) | -b, -f |
zstd -c file.txt | gzip > file.txt.zst.gz |
| zstd -l | List metadata of zstd compressed files | -v (verbose) |
zstd -l file.txt.zst |
Example: Compressing a directory with zstd
tar -cf - /path/to/directory | zstd -b 1048576 -c > directory.tar.zst
Note:
zstd offers high compression ratios and fast decompression, making it ideal for large files or backups.