الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

tar

Pack files and folders into one archive file (and unpack it), with optional compression.

المستوى
امتحان RHCSA
المجموعة
الأرشفة والضغط
يحتاج root
لا
الحزمة
tar

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

tar (tape archiver) collects many files, with their folders, permissions, owners and times, into one .tar file. With a compression option it also compresses the archive: .tar.gz (gzip), .tar.bz2 (bzip2) or .tar.xz (xz).

Every tar command has one main mode: -c create, -x extract, -t list (and some others). -f FILE names the archive; put it last in a group of letters (-czf backup.tar.gz).

GNU tar removes the leading / from names when it creates an archive. So extracting in another folder never overwrites the real system files by accident.

On extract, tar finds the compression by itself: tar -xf file.tar.xz works without -J.

طريقة الكتابة

tar -c [-f ARCHIVE] [OPTIONS] FILE...
tar -x [-f ARCHIVE] [OPTIONS] [MEMBER...]
tar -t [-f ARCHIVE] [OPTIONS] [MEMBER...]
tar {-r|-u} -f ARCHIVE [OPTIONS] FILE...

الخيارات 128

الأكثر استخدامًا

Main modes (pick one)

-c, --create
Create a new archive.
-x, --extract, --get
Extract files from an archive.
-t, --list
List the content of an archive.
-r, --append
Add files to the end of an archive (not for compressed archives).
-u, --update
Add only files that are newer than the copy in the archive.
-A, --catenate, --concatenate
Add other tar files to an archive.
-d, --diff, --compare
Compare the archive with the files on disk.
--delete
Remove members from an archive (not on tapes, not compressed).
--test-label
Test the volume label of the archive and exit.

Archive file and compression

-f, --file=archive
The archive file (or device). - = stdin/stdout.
-z, --gzip
Compress or decompress with gzip (.tar.gz, .tgz).
-j, --bzip2
Compress or decompress with bzip2 (.tar.bz2).
-J, --xz
Compress or decompress with xz (.tar.xz).
--zstd
Compress with zstd (.tar.zst).
-a, --auto-compress
Choose the compression from the archive name's ending.
-I, --use-compress-program=prog
Use this program for compression, like -I 'zstd -19' or pigz.
--lzma, --lzip, --lzop
Use these other compressors.
-Z, --compress
Use the old compress program (.tar.Z).
--no-auto-compress
Do not guess the compression from the name.

Show information

-v, --verbose
List the files while working. -vv shows details like ls -l.
--totals[=<signal>]
Print the total number of bytes at the end.
--checkpoint[=<n>]
Show a progress message every N records.
--checkpoint-action=action
What to do at each checkpoint (like dot or echo).
--index-file=file
Write the verbose list to this file.
--full-time
Show file times with full resolution.
--utc
Show times in UTC.
-R, --block-number
Show the block number of each message.
--show-defaults
Show the default settings of tar.
--show-omitted-dirs
Show folders that are skipped.
--show-transformed-names, --show-stored-names
Show names after --transform.
-w, --interactive, --confirmation
Ask for each file.
--warning=keyword
Turn certain warnings on or off.

Choose files

-C, --directory=dir
Change to this folder first (for extract: extract into it).
--exclude=pattern
Leave out files that match the pattern.
-X, --exclude-from=file
Read exclude patterns from a file.
--exclude-vcs
Leave out .git, .svn and other version control folders.
--exclude-vcs-ignores
Use the ignore files of git and others as exclude lists.
--exclude-backups
Leave out backup and lock files.
--exclude-caches, --exclude-caches-all, --exclude-caches-under
Leave out cache folders that have a CACHEDIR.TAG file.
--exclude-tag=file, --exclude-tag-all=file, --exclude-tag-under=file
Leave out folders that contain this file.
--exclude-ignore=file, --exclude-ignore-recursive=file
Read exclude patterns per folder from this file.
-T, --files-from=file
Read the names to archive or extract from a file.
--null
With -T: names are separated by NUL bytes (works with find -print0).
--no-null
Turn off --null again.
--add-file=file
Add this file, even if its name starts with a dash.
--no-recursion
Do not go into folders.
--recursion
Go into folders (the default).
--one-file-system
Stay on the file system where you started.
-N, --newer, --after-date=date|file
Only files newer than this date.
--newer-mtime=date
Only files whose data changed after this date.
-K, --starting-file=member
Begin at this member of the archive.
--occurrence[=<n>]
Use only the Nth copy of a member.
-h, --dereference
Archive the files that links point to, not the links.
--hard-dereference
Follow hard links and store the data each time.
-P, --absolute-names
Keep the leading / in names (dangerous on extract).
--anchored, --no-anchored
Patterns match at the start of the name / after any slash.
--ignore-case, --no-ignore-case
Ignore case in patterns, or not.
--wildcards, --no-wildcards
Use wildcards in patterns, or match exactly.
--wildcards-match-slash, --no-wildcards-match-slash
Let wildcards match a slash, or not.
--strip-components=n
On extract: remove the first N folders from each name.
--transform, --xform=sed-expr
Change names with a sed expression.

Extract behavior

-k, --keep-old-files
Do not replace existing files (report an error).
--skip-old-files
Do not replace existing files (skip silently).
--keep-newer-files
Do not replace files that are newer than the archive copy.
--overwrite
Overwrite existing files.
--overwrite-dir
Overwrite the metadata of existing folders.
--no-overwrite-dir
Keep the metadata of existing folders.
--one-top-level[=<dir>]
Extract everything into one new folder.
-O, --to-stdout
Extract files to the screen (stdout), not to disk.
--to-command=command
Send each extracted file to a command.
-m, --touch
Do not restore the modification time.
--remove-files
Remove the files from disk after adding them to the archive.

Owners, permissions, attributes

-p, --preserve-permissions, --same-permissions
Restore the exact permissions (default for root).
--no-same-permissions
Apply your umask on extract.
--same-owner
Restore the owners (default for root).
--no-same-owner
Extract files as yourself (default for normal users).
--numeric-owner
Use user and group numbers, not names.
--owner=name, --group=name
Set this owner or group for added files.
--owner-map=file, --group-map=file
Map owners and groups with a file.
--mode=changes
Set these permissions for added files.
--mtime=date|file
Set this modification time for added files.
--clamp-mtime
With --mtime: only change files newer than that time.
--atime-preserve[=<method>]
Keep the access times of the archived files.
--delay-directory-restore, --no-delay-directory-restore
Set folder times at the end (or not).
--sort=order
Order of folder entries: none, name, inode.
-s, --preserve-order, --same-order
Member list is in the order of the archive.
--acls, --no-acls
Store and restore POSIX ACLs (or not).
--selinux, --no-selinux
Store and restore SELinux contexts (or not).
--xattrs, --no-xattrs
Store and restore extended attributes (or not).
--xattrs-include=mask, --xattrs-exclude=mask
Which extended attributes to include or skip.

Format, devices and tapes

-H, --format=format
Archive format: gnu, oldgnu, pax, posix, ustar, v7.
--posix
Same as --format=posix.
-o, --old-archive, --portability
Old V7 format (with -c).
--pax-option=list
Options for the pax format.
-V, --label=text
Give the archive a volume name.
-S, --sparse
Store sparse files (like VM images) efficiently.
--sparse-version=ver
The sparse format to use.
--hole-detection=type
How to find holes in sparse files.
-g, --listed-incremental=file
Make an incremental backup using this snapshot file.
-G, --incremental
Old GNU incremental format.
--level=n
Dump level for listed-incremental archives.
--check-device, --no-check-device
Check device numbers for incremental backups (or not).
--ignore-failed-read
Do not stop when a file cannot be read.
-W, --verify
Verify the archive after writing.
-i, --ignore-zeros
Ignore blocks of zeros in the archive (read concatenated archives).
-B, --read-full-records
Reblock as you read (for pipes).
-b, --blocking-factor=blocks
Record size in 512-byte blocks.
--record-size=bytes
Record size in bytes.
-M, --multi-volume
Create, list or extract a multi-volume archive.
-L, --tape-length=n
Change the tape after N x 1024 bytes.
-F, --info-script, --new-volume-script=name
Run this script at the end of each tape.
--volno-file=file
Keep the volume number in a file.
-n, --seek, --no-seek
The archive can (or cannot) be seeked.
--force-local
The archive is local even if its name has a colon.
--rmt-command=cmd, --rsh-command=cmd
Programs for remote tapes.
--backup[=<control>]
Make a backup before removing a file on extract.
--suffix=string
The backup suffix.
--quoting-style=style, --quote-chars=chars, --no-quote-chars=chars
How names are quoted in output.
--unquote, --no-unquote
Unquote names in input (or not).
--verbatim-files-from, --no-verbatim-files-from
Read -T names literally (or as options).
--ignore-command-error, --no-ignore-command-error
Ignore exit codes of child programs (or not).
--restrict
Disable some options that could be harmful.
-?, --help
Show help.
--usage
Show a short usage message.
--version
Show the version.

أمثلة 13

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Back up a folder into a gzip archive

    tar -czvf /root/etc-$(date +%F).tar.gz /etc

    c = create, z = gzip, v = show files, f = archive name.

  2. 2List what is in an archive

    tar -tvf /root/etc-2026-10-25.tar.gz

    Look before you extract.

  3. 3Extract into another folder

    tar -xzvf /root/etc-2026-10-25.tar.gz -C /tmp/restore

    The folder must exist. The real /etc is not touched.

  4. 4Extract one file only

    tar -xzf backup.tar.gz -C /tmp etc/ssh/sshd_config

    Give the name as it is stored (without the leading slash).

  5. 5Create a bzip2 or xz archive

    tar -cjf docs.tar.bz2 docs/
    tar -cJf docs.tar.xz docs/

    xz makes the smallest file but takes more time.

  6. 6Leave out folders you do not need

    tar -czf app.tar.gz --exclude='*.log' --exclude='node_modules' --exclude-vcs app/

    Smaller backups and no secrets from .git folders.

  7. 7Keep SELinux labels and ACLs in a system backup

    tar --selinux --acls --xattrs -czpf /backup/www.tar.gz /var/www

    Without these options the labels are lost, and the restored site can give 403 errors.

  8. 8Remove the top folder when you extract

    tar -xzf terraform-1.9.tar.gz --strip-components=1 -C /opt/terraform

    Archives from the web often have one extra top folder.

  9. 9Copy a folder to another server through SSH

    tar -czf - /srv/data | ssh server2 "tar -xzf - -C /backup"

    No temporary file: - sends the archive through the pipe.

  10. 10Back up only files changed in the last day

    tar -czf /backup/changed.tar.gz -N "1 day ago" /home

    A simple differential backup.

  11. 11Make incremental backups with a snapshot file

    tar -czf /backup/home-full.tar.gz -g /backup/home.snar /home
    tar -czf /backup/home-inc1.tar.gz -g /backup/home.snar /home

    The second run stores only what changed since the first.

  12. 12Check a backup against the disk

    tar -dzf /root/etc-2026-10-25.tar.gz -C /

    Lists files that changed since the backup (useful in incident response).

  13. 13Read one file from an archive without extracting

    tar -xzOf backup.tar.gz etc/hosts | less

    -O sends the file content to the screen.

انتبه

  • -f takes the next word as the archive name: write -czf name.tar.gz, not -cfz.
  • Extracting as root restores owners and permissions; extracting in / overwrites system files.
  • Never extract an archive from an unknown source as root without listing it first (-t): names like ../../etc/passwd or absolute paths can be dangerous.
  • tar does not keep SELinux contexts and ACLs unless you add --selinux --acls --xattrs.

الملفات

/etc/rmt
The remote tape helper (rarely used).

نصائح للحفظ

  • c = create, x = extract, t = list, v = verbose, f = file, z/j/J = gz/bz2/xz, C = folder.
  • To remember: "eXtract Ze Files" = -xzf, "Create Ze File" = -czf.

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man tar