tar 用於打包 備份 管理磁帶 磁盤文檔。
tar 能將許多文件一起保存到磁帶或磁盤存檔中,且還可從存檔中還原單個文件。
tar 版本控製可設置 --backup 選項或 VERSION_CONTROL 環境變量。
tar 文件備份後綴默認為 ~ ,除非設置 --suffix 選項或 SIMPLE_BACKUP_SUFFIX 環境變量。
tar 主要負責打包,壓縮需設置壓縮選項 (或先打包成 .tar 文件,再調用其它壓縮程序進行壓縮)。
tar -xf archive.tar
基本語法
tar [OPTION...] [FILE]...
若 FILE 沒有或為 -,則操控標準輸入。
注意: 長選項的 "強製性|可選" 參數, 對於短選項也是 "強製性|可選" 的。
默認值 --format=gnu -f- -b20 --quoting-style=escape --rmt-command=/usr/lib/tar/rmt --rsh-command=/usr/bin/rsh
主操作模式
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| -A, --catenate, --concatenate | append tar files to an archive | 把 tar 文件追加到存檔 | |
| -c, --create | create a new archive | 創建新存檔 | |
| -d, --diff, --compare | find differences between archive and file system | 找齣存檔和文件係統之間的差異 | |
| --delete | delete from the archive (not on mag tapes!) | 從存檔中刪除 (不在 mag 磁帶上) | |
| -r, --append | append files to the end of an archive | 把文件追加到存檔末尾 | |
| -t, --list | list the contents of an archive | 列錶存檔內容 | |
| --test-label | test the archive volume label and exit | 測試存檔捲標簽並退齣 | |
| -u, --update | only append files newer than copy in archive | 僅追加比存檔中副本更新的文件 | |
| -x, --extract, --get | extract files from an archive | 從存檔提取文件 |
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| --check-device | check device numbers when creating incremental archives (default) | 當創建增量存檔時,檢查設備編號 (默認) | |
| -g, --listed-incremental=FILE | handle new GNU-format incremental backup | 處理新 GNU 格式增量備份 | |
| -G, --incremental | handle old GNU-format incremental backup | 處理舊 GNU 格式增量備份 | |
| --ignore-failed-read | do not exit with nonzero on unreadable files | 在不可讀文件中不以非零退齣 | |
| --level=NUMBER | dump level for created listed-incremental archive | 創建增量列錶存檔的轉儲級彆 | |
| -n, --seek | archive is seekable | 存檔可尋址 | |
| --no-check-device | do not check device numbers when creating incremental archives | 當創建增量存檔時,不檢查設備編號 | |
| --no-seek | archive is not seekable | 存檔不可尋址 | |
| --occurrence[=NUMBER] |
process only the NUMBERth occurrence of each file in the archive; this option is valid only in conjunction with one of the subcommands --delete, --diff, --extract or --list, and when a list of files is given either on the command line or via the -T option; NUMBER defaults to 1 |
僅處理存檔每文件的第 NUMBERth 個齣現; 僅與子命令 --delete --diff --extract 或 --list 聯閤使用, 且在命令行上或通過 -T 選項給齣文件列錶時,此選項纔有效。 NUMBER 默認為 1 |
|
| --sparse-version=MAJOR[.MINOR] | set version of the sparse format to use (implies --sparse) | 設置要使用稀疏格式的版本 (暗含 --sparse) | |
| -S, --sparse | handle sparse files efficiently | 有效處理稀疏文件 |
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| -k, --keep-old-files | don't replace existing files when extracting, treat them as errors | 提取時不替換現有文件,將它們視為錯誤 | |
| --keep-directory-symlink | preserve existing symlinks to directories when extracting | 提取時,保留目錄的現有符號鏈接 | |
| --keep-newer-files | don't replace existing files that are newer than their archive copies | 不要替換比其存檔副本更新的現有文件 | |
| --no-overwrite-dir | preserve metadata of existing directories | 保留現有目錄元數據 | |
| --one-top-level[=DIR] | create a subdirectory to avoid having loose files extracted | 創建子目錄,以避免提取鬆散文件 | |
| --overwrite | overwrite existing files when extracting | 提取時,覆寫現有文件 | |
| --overwrite-dir | overwrite metadata of existing directories when extracting (default) | 提取時,覆寫現有目錄元數據 (默認) | |
| --recursive-unlink | empty hierarchies prior to extracting directory | 在提取目錄前,清空層次結構 | |
| --remove-files | remove files after adding them to the archive | 添加到存檔後,移除文件 | |
| --skip-old-files | don't replace existing files when extracting, silently skip over them | 提取時不替換現有文件,靜默跳過它們 | |
| -U, --unlink-first | remove each file prior to extracting over it | 在提取每文件前將其刪除 | |
| -W, --verify | attempt to verify the archive after writing it | 寫入後,嘗試驗證存檔 |
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| --ignore-command-error | ignore exit codes of children | 忽略子級退齣代碼 | |
| --no-ignore-command-error | treat non-zero exit codes of children as error | 將子級非零退齣代碼,視為錯誤 | |
| -O, --to-stdout | extract files to standard output | 將文件提取到標準輸齣 | |
| --to-command=COMMAND | pipe extracted files to another program | 管道提取文件到另一程序 |
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| --atime-preserve[=METHOD] |
preserve access times on dumped files, either by restoring the times after reading (METHOD='replace'; default) or by not setting the times in the first place (METHOD='system') |
保留轉儲文件的訪問時間, 通過還原讀取後的時間 (METHOD='replace'; 默認) 或通過不首先設置時間 (METHOD='system') |
|
| --clamp-mtime | only set time when the file is more recent than what was given with --mtime | 僅當文件比 --mtime 給定更新時纔設置時間 | |
| --delay-directory-restore | delay setting modification times and permissions of extracted directories until the end of extraction | 延遲設置修改時間和提取目錄權限,直到提取結束 | |
| --group=NAME | force NAME as group for added files | 強製 NAME 作為添加文件組 | |
| --mode=CHANGES | force (symbolic) mode CHANGES for added files | 把已添加文件 (符號) 模式強製為 CHANGES | |
| --mtime=DATE-OR-FILE | set mtime for added files from DATE-OR-FILE | 從 DATE-OR-FILE 設置已添加文件 mtime | |
| -m, --touch | don't extract file modified time | 不提取文件修改時間 | |
| --no-delay-directory-restore | cancel the effect of --delay-directory-restore option | 取消 --delay-directory-restore 選項影響 | |
| --no-same-owner | extract files as yourself (default for ordinary users) | 自己提取文件 (默認普通用戶) | |
| --no-same-permissions | apply the user's umask when extracting permissions from the archive (default for ordinary users) | 從存檔提取權限時,應用用戶的 umask (默認普通用戶) | |
| --numeric-owner | always use numbers for user/group names | 始終使用數字作為 user/group 名 | |
| --owner=NAME | force NAME as owner for added files | 把 NAME 強製作為添加文件所有者 | |
| -p, --preserve-permissions, --same-permissions | extract information about file permissions (default for superuser) | 提取有關文件權限信息 (默認超級用戶) | |
| --preserve | same as both -p and -s | 與 -p 和 -s 兩者相同 | |
| --same-owner | try extracting files with the same ownership as exists in the archive (default for superuser) | 試著以存檔現有相同所有權,提取文件 (默認超級用戶) | |
| -s, --preserve-order, --same-order | member arguments are listed in the same order as the files in the archive | 成員自變量以與存檔文件相同順序列齣 | |
| --sort=ORDER | directory sorting order: none (default), name or inode | 目錄排序順序:無 (默認),名稱或索引節點 |
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| --acls | Enable the POSIX ACLs support | 啓用 POSIX ACL 支持 | |
| --no-acls | Disable the POSIX ACLs support | 禁用 POSIX ACL 支持 | |
| --no-selinux | Disable the SELinux context support | 禁用 SELinux 上下文支持 | |
| --no-xattrs | Disable extended attributes support | 禁用擴展屬性支持 | |
| --selinux | Enable the SELinux context support | 啓用 SELinux 上下文支持 | |
| --xattrs | Enable extended attributes support | 啓用擴展屬性支持 | |
| --xattrs-exclude=MASK | specify the exclude pattern for xattr keys | 指定 xattr 鍵排除模式 | |
| --xattrs-include=MASK | specify the include pattern for xattr keys | 指定 xattr 鍵包括模式 |
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| -f, --file=ARCHIVE | use archive file or device ARCHIVE | 使用存檔文件或設備 ARCHIVE | |
| --force-local | archive file is local even if it has a colon | 存檔文件是本地的,即使它帶有冒號 | |
| -F, --info-script=NAME, --new-volume-script=NAME | run script at end of each tape (implies -M) | 在每磁帶末尾運行腳本 (暗含 -M) | |
| -L, --tape-length=NUMBER | change tape after writing NUMBER x 1024 bytes | 在寫入 NUMBER x 1024 字節後,更改磁帶 | |
| -M, --multi-volume | create/list/extract multi-volume archive | 創建/列齣/提取 多捲存檔 | |
| --rmt-command=COMMAND | use given rmt COMMAND instead of rmt | 使用給定 rmt COMMAND 而不是 rmt | |
| --rsh-command=COMMAND | use remote COMMAND instead of rsh | 使用遠程 COMMAND 而不是 rsh | |
| --volno-file=FILE | use/update the volume number in FILE | 使用/更新 FILE 捲號 |
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| -b, --blocking-factor=BLOCKS | BLOCKS x 512 bytes per record | BLOCKS x 512 字節每記錄 | |
| -B, --read-full-records | reblock as we read (for 4.2BSD pipes) | 讀取時重新分塊 (4.2BSD 管道) | |
| -i, --ignore-zeros | ignore zeroed blocks in archive (means EOF) | 忽略存檔零塊 (意味著 EOF) | |
| --record-size=NUMBER | NUMBER of bytes per record, multiple of 512 | 每記錄字節數,512 的倍數 |
| 參數 | EN 解釋 | 中文翻譯 | 備注 | |
|---|---|---|---|---|
| -H, --format=FORMAT | create archive of the given format | 創建給定格式存檔 | ||
| gnu | GNU tar 1.13.x format | GNU tar 1.13.x 格式 | ||
| oldgnu | GNU format as per tar <= 1.12 | 符閤 tar <= 1.12 的 GNU 格式 | ||
| pax | POSIX 1003.1-2001 (pax) format | POSIX 1003.1-2001 (pax) 格式 | ||
| posix | same as pax | 同 pax | ||
| ustar | POSIX 1003.1-1988 (ustar) format | POSIX 1003.1-1988 (ustar) 格式 | ||
| v7 | old V7 tar format | 舊 V7 tar 格式 | ||
| --old-archive, --portability | same as --format=v7 | 同 --format=v7 | ||
| --pax-option=keyword[[:]=value][,keyword[[:]=value]]... | control pax keywords | 控製p ax 關鍵字 | ||
| --posix | same as --format=posix | 同 --format=posix | ||
| -V, --label=TEXT |
create archive with volume name TEXT; at list/extract time, use TEXT as a globbing pattern for volume name |
以捲名 TEXT 創建存檔; 在列錶/提取時間,使用 TEXT 作為捲名通配模式 |
||
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| -a, --auto-compress | use archive suffix to determine the compression program | 使用存檔後綴確定壓縮程序 | |
| -I, --use-compress-program=PROG | filter through PROG (must accept -d) | 通過 PROG 過濾 (必須接受 -d) | |
| -j, --bzip2 | filter the archive through bzip2 | 通過 bzip2 過濾存檔 | |
| -J, --xz | filter the archive through xz | 通過 xz 過濾存檔 | |
| --lzip | filter the archive through lzip | 通過 lzip 過濾存檔 | |
| --lzma | filter the archive through xz | 通過 xz 過濾存檔 | |
| --lzop | filter the archive through xz | 通過 xz 過濾存檔 | |
| --no-auto-compress | do not use archive suffix to determine the compression program | 不使用存檔後綴確定壓縮程序 | |
| -z, --gzip, --gunzip, --ungzip | filter the archive through gzip | 通過 gzip 過濾存檔 | |
| -Z, --compress, --uncompress | filter the archive through compress | 通過 compress 過濾存檔 |
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| --add-file=FILE | add given FILE to the archive (useful if its name starts with a dash) | 把給定 FILE 添加到存檔 (若其名稱以 - 短劃綫開頭,則會很有用) | |
| --backup[=CONTROL] | backup before removal, choose version CONTROL | 在移除前備份,選擇版本 CONTROL | |
| -C, --directory=DIR | change to directory DIR | 轉到目錄 DIR | |
| --exclude=PATTERN | exclude files, given as a PATTERN | 排除 PATTERN 給定文件 | |
| --exclude-backups | exclude backup and lock files | 排除備份並鎖定文件 | |
| --exclude-caches | exclude contents of directories containing CACHEDIR.TAG, except for the tag file itself | 排除包含 CACHEDIR.TAG 的目錄內容,tag 文件本身除外 | |
| --exclude-caches-all | exclude directories containing CACHEDIR.TAG | 排除包含 CACHEDIR.TAG 的目錄 | |
| --exclude-caches-under | exclude everything under directories containing CACHEDIR.TAG | 排除包含 CACHEDIR.TAG 的目錄下的一切 | |
| --exclude-ignore=FILE | read exclude patterns for each directory from FILE, if it exists | 從 FILE 中讀取每目錄排除模式,若其存在的話 | |
| --exclude-ignore-recursive=FILE | read exclude patterns for each directory and its subdirectories from FILE, if it exists | 從 FILE 中讀取每目錄及其子目錄排除模式,若其存在的話 | |
| --exclude-tag=FILE | exclude contents of directories containing FILE, except for FILE itself | 排除包含 FILE 的目錄內容,FILE 本身除外 | |
| --exclude-tag-all=FILE | exclude everything under directories containing FILE | 排除包含 FILE 目錄下的一切 | |
| --exclude-vcs | exclude version control system directories | 排除版本控製係統目錄 | |
| --exclude-vcs-ignores | read exclude patterns from the VCS ignore files | 從 VCS 忽略文件中,讀取排除模式 | |
| -h, --dereference | follow symlinks; archive and dump the files they point to | 遵循符號鏈接;存檔並轉儲它們指嚮的文件 | |
| --hard-dereference | follow hard links; archive and dump the files they refer to | 遵循硬鏈接;存檔並轉儲它們引用的文件 | |
| -K, --starting-file=MEMBER-NAME | begin at member MEMBER-NAME when reading the archive | 讀取存檔時,從成員 MEMBER-NAME 開始 | |
| --newer-mtime=DATE | compare date and time when data changed only | 比較僅更改數據時的日期 時間 | |
| --no-null | disable the effect of the previous --null option | 禁用先前 --null 選項影響 | |
| --no-recursion | avoid descending automatically in directories | 避免在目錄中自動下降 | |
| --no-unquote | do not unquote input file or member names | 不取消輸入文件或成員名稱的引號 | |
| --null | -T reads null-terminated names, disable -C | -T 讀取以空值結尾的名稱,禁用 -C | |
| -N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE | only store files newer than DATE-OR-FILE | 僅存儲比 DATE-OR-FILE 更新的文件 | |
| --one-file-system | stay in local file system when creating archive | 創建存檔時,留在本地文件係統 | |
| -P, --absolute-names | don't strip leading '/'s from file names | 不從文件名剝離前導 '/' 字符 | |
| --recursion | recurse into directories (default) | 遞歸進目錄 (默認) | |
| --suffix=STRING |
backup before removal, override usual suffix ('~' unless overridden by environment variable SIMPLE_BACKUP_SUFFIX) |
移除前備份,覆蓋常用後綴 ('~',除非被環境變量 SIMPLE_BACKUP_SUFFIX 覆蓋) |
|
| -T, --files-from=FILE | get names to extract or create from FILE | 從 FILE 獲取提取或創建名稱 | |
| --unquote | unquote input file or member names (default) | 取消輸入文件或成員名引號 (默認) | |
| -X, --exclude-from=FILE | exclude patterns listed in FILE | 排除 FILE 中列齣的模式 |
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| --strip-components=NUMBER | strip NUMBER leading components from file names on extraction | 提取時,從文件名中剝離 NUMBER 個先導組件 | |
| --transform=EXPRESSION, --xform=EXPRESSION | use sed replace EXPRESSION to transform file names | 使用 sed 替換 EXPRESSION 變換文件名 |
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| --anchored | patterns match file name start | 模式匹配文件名起始 | |
| --ignore-case | ignore case | 忽略大小寫 | |
| --no-anchored | patterns match after any '/' (default for exclusion) | 模式在任何 '/' 後匹配 (默認獨占) | |
| --no-ignore-case | case sensitive matching (default) | 區分大小寫匹配 (默認) | |
| --no-wildcards | verbatim string matching | 逐字字符串匹配 | |
| --no-wildcards-match-slash | wildcards do not match '/' | 通配符不匹配 '/' 字符 | |
| --wildcards | use wildcards (default for exclusion) | 使用通配符 (默認獨占) | |
| --wildcards-match-slash | wildcards match '/' (default for exclusion) | 通配符匹配 '/' 字符 (默認獨占) |
| 參數 | EN 解釋 | 中文翻譯 | 備注 | |
|---|---|---|---|---|
| --checkpoint[=NUMBER] | display progress messages every NUMBERth record (default 10) | 每隔 NUMBER 條記錄顯示進度消息 (默認 10) | ||
| --checkpoint-action=ACTION | execute ACTION on each checkpoint | 在每檢查點執行 ACTION | ||
| --full-time | print file time to its full resolution | 以完整分辨率打印文件時間 | ||
| --index-file=FILE | send verbose output to FILE | 發送詳細輸齣到 FILE | ||
| -l, --check-links | print a message if not all links are dumped | 打印消息,若不是所有鏈接被轉儲 | ||
| --no-quote-chars=STRING | disable quoting for characters from STRING | 禁用 STRING 引號字符 | ||
| --quote-chars=STRING | additionally quote characters from STRING | 附加來自 STRING 的引號字符 | ||
| --quoting-style=STYLE | set name quoting style | 設置名稱引號樣式 | ||
| STYLE | literal | |||
| shell | ||||
| shell-always | ||||
| c | ||||
| c-maybe | ||||
| escape | ||||
| locale | ||||
| clocale | ||||
| -R, --block-number | show block number within archive with each message | 每消息展示存檔塊號 | ||
| --show-defaults | show tar defaults | 展示 tar 默認值 | ||
| --show-omitted-dirs | when listing or extracting, list each directory that does not match search criteria | 列錶或提取時, 列齣不匹配搜索條件的每一目錄 | ||
| --show-snapshot-field-ranges | show valid ranges for snapshot-file fields | 展示快照文件字段的有效範圍 | ||
| --show-transformed-names, --show-stored-names | show file or archive names after transformation | 變換後,展示文件或檔案名 | ||
| --totals[=SIGNAL] |
print total bytes after processing the archive; with an argument - print total bytes when this SIGNAL is delivered; Allowed signals are: SIGHUP, SIGQUIT, SIGINT, SIGUSR1 and SIGUSR2; the names without SIG prefix are also accepted |
處理存檔後,打印總字節數; 帶自變量 - 打印總字節,當交付相應 SIGNAL 時; 允許信號,包括:SIGHUP SIGQUIT SIGINT SIGUSR1 SIGUSR2; 還接受不帶 SIG 前綴的名稱 |
||
| --utc | print file modification times in UTC | 打印文件 UTC 修改時間 | ||
| -v, --verbose | verbosely list files processed | 詳細列齣已處理文件 | ||
| --warning=KEYWORD | warning control | 警告控製 | ||
| -w, --interactive, --confirmation | ask for confirmation for every action | 要求確認每一動作 | ||
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| -o |
when creating, same as --old-archive; when extracting, same as --no-same-owner |
創建時,同 --old-archive; 提取時,同 --no-same-owner |
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| -?, --help | give this help list | 給齣相應幫助信息 | |
| --restrict | disable use of some potentially harmful options | 禁用某些潛在有害選項 | |
| --usage | give a short usage message | 給齣簡短用法消息 | |
| --version | print program version | 打印程序版本 |
| 參數 | EN 解釋 | 中文翻譯 | 備注 |
|---|---|---|---|
| none, off | never make backups | 從不生成備份 | |
| t, numbered | make numbered backups | 生成編號備份 | |
| nil, existing | numbered if numbered backups exist, simple otherwise | 若存在編號備份,則編號;否則,simple | |
| never, simple | always make simple backups | 始終生成簡單備份 |
| 功能 | 命令 | 文字解釋 | 示例 | 示例解釋 |
|---|---|---|---|---|
| 提取 | tar -xf archive.tar | 從 archive.tar 提取所有文件 | ||
| 壓縮 | tar -cf archive.tar foo bar | 從文件 foo bar 創建 archive.tar 壓縮包 | ||
| 列錶 | tar -tvf archive.tar | 詳細列齣 archive.tar 中的所有文件 | ||
| 版本 | tar --version | 打印版本信息 | ||
| 幫助 | tar -? | 展示幫助信息 | ||
| tar --help |
版權聲明: 本文為獨傢原創稿件,版權歸 樂數軟件 ,未經許可不得轉載。