Solaris · Archive

Solaris · Lesson 13

Archive

Create tar and cpio archives. Backup and restore methods. Compression utilities. Admin level examples.

Archive vs compression – core idea

Many admins mix the words archive and compression, but they are different concepts:

  • Archive = combine multiple files/directories into a single file (tar).
  • Compression = reduce the size of data (gzip, bzip2, xz).
  • In practice you often do tar + compression → e.g. .tar.gz, .tar.bz2, .tar.xz.

Compression types: speed vs ratio

gzip (.gz)

Fastest in most cases, good compression ratio. Ideal for backups and logs you compress regularly.

bzip2 (.bz2)

Slower than gzip but usually better compression. Good when you want smaller archives and can wait a bit longer.

xz (.xz)

Often gives the best compression ratio, but slowest. Use for long-term archives or software distributions.

Step-by-step archive and compression commands

Use these flows in your Solaris lab VM. Work in a test directory so you can create and remove archives freely.

1. Archive vs compression – see the difference

Archiving (tar) combines many files into one. Compression (gzip/bzip2/xz) reduces size. Often you archive first, then compress.

terminal — archive
solaris-lab
[root@solaris ~]# ls project
config.yml index.html script.sh README.txt
 
# Archive only (no compression)
[root@solaris ~]# tar cvf project.tar project
a project/
a project/config.yml
a project/index.html
a project/script.sh
a project/README.txt
 
[root@solaris ~]# ls -lh project.tar
-rw-r--r-- 1 root root 40K Jan 11 12:10 project.tar
 
# Compress a single file
[root@solaris ~]# gzip README.txt
[root@solaris ~]# ls
project project.tar README.txt.gz

2. gzip – fast compression, good enough for most cases

gzip is usually the default choice: decent compression with good speed.

terminal — archive
solaris-lab
[root@solaris ~]# ls -lh bigfile.log
-rw-r--r-- 1 root root 500M Jan 11 11:00 bigfile.log
 
[root@solaris ~]# gzip bigfile.log
[root@solaris ~]# ls -lh bigfile.log.gz
-rw-r--r-- 1 root root 50M Jan 11 11:05 bigfile.log.gz
 
# View without decompressing permanently:
[root@solaris ~]# zcat bigfile.log.gz | head
Jan 11 10:00:01 sol11 app[1001]: starting up...
...
 
# Decompress back
[root@solaris ~]# gunzip bigfile.log.gz
[root@solaris ~]# ls -lh bigfile.log
-rw-r--r-- 1 root root 500M Jan 11 11:00 bigfile.log

3. bzip2 & xz – better compression, slower speed

bzip2 and xz usually give a smaller file than gzip, but take more CPU/time. Use them for archives you rarely touch.

terminal — archive
solaris-lab
[root@solaris ~]# ls -lh db_dump.sql
-rw-r--r-- 1 root root 2.0G Jan 11 10:30 db_dump.sql
 
# bzip2: better ratio than gzip, slower
[root@solaris ~]# bzip2 db_dump.sql
[root@solaris ~]# ls -lh db_dump.sql.bz2
-rw-r--r-- 1 root root 600M Jan 11 10:40 db_dump.sql.bz2
 
# View without full decompression:
[root@solaris ~]# bzcat db_dump.sql.bz2 | head
CREATE DATABASE training;
...
 
# xz: often best ratio, slowest
[root@solaris ~]# xz -k db_dump.sql.bz2
[root@solaris ~]# ls -lh db_dump.sql.bz2.xz
-rw-r--r-- 1 root root 400M Jan 11 10:50 db_dump.sql.bz2.xz
 
# Decompress:
[root@solaris ~]# bunzip2 db_dump.sql.bz2
[root@solaris ~]# unxz db_dump.sql.bz2.xz

4. Create and extract tar archives (no compression)

Use tar cvf to create an archive, tvf to list it, and xvf to extract it.

terminal — archive
solaris-lab
[root@solaris ~]# ls appdir
bin conf logs
 
[root@solaris ~]# tar cvf appdir.tar appdir
a appdir/
a appdir/bin/
a appdir/bin/start.sh
a appdir/conf/app.conf
a appdir/logs/app.log
 
[root@solaris ~]# ls -lh appdir.tar
-rw-r--r-- 1 root root 5.0M Jan 11 12:20 appdir.tar
 
# List contents without extracting
[root@solaris ~]# tar tvf appdir.tar
drwxr-xr-x root/root 0 Jan 11 12:20 appdir/
drwxr-xr-x root/root 0 Jan 11 12:20 appdir/bin/
-rwxr-xr-x root/root 1024 Jan 11 12:20 appdir/bin/start.sh
...
 
# Extract into current directory
[root@solaris ~]# tar xvf appdir.tar

5. tar with compression: gzip, bzip2, xz

Combine archiving and compression in a single tar command using -z (gzip), -j (bzip2) or -J (xz).

terminal — archive
solaris-lab
[root@solaris ~]# ls project
config.yml index.html script.sh README.txt
 
# tar + gzip → .tar.gz or .tgz (fastest)
[root@solaris ~]# tar czvf project.tar.gz project
[root@solaris ~]# tar tzvf project.tar.gz | head
drwxr-xr-x root/root 0 Jan 11 12:30 project/
-rw-r--r-- root/root 2048 Jan 11 12:30 project/config.yml
...
 
# tar + bzip2 → .tar.bz2 (smaller, slower)
[root@solaris ~]# tar cjvf project.tar.bz2 project
[root@solaris ~]# tar tjvf project.tar.bz2 | head
...
 
# tar + xz → .tar.xz (smallest, slowest)
[root@solaris ~]# tar cJvf project.tar.xz project
[root@solaris ~]# tar tJvf project.tar.xz | head
...

6. Extract compressed tar archives

Use xzvf / xjvf / xJvf to extract tar archives compressed with gzip, bzip2 and xz.

terminal — archive
solaris-lab
[root@solaris ~]# ls
project.tar.gz project.tar.bz2 project.tar.xz
 
# Extract gzip tarball
[root@solaris ~]# tar xzvf project.tar.gz
 
# Extract bzip2 tarball
[root@solaris ~]# tar xjvf project.tar.bz2
 
# Extract xz tarball
[root@solaris ~]# tar xJvf project.tar.xz

Quick command cheat sheet

Compression only

  • gzip file → creates file.gz
  • gunzip file.gz → restore original file
  • zcat file.gz → view compressed file
  • bzip2 file → creates file.bz2
  • bunzip2 file.bz2 → restore original
  • bzcat file.bz2 → view compressed file
  • xz file → creates file.xz
  • unxz file.xz → restore original
  • xzcat file.xz → view compressed file

tar + optional compression

  • tar cvf file.tar dir/ → archive only
  • tar tvf file.tar → list
  • tar xvf file.tar → extract
  • tar czvf file.tar.gz dir/ → tar + gzip
  • tar cjvf file.tar.bz2 dir/→ tar + bzip2
  • tar cJvf file.tar.xz dir/ → tar + xz
  • tar tzvf file.tar.gz → list gzip tar
  • tar tjvf file.tar.bz2 → list bzip2 tar
  • tar tJvf file.tar.xz → list xz tar
  • tar xzvf / xjvf / xJvf → extract compressed tarballs

Practice task – backup and restore a small project

  • Create a test directory with a few subdirectories and files (scripts, configs, logs).
  • Create archives using tar cvf, tar czvf, tar cjvf and tar cJvf, and compare resulting sizes.
  • Practice listing and extracting archives safely in a separate directory so you don't overwrite existing files.
  • Decide which compression type you will use by default in your environment (speed vs size) and document your choice.

Archive and compression skills will be used again for log rotation, backups and moving data between Solaris and Linux systems.