هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
المهارات في هذا الفصل
- إنشاء أرشيفات مضغوطة باستخدام tar
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.
| Compression | tar option | File ending | Speed / size |
|---|---|---|---|
| gzip | -z | .tar.gz, .tgz | Fast, good — the usual choice |
| bzip2 | -j | .tar.bz2 | Slower, a little smaller |
| xz | -J | .tar.xz | Slowest to pack, smallest file |
| zstd | --zstd | .tar.zst | Very fast with good size |
| none | — | .tar | Only packing |
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| Option | Meaning |
|---|---|
-c | Create a new archive |
-f FILE | The archive file (put it last in a group: -czf FILE) |
-v | Verbose: show every file |
-z / -j / -J | gzip / bzip2 / xz |
-C DIR | Change to DIR first (no long paths in the archive) |
--exclude=PATTERN | Leave out matching files |
ملاحظة
tar removes the leading / from names ("Removing leading '/' from member names"). This is good: unpacking can not overwrite your real /etc by mistake.
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/fstabtar detects the compression by itself when you list or unpack, so -z, -j or -J is not needed there.
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'انتبه
Unpacking in / as root writes over live system files. Unpack into an empty test folder first, compare, then copy what you need.
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| Option | Keeps |
|---|---|
-p | Rights (root keeps them by default on extract) |
--same-owner | Owners (default for root) |
--acls | POSIX ACLs |
--xattrs | Extended attributes |
--selinux | SELinux contexts |
تذكّر
After restoring files somewhere new, run restorecon -Rv PATH anyway — the labels must fit the new place.
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| Tool | Pack / unpack | Read without unpacking |
|---|---|---|
| gzip | gzip / gunzip | zcat, zless, zgrep |
| bzip2 | bzip2 / bunzip2 | bzcat, bzgrep |
| xz | xz / unxz | xzcat, xzgrep |
| zip | zip -r / unzip | unzip -l (list) |
في العمل
Use zip only to share files with Windows users. For Linux backups, tar keeps much more information (owners, links, labels).
Check an archive after you copy it
A checksum proves that the archive arrived unchanged.