Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
Wat het doet 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.
Zo schrijf je het 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... Meest gebruikt
Main modes (pick one) -c, --createCreate a new archive. -t, --listList the content of an archive. -r, --appendAdd files to the end of an archive (not for compressed archives). -u, --updateAdd only files that are newer than the copy in the archive. -A, --catenate, --concatenateAdd other tar files to an archive. -d, --diff, --compareCompare the archive with the files on disk. --deleteRemove members from an archive (not on tapes, not compressed). --test-labelTest the volume label of the archive and exit. Archive file and compression -f, --file=archive The archive file (or device). - = stdin/stdout. -z, --gzipCompress or decompress with gzip (.tar.gz, .tgz). -j, --bzip2Compress or decompress with bzip2 (.tar.bz2). -J, --xzCompress or decompress with xz (.tar.xz). --zstdCompress with zstd (.tar.zst). -a, --auto-compressChoose 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, --lzopUse these other compressors. -Z, --compressUse the old compress program (.tar.Z). --no-auto-compressDo not guess the compression from the name. Show information -v, --verboseList 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-timeShow file times with full resolution. -R, --block-numberShow the block number of each message. --show-defaultsShow the default settings of tar. --show-omitted-dirsShow folders that are skipped. -w, --interactive, --confirmationAsk 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-vcsLeave out .git, .svn and other version control folders. --exclude-vcs-ignoresUse the ignore files of git and others as exclude lists. --exclude-backupsLeave out backup and lock files. --exclude-caches, --exclude-caches-all, --exclude-caches-underLeave 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. --nullWith -T: names are separated by NUL bytes (works with find -print0). --no-nullTurn off --null again. --add-file=file Add this file, even if its name starts with a dash. --no-recursionDo not go into folders. --recursionGo into folders (the default). --one-file-systemStay 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, --dereferenceArchive the files that links point to, not the links. --hard-dereferenceFollow hard links and store the data each time. -P, --absolute-namesKeep the leading / in names (dangerous on extract). --anchored, --no-anchoredPatterns match at the start of the name / after any slash. --ignore-case, --no-ignore-caseIgnore case in patterns, or not. --wildcards, --no-wildcardsUse wildcards in patterns, or match exactly. --wildcards-match-slash, --no-wildcards-match-slashLet wildcards match a slash, or not. --strip-components=n On extract: remove the first N folders from each name. Extract behavior -k, --keep-old-filesDo not replace existing files (report an error). --skip-old-filesDo not replace existing files (skip silently). --keep-newer-filesDo not replace files that are newer than the archive copy. --overwriteOverwrite existing files. --overwrite-dirOverwrite the metadata of existing folders. --no-overwrite-dirKeep the metadata of existing folders. -U, --unlink-firstRemove each file before extracting over it. --recursive-unlinkRemove whole folders before extracting. --keep-directory-symlinkKeep existing links to folders. --one-top-level[=<dir>] Extract everything into one new folder. -O, --to-stdoutExtract files to the screen (stdout), not to disk. --to-command=command Send each extracted file to a command. -m, --touchDo not restore the modification time. --remove-filesRemove the files from disk after adding them to the archive. Owners, permissions, attributes -p, --preserve-permissions, --same-permissionsRestore the exact permissions (default for root). --no-same-permissionsApply your umask on extract. --same-ownerRestore the owners (default for root). --no-same-ownerExtract files as yourself (default for normal users). --numeric-ownerUse 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-mtimeWith --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-restoreSet folder times at the end (or not). --sort=order Order of folder entries: none, name, inode. -s, --preserve-order, --same-orderMember list is in the order of the archive. --acls, --no-aclsStore and restore POSIX ACLs (or not). --selinux, --no-selinuxStore and restore SELinux contexts (or not). --xattrs, --no-xattrsStore and restore extended attributes (or not). --xattrs-include=mask , --xattrs-exclude=mask Which extended attributes to include or skip. Format, devices and tapes --posixSame as --format=posix. -o, --old-archive, --portabilityOld V7 format (with -c). --pax-option=list Options for the pax format. -V, --label=text Give the archive a volume name. -S, --sparseStore 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, --incrementalOld GNU incremental format. --level=n Dump level for listed-incremental archives. --check-device, --no-check-deviceCheck device numbers for incremental backups (or not). --ignore-failed-readDo not stop when a file cannot be read. -W, --verifyVerify the archive after writing. -i, --ignore-zerosIgnore blocks of zeros in the archive (read concatenated archives). -B, --read-full-recordsReblock as you read (for pipes). -b, --blocking-factor=blocks Record size in 512-byte blocks. --record-size=bytes Record size in bytes. -M, --multi-volumeCreate, 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-seekThe archive can (or cannot) be seeked. --force-localThe 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-unquoteUnquote names in input (or not). --verbatim-files-from, --no-verbatim-files-fromRead -T names literally (or as options). --ignore-command-error, --no-ignore-command-errorIgnore exit codes of child programs (or not). --restrictDisable some options that could be harmful. --usageShow a short usage message. --versionShow the version. Niets past bij dit filter.
$ als gewone gebruiker# als root (of met sudo)
1 Back up a folder into a gzip archive Kopieer # tar -czvf /root/etc-$( date +%F).tar.gz /etcc = create, z = gzip, v = show files, f = archive name.
2 List what is in an archive Kopieer $ tar -tvf /root/etc-2026-10-25.tar.gzLook before you extract.
3 Extract into another folder Kopieer # tar -xzvf /root/etc-2026-10-25.tar.gz -C /tmp/restoreThe folder must exist. The real /etc is not touched.
4 Extract one file only Kopieer # tar -xzf backup.tar.gz -C /tmp etc/ssh/sshd_configGive the name as it is stored (without the leading slash).
5 Create a bzip2 or xz archive Kopieer $ tar -cjf docs.tar.bz2 docs/
$ tar -cJf docs.tar.xz docs/xz makes the smallest file but takes more time.
6 Leave out folders you do not need Kopieer $ tar -czf app.tar.gz --exclude='*.log' --exclude='node_modules' --exclude-vcs app/Smaller backups and no secrets from .git folders.
7 Keep SELinux labels and ACLs in a system backup Kopieer # tar --selinux --acls --xattrs -czpf /backup/www.tar.gz /var/wwwWithout these options the labels are lost, and the restored site can give 403 errors.
8 Remove the top folder when you extract Kopieer $ tar -xzf terraform-1.9.tar.gz --strip-components=1 -C /opt/terraformArchives from the web often have one extra top folder.
9 Copy a folder to another server through SSH Kopieer $ tar -czf - /srv/data | ssh server2 "tar -xzf - -C /backup"No temporary file: - sends the archive through the pipe.
10 Back up only files changed in the last day Kopieer # tar -czf /backup/changed.tar.gz -N "1 day ago" /homeA simple differential backup.
11 Make incremental backups with a snapshot file Kopieer # tar -czf /backup/home-full.tar.gz -g /backup/home.snar /home
# tar -czf /backup/home-inc1.tar.gz -g /backup/home.snar /homeThe second run stores only what changed since the first.
12 Check a backup against the disk Kopieer # tar -dzf /root/etc-2026-10-25.tar.gz -C /Lists files that changed since the backup (useful in incident response).
13 Read one file from an archive without extracting Kopieer $ tar -xzOf backup.tar.gz etc/hosts | less -O sends the file content to the screen.
Niets past bij dit filter.
Let op -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. Bestanden
/etc/rmtThe remote tape helper (rarely used). Tips om te onthouden 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.