Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 7Name in the course: Archiving Files

Pack and compress files with tar

To back up a folder, move a web site or send many logs to support, you pack the files into one archive and make it smaller with compression. tar is the standard tool on every Linux. This chapter shows how to create, list and unpack archives, keep owners and SELinux labels, and choose the right compression.

  • 7 Lessons
  • 14 Commands
  • 1 Labs

$ run as a normal user# run as root (or with sudo)

Skills in this chapter

  • Make compressed archives with tar

7.1

Know archives and compression

An archive puts many files into one file (with names, folders, owners, rights). Compression makes a file smaller. tar can do both at once.

Compressiontar optionFile endingSpeed / size
gzip-z.tar.gz, .tgzFast, good — the usual choice
bzip2-j.tar.bz2Slower, a little smaller
xz-J.tar.xzSlowest to pack, smallest file
zstd--zstd.tar.zstVery fast with good size
none—.tarOnly packing

7.2

Create an archive

tar -c creates, -f FILE names the archive, -v lists the files. Add the compression letter.

tar -cf docs.tar ~/Documents
tar -czf docs.tar.gz ~/Documents
tar -cjf logs.tar.bz2 /var/log/*.log
tar -cJf etc.tar.xz /etc
tar -czvf /root/etc-$(date +%F).tar.gz /etc
tar -czf site.tar.gz -C /var/www html
tar -czf home.tar.gz --exclude='*.iso' --exclude='.cache' /home/student
OptionMeaning
-cCreate a new archive
-f FILEThe archive file (put it last in a group: -czf FILE)
-vVerbose: show every file
-z / -j / -Jgzip / bzip2 / xz
-C DIRChange to DIR first (no long paths in the archive)
--exclude=PATTERNLeave out matching files

Note

tar removes the leading / from names ("Removing leading '/' from member names"). This is good: unpacking can not overwrite your real /etc by mistake.

7.3

Look inside an archive

tar -t lists the content without unpacking. Check an archive before you trust it.

tar -tf docs.tar.gz
tar -tvf etc.tar.xz | head
-rw-r--r-- root/root       158 2026-10-20 10:12 etc/hosts
drwxr-xr-x root/root         0 2026-10-20 10:12 etc/ssh/
tar -tzf site.tar.gz | grep index
tar -tf etc.tar.xz etc/fstab

tar detects the compression by itself when you list or unpack, so -z, -j or -J is not needed there.

7.4

Unpack an archive

tar -x unpacks into the current folder (or into -C DIR). Unpack single files by naming them.

mkdir /tmp/restore
tar -xf docs.tar.gz -C /tmp/restore
tar -xvf etc.tar.xz -C /tmp/restore etc/fstab etc/hosts
tar -xzf site.tar.gz --strip-components=1 -C /var/www/new
tar -xf docs.tar.gz --wildcards '*.pdf'

Watch out

Unpacking in / as root writes over live system files. Unpack into an empty test folder first, compare, then copy what you need.

7.5

Keep rights, owners and SELinux labels

For a real backup of system files, keep owners, rights, ACLs, extended attributes and SELinux labels.

tar --selinux --acls --xattrs -czpf /backup/srv.tar.gz /srv
tar --selinux --acls --xattrs -xzpf /backup/srv.tar.gz -C /
restorecon -Rv /srv
OptionKeeps
-pRights (root keeps them by default on extract)
--same-ownerOwners (default for root)
--aclsPOSIX ACLs
--xattrsExtended attributes
--selinuxSELinux contexts

Remember

After restoring files somewhere new, run restorecon -Rv PATH anyway — the labels must fit the new place.

7.6

Compress single files

gzip, bzip2 and xz compress one file and replace it with FILE.gz / .bz2 / .xz. They do not pack folders.

gzip big.log
ls big.log*
big.log.gz
gunzip big.log.gz
gzip -k -9 dump.sql
xz -T0 disk.img
zcat access.log.gz | grep ' 500 '
zgrep 'error' /var/log/messages-*.gz
zip -r site.zip /var/www/html
unzip -l site.zip
ToolPack / unpackRead without unpacking
gzipgzip / gunzipzcat, zless, zgrep
bzip2bzip2 / bunzip2bzcat, bzgrep
xzxz / unxzxzcat, xzgrep
zipzip -r / unzipunzip -l (list)

At work

Use zip only to share files with Windows users. For Linux backups, tar keeps much more information (owners, links, labels).

7.7

Check an archive after you copy it

A checksum proves that the archive arrived unchanged.

sha256sum etc-2026-10-27.tar.gz > etc-2026-10-27.tar.gz.sha256
scp etc-2026-10-27.tar.gz* backup@server2:/backup/
ssh backup@server2 'cd /backup && sha256sum -c etc-2026-10-27.tar.gz.sha256'
etc-2026-10-27.tar.gz: OK
gzip -t etc-2026-10-27.tar.gz && echo "archive is not damaged"

Practice

All commands in this chapter