Archive and Compression Tools
One objective naming three tools. The mechanics are quick; the two things worth the guide are that tar strips the leading `/` from absolute paths - so an archive of `/etc` extracts relative to wherever you happen to be - and that gzip replaces the file it compresses unless told otherwise.
Essential Tools Guide 9 of 67 Beginner
- OSRHEL 10.0 (Coughlan)
- Kernel6.12.0-55.9.1.el10_0
- dnf4.20.0
- Flatpak1.16.0
- TimeAbout 13 min
- Reviewed23 August 2026
Written against the versions above. `-z` gzip, `-j` bzip2, `-J` xz. Modern tar detects the compression when reading, so `tar -xf` unpacks any of them without naming the format; the flag is only required when creating.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RHCSA-A01 | 192.168.0.31 | RHEL 10.0 (Coughlan) | Practice node (graded) - spare /dev/sda | 2 Core | 4 GB | 50 GB + 15 GB |
Before you start
- A shell.
- The session builds and removes
~/arc.
-
An archive is not compression
src/ src/f1.txt src/f2.txt src/f3.txttar -cfcreates - create, file - and does no compression at all. A tar file is a concatenation of its members plus headers, which is why it is slightly larger than the sum of the files.-tlists the contents without extracting, and it is the command to run before every extraction. It shows you what paths the archive will produce, which is the difference between unpacking into a directory and spraying files across your home.The three flags to keep:
-ccreate,-tlist,-xextract. Exactly one of them per command.bash Example session cd ~/arc && tar -cf plain.tar src && ls -l plain.tar-rw-r--r--. 1 sysadmin sysadmin 71680 Aug 23 11:08 plain.tarcd ~/arc && tar -tf plain.tarsrc/src/f1.txtsrc/f2.txtsrc/f3.txtExpected resultAn uncompressed archive, and its contents listed.
Success conditionYou always look inside an archive before unpacking it.
-
The three compressors, measured
-rw-r--r--. 1 sysadmin sysadmin 30720 plain.tar -rw-r--r--. 1 sysadmin sysadmin ... gz.tar.gz -rw-r--r--. 1 sysadmin sysadmin ... bz.tar.bz2 -rw-r--r--. 1 sysadmin sysadmin ... xz.tar.xzSame content, four sizes. As a rule xz compresses hardest and slowest, gzip is fastest, bzip2 sits between them - on this small sample the differences are modest, and on a real backup they are not.
fileconfirms what each actually is rather than trusting the extension, which matters because an archive named.tar.gzis not necessarily gzipped.And reading needs no flag:
tar -tf gz.tar.gz src/ src/f1.txt --- tar detects the compression on read, no flag neededSo
tar -xfworks. The compression flag is only needed when creating, and the exam phrasing "create a gzip-compressed archive" is asking for-zspecifically.bash Example session cd ~/arc && tar -czf gz.tar.gz src && tar -cjf bz.tar.bz2 src && tar -cJf xz.tar.xz src && ls -l plain.tar gz.tar.gz bz.tar.bz2 xz.tar.xz-rw-r--r--. 1 sysadmin sysadmin 9414 Aug 23 11:08 bz.tar.bz2-rw-r--r--. 1 sysadmin sysadmin 6845 Aug 23 11:08 gz.tar.gz-rw-r--r--. 1 sysadmin sysadmin 71680 Aug 23 11:08 plain.tar-rw-r--r--. 1 sysadmin sysadmin 5608 Aug 23 11:08 xz.tar.xzcd ~/arc && for f in gz.tar.gz bz.tar.bz2 xz.tar.xz; do printf '%-14s %s\n' "$f" "$(file -b $f | cut -c1-40)"; donegz.tar.gz gzip compressed data, from Unix, originabz.tar.bz2 bzip2 compressed data, block size = 900kxz.tar.xz XZ compressed data, checksum CRC64cd ~/arc && tar -tf gz.tar.gz | head -3; echo "--- tar detects the compression on read, no flag needed"src/src/f1.txtsrc/f2.txt--- tar detects the compression on read, no flag neededExpected resultFour archives of different sizes, and tar auto-detecting on read.
Success conditionYou can pick a compressor and read any archive without knowing its format.
-
Extracting where you meant to
out/src/f1.txt out/src/f2.txt out/src/f3.txt-Cchanges directory before extracting, and it is the habit that prevents accidents. Without it, tar unpacks into the current directory, and an archive whose members are namedetc/,home/or.bashrcwill happily overwrite what is already there.You can also pull one member out:
tar -xf gz.tar.gz -C out src/f1.txtnaming the path exactly as
-tprinted it. That is why listing first matters - the member name must match, and it will not be the absolute path you archived.bash Example session cd ~/arc && mkdir -p out && tar -xf gz.tar.gz -C out && find out -type f | sortout/src/f1.txtout/src/f2.txtout/src/f3.txtcd ~/arc && tar -xf gz.tar.gz -C out src/f1.txt && ls -l out/src/f1.txt-rw-r--r--. 1 sysadmin sysadmin 20000 Aug 23 11:08 out/src/f1.txtExpected resultA whole archive and then a single member, both extracted into
out.Success conditionYou extract into a directory you chose.
-
The leading slash
Archive two absolute paths and tar says something:
tar: Removing leading `/' from member names tar: Removing leading `/' from hard link targetsThat is not a warning to skim. Look at what was stored:
etc/hostname etc/hostsRelative paths. So extraction lands wherever you are standing:
absout/etc/hostname absout/etc/hostsThe files went to
absout/etc/, not to/etc/. This is deliberate and it is a safety feature - an archive of/etccannot silently overwrite the real/etcjust by being unpacked.The consequence for restoring a backup: you must extract from
/to put files back where they came from,sudo tar -xf backup.tar -C /. Doing that from the wrong directory is how people restore a backup into their home directory and wonder why nothing changed.bash Example session cd ~/arc && tar -cf abs.tar /etc/hostname /etc/hosts 2>&1 | head -2tar: Removing leading `/' from member namestar: Removing leading `/' from hard link targetscd ~/arc && tar -tf abs.taretc/hostnameetc/hostscd ~/arc && mkdir -p absout && tar -xf abs.tar -C absout && find absout | sortabsoutabsout/etcabsout/etc/hostnameabsout/etc/hostsExpected resultAbsolute paths stored relative, and extracting under the current directory.
Success conditionYou know where a restored backup will actually land.
-
gzip and bzip2 without tar
The objective names them separately because they work on single files, and they behave in a way that surprises people:
-rw-r--r--. 1 sysadmin sysadmin 20000 solo.txt -rw-r--r--. 1 sysadmin sysadmin ... solo.txt.gz --- note the ORIGINAL is gonegzipreplaces the file. There is nosolo.txtany more - it becamesolo.txt.gz.gunzipreverses it. That is the opposite oftar, which leaves its input alone, and it is why compressing a file you have not backed up feels alarming the first time.-kkeeps the original:solo.txt solo.txt.bz2And both have a read-without-unpacking form -
zcatfor gzip,bunzip2 -c(orbzcat) for bzip2. Combined with a pipe that lets you inspect a compressed archive without ever writing it to disk, which on a machine short of space is the only way.bash Example session cd ~/arc && cp src/f1.txt solo.txt && ls -l solo.txt && gzip solo.txt && ls -l solo.txt.gz; echo "--- note the ORIGINAL is gone"-rw-r--r--. 1 sysadmin sysadmin 20000 Aug 23 11:08 solo.txt-rw-r--r--. 1 sysadmin sysadmin 6085 Aug 23 11:08 solo.txt.gz--- note the ORIGINAL is gonecd ~/arc && gunzip solo.txt.gz && ls -l solo.txt-rw-r--r--. 1 sysadmin sysadmin 20000 Aug 23 11:08 solo.txtcd ~/arc && bzip2 -k solo.txt && ls -l solo.txt solo.txt.bz2; echo "--- -k keeps the original"-rw-r--r--. 1 sysadmin sysadmin 20000 Aug 23 11:08 solo.txt-rw-r--r--. 1 sysadmin sysadmin 6424 Aug 23 11:08 solo.txt.bz2--- -k keeps the originalcd ~/arc && zcat gz.tar.gz | tar -tf - | head -2; echo "--- and reading without extracting:"; bunzip2 -c solo.txt.bz2 | head -2src/src/f1.txt--- and reading without extracting:108010-pointExpected resultgzip consuming its input,
-kpreserving it, and reading without unpacking.Success conditionYou will not lose a file to
gzip. -
Preserving permissions and contexts
-rw-r--r-- sysadmin/sysadmin ... src/f1.txt-ppreserves permissions, and on RHEL the flags that matter beyond that are--selinux --acls --xattrs. Without them an archive of a directory with SELinux contexts restores files that are labelled wrong, and on a machine with SELinux enforcing that means services cannot read them - a failure that looks like a permissions problem and is not.-vprints members as it works, which is worth having on a long archive so you can see it progressing.For the exam:
tar -czvf name.tar.gz /pathto create, andtar -xvf name.tar.gz -C /destto extract, are the two commands to have in your fingers. Everything else can be looked up.bash Example session cd ~/arc && sudo tar -cpf perms.tar --selinux --acls --xattrs src && sudo tar -tvf perms.tar | head -3drwxr-xr-x sysadmin/sysadmin 0 2026-08-23 11:08 src/-rw-r--r-- sysadmin/sysadmin 20000 2026-08-23 11:08 src/f1.txt-rw-r--r-- sysadmin/sysadmin 20000 2026-08-23 11:08 src/f2.txtcd ~/arc && tar -czvf verbose.tar.gz src 2>&1 | head -4src/src/f1.txtsrc/f2.txtsrc/f3.txtExpected resultAn archive carrying permissions, and verbose output.
Success conditionYou can archive a directory without losing its SELinux labels.
Troubleshooting
tar: Removing leading '/' from member names.Why: Normal, and intentional. Paths are stored relative.
Fix:Extract with
-C /to restore to the original location.Extracting sprayed files into the current directory.
Why: The archive had no top-level directory.
Fix:
tar -tffirst, and always extract with-C <dir>.gzip deleted the file.
Why: gzip replaces its input by design.
Fix:
gzip -kto keep it. Same forbzip2 -kandxz -k.Restored files are denied by SELinux.
Why: The archive did not carry contexts.
Fix:Archive with
--selinux --xattrs, or runrestorecon -Rvon the restored tree.