1.命令作用
命令tar將許多文件一起保存到單個磁帶或磁盤存檔中,并且可以從存檔中恢復單個文件(GNU 'tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive.)。
2.命令語法
Usage: tar [OPTION...] [FILE]...
2.簡單舉例
?tar -cf archive.tar foo bar,將文件foo和bar,創建一個tar格式的歸檔(archive.tar)。
?tar -tvf archive.tar,詳細列出archive.tar中的所有文件。
?tar -xf archive.tar,從archive.tar中提取所有文件。
3.參數詳解
OPTION:
①主要運行方式:
- -A, --catenate, --concatenate,將tar文件附加到另一個tar文件(歸檔文件)中,需與-f 參數一起使用;僅可一個一個tar文件附加,不可多個tar文件一次性附加;
- -c, --create,創建一個新的歸檔文件,需與-f 參數一起使用;
- -d, --diff, --compare,查找歸檔中文件和系統上解壓后的文件之間的差異,系統上解壓后的文件多了不會輸出,如果解壓后的文件少了則會輸出提示,需與-f、-v 參數一起使用;
- --delete,從存檔中刪除文件或者目錄,刪除的文件和目錄可以使用tar -tf 查看;
- -r, --append,將文件追加到存檔文件末尾,需與-f 參數一起使用;
- -t, --list,列出存檔的內容列表,需與-f 參數一起使用;
- --test-label,測試歸檔卷標簽并退出,用于自動化腳本驗證歸檔的存在和卷標是否符合要求;
- -u, --update,只追加比歸檔文件中比列表文件更新的文件,需與-f、-v 參數一起使用;使用-tf 查看更新后的歸檔文件列表,是追加方式,即列表中存在多個不同版本但相同名稱的文件;注意解壓后僅只保留版本最新的文件;
- -x, --extract, --get,從存檔中提取全部文件,或者指定文件名稱進行提取,通常與-f、-v 參數一起使用
②操作修飾符:
- --check-device,在創建或增量存檔時,檢查設備(默認)
- -g, --listed-incremental=FILE,處理新GNU格式的增量歸檔,需要保留所有的歸檔,增量歸檔解壓需要指定快照文件,全量歸檔加多個增量歸檔可以進行備份;
- -G, --incremental,處理舊GNU格式增量備份,與-g用法類似
- --ignore-failed-read,解壓歸檔時,當歸檔中有不可讀的文件(歸檔存在損壞情況),不會因為非零退出,而是繼續提取歸檔中獲取文件,對損壞的歸檔解壓場景很有用;默認情況下遇到錯誤直接退出;簡而言之即忽略錯誤繼續解壓,默認遇到錯誤直接退出;
- --level=NUMBER,指定全量歸檔的增量歸檔文件的轉儲級別,全量為--level=0,增量依次為1、2、3、...,通常與-g參數一起使用;
- -n, --seek,歸檔可查找,tar -cn ...tar顯示tar文件中所有文件內容;
- --no-check-device,在創建或增量歸檔時,不檢查設備;
- --no-seek,歸檔不可查;
- --occurrence[=NUMBER],只處理存檔中每個文件的第number次出現;此選項僅在與子命令---delete、--diff、--extract或--list一起使用時有效,并且當在命令行上或通過-T選項給出文件列表時有效;NUMBER默認為1
- --sparse-version=MAJOR[.MINOR],設置要使用的稀疏格式的版本(暗含--sparse)
- -S, --sparse,有效地處理稀疏文件
③輸入控制覆蓋寄存器:
- -k, --keep-old-files,提取時不提取系統中已存在的現有文件,將其視為錯誤
- --keep-directory-symlink,提取時保留到目錄的現有符號鏈接,tar默認會保留符號鏈接,與--dereference參數相反
- --keep-newer-files,提取時不提取系統中現有文件,比歸檔中文件版本新,或者與歸檔中文件版本一致的文件;即如果歸檔中有新版本的文件會被提取覆蓋系統中的現有文件;
- --no-overwrite-dir,提取歸檔時,保留已存在目錄的元數據(比如用戶和讀寫權限);只有系統上存在多層目錄時才有效,且對最內層目錄無效;可使用stat?dir查看,其中Access、Modify、Change時間會有所變化;
- --overwrite,提取歸檔時,覆蓋已存在的文件;
- --overwrite-dir,提取歸檔時,覆蓋已存在目錄的元數據(默認),與--no-overwrite-dir效果相反;
- --recursive-unlink,在提取目錄之前去除目錄遞歸(刪除目錄文件夾),無明顯效果,與默認差不多;
- --remove-files,將文件添加到存檔后刪除文件;同樣適用于目錄歸檔
- --skip-old-files,提取時不要替換現有文件,靜默地跳過它們;與-k類似;
- -U, --unlink-first,在對每個文件進行提取之前先刪除它;
- -W, --verify,在寫入歸檔文件后嘗試驗證它;
④選擇輸出流:
- --ignore-command-error,忽略命令錯誤而導致執行退出;主要處理外部壓縮工具gzip、bzip2時的錯誤怎么處理,啟動參數會跳過錯誤繼續執行,未啟動參數報錯后立即終止壓縮/解壓;
- --no-ignore-command-error,將非零退出視為錯誤,與--ignore-command-error相反;
- -O, --to-stdout,將文件提取到標準輸出
- --to-command=COMMAND,管道提取文件到另一個程序
⑤文件屬性的處理:
- --atime-preserve[=METHOD],保留對轉儲文件的訪問時間,或者通過恢復讀取后的時間(METHOD='replace',默認)或不首先設置時間(METHOD='system');測試無效果;
- --delay-directory-restore,將解壓目錄的修改次數和權限設置延遲到解壓結束,無效果;
- --group=NAME,創建歸檔時,強制NAME作為添加文件的組權限名稱;
- --mode=CHANGES,強制(符號)模式,對新增文件的更改;無法使用,沒說明模式可設置哪些參數;
- --mtime=DATE-OR-FILE,創建歸檔時,設置歸檔中文件的mtime為DATE-OR-FILE;
- -m, --touch,不提取文件修改時間;
- --no-delay-directory-restore,取消--delay-directory-restore選項的效果,無效果;
- --no-same-owner,按照自己的方式提取文件,即強制文件歸屬當前用戶(默認為普通用戶);
- --no-same-permissions,在從歸檔文件中提取權限時應用用戶的umask(默認為普通用戶);
- --numeric-owner,始終使用數字作為用戶/組名;
- --owner=NAME,創建歸檔時,強制NAME作為添加文件的所有者;
- -p, --preserve-permissions, --same-permissions,提取有關文件權限的信息(默認為超級用戶),測試無效果;
- --preserve,和-p和-s一樣,提示已經不再使用;
- --same-owner,嘗試提取與存檔中存在的所有權相同的文件(默認為超級用戶)
- -s, --preserve-order, --same-order,成員參數的列出順序與存檔中的文件相同,測試無效果;
⑥擴展文件屬性的處理(通常不會使用到):
- --acls,啟用POSIX acl支持
- --no-acls,關閉POSIX acl支持
- --no-selinux,禁用SELinux上下文支持
- --no-xattrs,禁用擴展屬性支持
- --selinux,啟用SELinux上下文支持
- --xattrs,啟用擴展屬性支持
- --xattrs-exclude=MASK,指定xattr鍵的排除模式
- --xattrs-include=MASK,為xattr鍵指定包含模式
⑦設備選擇和切換:
- -f, --file=ARCHIVE,使用歸檔文件或歸檔設備
- --force-local,歸檔文件是本地的,即使它有冒號;通常冒號是遠程文件的書寫格式,使用該參數,強制將帶有冒號的文件名稱視為本地文件;
- -F, --info-script=NAME, --new-volume-script=NAME,在每個磁帶的末尾運行腳本(暗含-M)
- -L, --tape-length=NUMBER,寫入NUMBER * 1024字節后更改磁帶
- -M, --multi-volume,創建/列表/提取多卷存檔
- --rmt-command=COMMAND,使用給定的rmt命令而不是rmt
- --rsh-command=COMMAND,使用遠程命令代替rsh
- --volno-file=FILE,使用/update文件中的卷號
⑧裝置塊Device blocking:
- -b, --blocking-factor=BLOCKS,BLOCKS x每條記錄512字節
- -B, --read-full-records,讀取時重新阻塞(4.2BSD管道)
- -i, --ignore-zeros,忽略歸檔中的歸零塊(意味著EOF)
- --record-size=NUMBER,每條記錄的字節數,512的倍數
⑨檔案格式選擇:
- -H, --format=FORMAT,創建給定格式的存檔;FORMAT是下列選項之一:
- gnu? ? ? ? GNU tar 1.13.x format(默認格式,支持長文件名、大文件及擴展元數據)
- oldgnu? ?GNU format as per tar <= 1.12
- pax? ? ? ? POSIX 1003.1-2001 (pax) format
- posix? ? ? same as pax(跨平臺兼容性好,但功能限制多)
- ustar? ? ? POSIX 1003.1-1988 (ustar) format
- v7? ? ? ? ? old V7 tar format
- --old-archive, --portability,與--format=v7相同
- --pax-option=keyword[[:]=value][,keyword[[:]=value]]...,控制pax的關鍵字,需要與--format=pax一起使用,keyword=value可設置為"charset=UTF-8"、"mtime:=%s.%N"、"delete=uid"、"delete=gid"
- --posix,與--format=posix相同
- -V, --label=TEXT,創建歸檔時定義一個標簽為TEXT;在查看歸檔內容(-t)和提取歸檔時顯示標簽TEXT內容;
⑩壓縮選項:
- -a, --auto-compress,使用歸檔后綴來確定壓縮程序
- -I, --use-compress-program=PROG,通過PROG壓縮方式歸檔(必須接受-d),簡言之通過-l指定壓縮方式,PROG可取值bzip2、xz、lzip;
- -j, --bzip2,通過bzip2壓縮歸檔文件,歸檔命名后綴為.tar.bz2
- -J, --xz,通過xz壓縮歸檔文件,歸檔命名后綴為.tar.xz
- --lzip,通過lzip壓縮歸檔文件,歸檔命名后綴為.tar.lz;測試報錯
- --lzma,通過lzma壓縮歸檔文件,歸檔命名后綴為.tar.lzma;測試報錯
- --lzop,通過lzma壓縮歸檔文件,歸檔命名后綴為.tar.lzo
- --no-auto-compress,不使用歸檔后綴來確定壓縮程序,與-a參數相反
- -z, --gzip, --gunzip, --ungzip,通過gzip壓縮歸檔文件,歸檔命名后綴為.tar.gz
- -Z, --compress, --uncompress,通過compress壓縮歸檔文件,歸檔命名后綴為.tar.Z
?本地文件選擇:
- --add-file=FILE,將給定的文件添加到存檔(如果其名稱以破折號開頭,則很有用);測試發現不是追加,是替換效果
- --backup[=CONTROL],刪除前備份,選擇版本控制;提取壓縮歸檔時,系統已存在文件進行版本控制和備份,CONTROL可取值有none / off(不備份直接覆蓋,默認值)、numbered / t(創建帶數字的后綴備份)、nil / existing(如之前有帶數字的后綴就繼續數字備份,否者使用簡單后綴)、never / simple(簡單后綴)
- -C, --directory=DIR,更改為目錄DIR,而不是默認的當前目錄
- --exclude=PATTERN,排除文件,以PATTERN形式給出(創建/提取歸檔都適用)
- --exclude-backups,排除備份和鎖定文件
- --exclude-caches,排除包含CACHEDIR的目錄內容。TAG,除了標記文件本身
- --exclude-caches-all,排除包含CACHEDIR.TAR的目錄
- --exclude-caches-under,排除包含CACHEDIR.TAR目錄下的所有內容
- --exclude-tag=FILE,排除包含FILE的目錄的內容,除了FILE本身
- --exclude-tag-all=FILE,排除包含FILE的目錄
- --exclude-tag-under=FILE,排除包含FILE的目錄下的所有內容
- --exclude-vcs,排除版本控制系統目錄;歸檔時會自動跳過.git .svn .hg等版本控制目錄及其目錄下所有的文件和目錄
- -h, --dereference,創建歸檔時,將軟鏈接轉儲為它們指向的實際文件;即軟鏈接會變成實際的文件或者目錄;
- --hard-dereference,創建歸檔時,將硬鏈接轉儲為它們所引用的文件;即硬鏈接會變成實際的文件或者目錄;
- -K, --starting-file=MEMBER-NAME,提取歸檔時,從MEMBER-NAME開始提取;MEMBER-NAME名稱可以通過tar -tf查看,-tf查看時也可以使用該參數;
- --newer-mtime=DATE,僅比較數據更改時的日期和時間
- --no-null,-T參數使用時,指定文件命令列表的分隔方式,該方式為換行\n,為默認值
- --no-recursion,指定歸檔的目錄層級,指定的目錄層級之下的文件或者目錄不會被歸檔
- --no-unquote,不取消用-T讀取的文件名的引號
- --null,-T參數使用時,指定文件命令列表的分隔方式,與--no-null對立
- -N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE,歸檔ctime在DATE-OR-FILE指定時間及其之后的文件
- --one-file-system,在創建存檔時保留在本地文件系統中的中;歸檔備份"/"目錄不歸檔/proc、/mnt、/dev等掛載內容;
- -P, --absolute-names,不要從文件名中去掉前導"/"(更目錄)
- --recursion,遞歸到目錄(默認)
- --suffix=STRING,刪除前備份,覆蓋通常的后綴('~',除非被環境變量SIMPLE_BACKUP_SUFFIX覆蓋);指定后備份后綴還是'~'
- -T, --files-from=FILE,從FILE文件內容指定的文件名稱列表提取/創建歸檔
- --unquote,用-T讀取的文件名取消引號(默認)
- -X, --exclude-from=FILE,排除FILE文件中列出的文件或者目錄,與-T相反
?文件名轉換:
- --strip-components=NUMBER,提取時從文件名中剝離前NUMBER層目錄
- --transform=EXPRESSION, --xform=EXPRESSION,使用sed替換表達式來轉換文件名
?文件名匹配選項(影響排除和包含模式):
- --anchored,模式匹配文件名開頭
- --ignore-case,忽略大小寫匹配,比如使用--exclude時使用
- --no-anchored,模式匹配任何 '/' (默認為排除)
- --no-ignore-case,區分大小寫匹配(默認)
- --no-wildcards,逐字字符串匹配
- --no-wildcards-match-slash,通配符不匹配 '/'
- --wildcards,使用通配符(默認)
- --wildcards-match-slash,通配符匹配 '/' (默認排除)
?信息輸出:
- --checkpoint[=NUMBER],根據NUMBER值顯示創建/提取歸檔進度(默認為10)
- --checkpoint-action=ACTION,在每個檢查點上執行ACTION
- --full-time,創建歸檔/查看歸檔時,顯示文件目錄更詳細的時間信息
- --index-file=FILE,將標準輸出輸出到FILE
- -l, --check-links,如果不是所有鏈接都被轉儲,則打印一條消息
- --no-quote-chars=STRING,禁用字符串字符的引號
- --quote-chars=STRING,另外引用字符串中的字符
- --quoting-style=STYLE,設置名稱引用樣式;下面是有效的STYLE值
- ? literal
- ? shell
- ? shell-always
- ? c
- ? c-maybe
- ? escape
- ? locale
- ? clocale
- -R, --block-number,顯示每條消息存檔中的塊號,如:tar -Rtf file.tar
- --show-defaults,顯示tar默認值,如:tar --show-defaults
- --show-omitted-dirs,列出或提取出現錯誤時,列出不匹配搜索條件的每個目錄
- --show-transformed-names, --show-stored-names,-tf查看列表顯示轉換后的文件或存檔名稱,與--transform參數或者--strip-components參數一起使用
- --totals[=SIGNAL],打印歸檔處理后的總字節數;tar -cvf test.tar *.txt --totals
- --utc,以UTC格式打印文件修改時間,-cvf與-tf創建歸檔和查看歸檔列表時使用
- -v, --verbose,標準輸出處理過程中的相關信息
- --warning=KEYWORD,告警控制;可取值none(禁止所有告警)、all(啟動所有告警)、no-timestamp(忽略歸檔中的時間戳異常的告警)
- -w, --interactive, --confirmation,每歸檔一個目標都要輸入y確認
?其他選擇:
- -?, --help,給出以下幫助列表
- --restrict,禁用一些可能有害的選項
- --usage,給出簡短的用法信息
- --version,打印程序版本
?tar默值,可通過tar --show-defaults查看:
- --format=gnu -f- -b20 --quoting-style=escape --rmt-command=/etc/rmt --rsh-command=/usr/bin/ssh
4.常用用例
4.1.歸檔文件內容查看
## 查看歸檔文件(tar文件)內的文件列表
[root@node1 test]# tar -tf file1.txt.tar
file1.txt
[root@node1 test]#
4.2.主動運行方式
4.2.1.將一個歸檔文件附加到另一個歸檔文件中
[root@node1 test]# tar -tf file1.txt.tar
file1.txt
[root@node1 test]# tar -tf file2.txt.tar
file2.txt
[root@node1 test]# tar -Af file1.txt.tar file2.txt.tar
[root@node1 test]# tar -tf file1.txt.tar
file1.txt
file2.txt
[root@node1 test]#
4.2.2.對比歸檔文件與解壓后文件系統中文件的差異
[root@node1 test]# ll
total 643712
-rw-r--r--. 1 root root 439439360 May 13 22:50 file1.txt.tar
-rw-r--r--. 1 root root 219719680 May 13 22:49 file2.txt.tar
[root@node1 test]# tar -xvf file1.txt.tar
file1.txt
file2.txt
[root@node1 test]# tar -dvf file1.txt.tar ## 文件一致,僅輸出歸檔文件內容列表
file1.txt
file2.txt
[root@node1 test]# ll
total 1072840
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 439439360 May 13 22:50 file1.txt.tar
-rw-r--r--. 1 root root 219712350 Mar 2 03:05 file2.txt
-rw-r--r--. 1 root root 219719680 May 13 22:49 file2.txt.tar
[root@node1 test]# rm -rf file2.txt
[root@node1 test]# tar -dvf file1.txt.tar ## 文件不一致,輸出歸檔文件內容列表,且輸出不存在的文件提升
file1.txt
file2.txt
tar: file2.txt: Warning: Cannot stat: No such file or directory
[root@node1 test]#
4.2.3.刪除歸檔文件中的指定內容
[root@node1 test]# tar -tf file1.txt.tar
file1.txt
file2.txt
[root@node1 test]# tar --delete -f file1.txt.tar file1.txt
[root@node1 test]# tar -tf file1.txt.tar
file2.txt
[root@node1 test]#
4.2.4.將文件追加到存檔文件末尾
[root@node1 test]# ll
total 643708
-rw-r--r--. 1 root root 219712350 May 13 23:13 file1.txt
-rw-r--r--. 1 root root 219719680 May 13 23:07 file1.txt.tar
-rw-r--r--. 1 root root 219719680 May 13 22:49 file2.txt.tar
[root@node1 test]# tar -tf file1.txt.tar
file2.txt
[root@node1 test]# tar -rf file1.txt.tar file1.txt
[root@node1 test]# tar -tf file1.txt.tar
file2.txt
file1.txt
[root@node1 test]#
4.2.5.驗證歸檔文件是否存在
[root@node1 test]# ll
total 643704
-rw-r--r--. 1 root root 439429120 May 13 23:18 file1.txt.tar
-rw-r--r--. 1 root root 219719680 May 13 22:49 file2.txt.tar
[root@node1 test]# tar --test-label -f file2.txt.tar
[root@node1 test]# echo $?
0
[root@node1 test]# tar --test-label -f file3.txt.tar
tar: file3.txt.tar: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
[root@node1 test]# echo $?
2
[root@node1 test]#
4.2.6.更新歸檔文件里面更新過的新版本文件
[root@node1 Desktop]# tar -tf file.tar
file1.txt
file2.txt
file3.txt
file4.txt
[root@node1 Desktop]# vi file2.txt
[root@node1 Desktop]#
[root@node1 Desktop]# tar -uvf file.tar file1.txt file2.txt file3.txt file4.txt
file2.txt
[root@node1 Desktop]# tar -tf file.tar
file1.txt
file2.txt
file3.txt
file4.txt
file2.txt
[root@node1 Desktop]#
4.2.7.從歸檔中提取指定文件或全部文件
[root@node1 test]# tar -tf file.tar
file1.txt
file2.txt
file3.txt
file4.txt
file2.txt
[root@node1 test]# tar -xvf file.tar file3.txt
file3.txt
[root@node1 test]# ll
total 1287384
-rw-r--r--. 1 root root 219712350 May 13 22:21 file3.txt
-rw-r--r--. 1 root root 1098567680 May 13 23:48 file.tar
[root@node1 test]# tar -xvf file.tar
file1.txt
file2.txt
file3.txt
file4.txt
file2.txt
[root@node1 test]# ll
total 1931076
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 219712359 May 13 23:46 file2.txt
-rw-r--r--. 1 root root 219712350 May 13 22:21 file3.txt
-rw-r--r--. 1 root root 219712350 May 13 22:21 file4.txt
-rw-r--r--. 1 root root 1098567680 May 13 23:48 file.tar
[root@node1 test]#
4.3.?操作修飾符
4.3.1.是否檢查設備
[root@node1 test]# tar --check-device -uvf file.tar file1.txt # 更新替換追加
file1.txt
[root@node1 test]# vi file1.txt ## 更新文件
[root@node1 test]# tar --no-check-device -uvf file.tar file1.txt # 更新替換追加
file1.txt
[root@node1 test]#
4.3.2.全量備份與增量備份
[root@node1 Desktop]# tar -g snapshot.file -cvf file.full.txt.tar file[1-4].txt # 全量備份,生產快照文件snapshot.file
file1.txt
file2.txt
file3.txt
file4.txt
[root@node1 Desktop]# cat snapshot.file
GNU tar-1.26-2
1747152665332697702[root@node1 Desktop]# vi file3.txt # 更新文件
[root@node1 Desktop]# tar -g snapshot.file -cvf file.inc.txt.tar file[1-4].txt # 增量備份,更新快照文件snapshot.file
file3.txt
[root@node1 Desktop]# cat snapshot.file
GNU tar-1.26-2
1747152815368262961[root@node1 Desktop]#
4.3.3.全量增量備份級別指定
[root@node1 Desktop]# tar --level=0 -g snapshot.file -cvf file.full.txt.tar file[1-4].txt
file1.txt
file2.txt
file3.txt
file4.txt
[root@node1 Desktop]# cat snapshot.file
GNU tar-1.26-2
1747154213825223342[root@node1 Desktop]# vi file2.txt
[root@node1 Desktop]# tar --level=1 -g snapshot.file -cvf file.inc.txt.tar file[1-4].txt
file2.txt
[root@node1 Desktop]# cat snapshot.file
GNU tar-1.26-2
1747154260428040570[root@node1 Desktop]#
4.3.4.歸檔中多版本相同名稱文件,處理指定的文件
[root@node1 Desktop]# tar -tf file.tar
file5.txt
file6.txt
file5.txt
[root@node1 Desktop]# tar --delete -f file.tar --occurrence=2 file5.txt # 指定刪除第2個file5.txt
[root@node1 Desktop]# tar -tf file.tar
file5.txt
file6.txt
[root@node1 Desktop]#
----------------------------------------------------------
[root@node1 Desktop]# tar -tf file.tar
file5.txt
file6.txt
file5.txt
[root@node1 Desktop]# tar --delete -f file.tar --occurrence=1 file5.txt # 指定刪除第1個file5.txt
[root@node1 Desktop]# tar -tf file.tar
file6.txt
file5.txt
[root@node1 Desktop]#
4.3.5.稀疏文件的歸檔
[root@node1 Desktop]# qemu-img create -f raw file8.txt 2G # 創建一個稀疏文件
Formatting 'file8.txt', fmt=raw size=2147483648
[root@node1 Desktop]# ll file8.txt
-rw-r--r--. 1 root root 2147483648 May 14 01:49 file8.txt
[root@node1 Desktop]# ll file8.txt -h # 稀疏文件ls -h查看ll -h查看,大小顯示比較大
-rw-r--r--. 1 root root 2.0G May 14 01:49 file8.txt
[root@node1 Desktop]# du -sh file8.txt # 使用du -sh查看就很小
0 file8.txt
[root@node1 Desktop]#
[root@node1 Desktop]# tar -cvf file8.txt.tar file8.txt # 普通歸檔文件與ls -h的結果接近
file8.txt
[root@node1 Desktop]# tar -Scvf file8.txt.1.tar file8.txt # 稀疏歸檔與du -sh的結果接近
file8.txt
[root@node1 Desktop]# ll -h file8*
-rw-r--r--. 1 root root 2.0G May 14 01:49 file8.txt
-rw-r--r--. 1 root root 10K May 14 01:51 file8.txt.1.tar
-rw-r--r--. 1 root root 2.1G May 14 01:50 file8.txt.tar
[root@node1 Desktop]#
4.4.輸入控制覆蓋寄存器
4.4.1.提取歸檔時,是否替換系統已存在文件
[root@node1 test]# ll
total 1072832
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 28 May 14 01:24 file5.txt
-rw-r--r--. 1 root root 20 May 14 00:45 file6.txt
-rw-r--r--. 1 root root 878858240 May 14 02:00 file.tar
[root@node1 test]# tar -tf file.tar
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
[root@node1 test]# tar -xvf file.tar # 提取歸檔時,替換系統已存在文件
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
[root@node1 test]# ll
total 1716524
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 219712363 May 14 00:37 file2.txt
-rw-r--r--. 1 root root 219712372 May 14 00:13 file3.txt
-rw-r--r--. 1 root root 219712350 May 13 22:21 file4.txt
-rw-r--r--. 1 root root 28 May 14 01:24 file5.txt
-rw-r--r--. 1 root root 20 May 14 00:45 file6.txt
-rw-r--r--. 1 root root 878858240 May 14 02:00 file.tar
[root@node1 test]#
----------------------------------------------------------
[root@node1 test]# ll
total 1072832
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 28 May 14 01:24 file5.txt
-rw-r--r--. 1 root root 20 May 14 00:45 file6.txt
-rw-r--r--. 1 root root 878858240 May 14 02:00 file.tar
[root@node1 test]# tar -kxvf file.tar # 提取歸檔時,不替換系統已存在文件,視其為錯誤
file1.txt
tar: file1.txt: Cannot open: File exists
file2.txt
file3.txt
file4.txt
file5.txt
tar: file5.txt: Cannot open: File exists
file6.txt
tar: file6.txt: Cannot open: File exists
tar: Exiting with failure status due to previous errors
[root@node1 test]# ll
total 1716524
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 219712363 May 14 00:37 file2.txt
-rw-r--r--. 1 root root 219712372 May 14 00:13 file3.txt
-rw-r--r--. 1 root root 219712350 May 13 22:21 file4.txt
-rw-r--r--. 1 root root 28 May 14 01:24 file5.txt
-rw-r--r--. 1 root root 20 May 14 00:45 file6.txt
-rw-r--r--. 1 root root 878858240 May 14 02:00 file.tar
4.4.2.是否覆蓋新文件
[root@node1 test]# ll
total 858264
-rw-r--r--. 1 root root 219712350 May 13 22:21 file1.txt
-rw-r--r--. 1 root root 219712363 May 14 00:37 file2.txt
-rw-r--r--. 1 root root 219712372 May 14 00:13 file3.txt
-rw-r--r--. 1 root root 219712350 May 13 22:21 file4.txt
-rw-r--r--. 1 root root 24 May 14 02:28 file5.txt
-rw-r--r--. 1 root root 20 May 14 00:45 file6.txt
[root@node1 test]# tar -cvf file.tar file[1-6].txt
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
[root@node1 test]# vi file5.txt # 更新文件
[root@node1 test]# tar -xvf file.tar # 默認提取歸檔是覆蓋系統上已存在的文件
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
[root@node1 test]#
----------------------------------------------------------
[root@node1 test]# ll
total 1716524
drwxr-xr-x. 2 root root 124 May 14 04:43 dir1
-rw-r--r--. 1 root root 219712353 May 14 04:43 file1.txt
-rw-r--r--. 1 root root 219712363 May 14 04:43 file2.txt
-rw-r--r--. 1 root root 219712391 May 14 04:43 file3.txt
-rw-r--r--. 1 root root 219712350 May 14 04:43 file4.txt
-rw-r--r--. 1 root root 72 May 14 04:43 file5.txt
-rw-r--r--. 1 root root 20 May 14 04:43 file6.txt
-rw-r--r--. 1 root root 878858240 May 14 04:43 file.tar
[root@node1 test]# vi file5.txt # 編輯文件使得成為新的版本文件
[root@node1 test]# rm -rf file.tar
[root@node1 test]# tar -cvf file.tar file[1-6].txt # 重新歸檔打包
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
[root@node1 test]# cp file.tar dir1/ # 覆蓋之前的歸檔文件
cp: overwrite ‘dir1/file.tar’? yes
[root@node1 test]# cd dir1/
[root@node1 dir1]# ll # 查看之前歸檔提取的文件,下面添加--keep-newer-files參數提取
total 1716524
-rw-r--r--. 1 root root 219712353 May 14 04:43 file1.txt
-rw-r--r--. 1 root root 219712363 May 14 04:43 file2.txt
-rw-r--r--. 1 root root 219712391 May 14 04:43 file3.txt
-rw-r--r--. 1 root root 219712350 May 14 04:43 file4.txt
-rw-r--r--. 1 root root 72 May 14 04:43 file5.txt
-rw-r--r--. 1 root root 20 May 14 04:43 file6.txt
-rw-r--r--. 1 root root 878858240 May 14 04:43 file.tar
[root@node1 dir1]# tar --keep-newer-files -xvf file.tar # 歸檔中新版本文件會被提取并替換系統中對應的現有文件,歸檔中其他舊版本或者相同版本不會被提取
file1.txt
tar: Current `file1.txt' is newer or same age
file2.txt
tar: Current `file2.txt' is newer or same age
file3.txt
tar: Current `file3.txt' is newer or same age
file4.txt
tar: Current `file4.txt' is newer or same age
file5.txt
file6.txt
tar: Current `file6.txt' is newer or same age
[root@node1 dir1]#
4.4.3.保留目錄元數據作用域
[root@node1 dir2]# chmod -R 777 dir1
[root@node1 dir2]# chown -R qwer:qwer dir1
[root@node1 dir2]# ll
total 12
drwxrwxrwx. 4 qwer qwer 49 May 14 13:32 dir1
-rw-r--r--. 1 root root 10240 May 14 05:01 dir1.tar
[root@node1 dir2]# ll dir1
total 4
drwxrwxrwx. 3 qwer qwer 36 May 14 13:32 dir11
drwxrwxrwx. 3 qwer qwer 36 May 14 13:32 dir12
-rwxrwxrwx. 1 qwer qwer 17 May 14 04:49 file1.txt
[root@node1 dir2]# ll dir1/dir11
total 4
drwxrwxrwx. 2 qwer qwer 6 May 14 05:00 dir21 # 修改了用戶和讀寫執行權限
-rwxrwxrwx. 1 qwer qwer 15 May 14 04:48 file1.txt
[root@node1 dir2]#
[root@node1 dir2]# tar --no-overwrite-dir -xvf dir1.tar # 之前解壓過,修改權限后,使用--no-overwrite-dir參數再解壓
dir1/
dir1/dir11/
dir1/dir11/file1.txt
dir1/dir11/dir21/
dir1/dir12/
dir1/dir12/file1.txt
dir1/dir12/dir22/
dir1/file1.txt
[root@node1 dir2]# ll
total 12
drwxrwxrwx. 4 qwer qwer 49 May 14 13:33 dir1 # 已經存在的非最內層目錄保留了元數據
-rw-r--r--. 1 root root 10240 May 14 05:01 dir1.tar
[root@node1 dir2]# ll dir1
total 4
drwxrwxrwx. 3 qwer qwer 36 May 14 13:33 dir11 # 已經存在的非最內層目錄保留了元數據
drwxrwxrwx. 3 qwer qwer 36 May 14 13:33 dir12 # 已經存在的非最內層目錄保留了元數據
-rw-r--r--. 1 root root 17 May 14 04:49 file1.txt # 僅對目錄有效果,對文件無效果
[root@node1 dir2]# ll dir1/dir11
total 4
drwxr-xr-x. 2 root root 6 May 14 05:00 dir21 # 已經存在的最內層目錄沒有保留元數據
-rw-r--r--. 1 root root 15 May 14 04:48 file1.txt
[root@node1 dir2]#
4.4.4.歸檔提取覆蓋系統上現有文件
[root@node1 dir1]# cat file1.txt
aaaaaaaa
bbbbbbbb
cccccccc
[root@node1 dir1]# tar -cvf file1.tar file1.txt
file1.txt
[root@node1 dir1]# ll
total 16
-rw-r--r--. 1 root root 10240 May 14 13:54 file1.tar
-rw-r--r--. 1 root root 27 May 14 13:54 file1.txt
[root@node1 dir1]# vi file1.txt
[root@node1 dir1]# cat file1.txt # 文件修改后保留兩行
aaaaaaaa
bbbbbbbb
[root@node1 dir1]# tar --overwrite -xvf file1.tar # 覆蓋提取文件
file1.txt
[root@node1 dir1]# cat file1.txt # 覆蓋后文件還是原始的三行
aaaaaaaa
bbbbbbbb
cccccccc
[root@node1 dir1]#
4.4.5.文件添加到歸檔后刪除文件,同樣適用于目錄歸檔
[root@node1 dir1]# ll
total 4
-rw-r--r--. 1 root root 27 May 14 13:54 file1.txt
[root@node1 dir1]# tar --remove-files -cvf file1.tar file1.txt
file1.txt
[root@node1 dir1]# ll
total 12
-rw-r--r--. 1 root root 10240 May 14 14:10 file1.tar
[root@node1 dir1]#
4.4.6.提取歸檔時,先刪除對應內容(刪除目錄時,提升目錄非空錯誤)
[root@node1 dir2]# ll
total 12
drwxr-xr-x. 4 root root 49 May 14 04:49 dir1
-rw-r--r--. 1 root root 10240 May 14 05:01 dir1.tar
[root@node1 dir2]# tar -Uxvf dir1.tar
dir1/
tar: dir1: Cannot unlink: Directory not empty
dir1/dir11/
tar: dir1/dir11: Cannot unlink: Directory not empty
dir1/dir11/file1.txt
dir1/dir11/dir21/
dir1/dir12/
tar: dir1/dir12: Cannot unlink: Directory not empty
dir1/dir12/file1.txt
dir1/dir12/dir22/
dir1/file1.txt
tar: Exiting with failure status due to previous errors
[root@node1 dir2]#
4.4.7.寫入歸檔后驗證歸檔內文件目錄
[root@node1 dir2]# tar -Wcvf dir2.tar dir1/
dir1/
dir1/dir11/
dir1/dir11/file1.txt
dir1/dir11/dir21/
dir1/dir12/
dir1/dir12/file1.txt
dir1/dir12/dir22/
dir1/file1.txt
Verify dir1/
Verify dir1/dir11/
Verify dir1/dir11/file1.txt
Verify dir1/dir11/dir21/
Verify dir1/dir12/
Verify dir1/dir12/file1.txt
Verify dir1/dir12/dir22/
Verify dir1/file1.txt
[root@node1 dir2]#
4.5.選擇輸出流
4.5.1.將歸檔中的文件提取到標準輸出
[root@node1 dir1]# tar -Oxvf file1.tar
file1.txt
aaaaaaaa
bbbbbbbb
cccccccc
[root@node1 dir1]# ll
total 12
-rw-r--r--. 1 root root 10240 May 14 14:10 file1.tar
[root@node1 dir1]#
4.5.2.將歸檔中文件提取到指定命令進行處理
[root@node1 dir1]# tar -xvf file1.tar --to-command='cat'
file1.txt
aaaaaaaa
bbbbbbbb
cccccccc
[root@node1 dir1]# ll
total 12
-rw-r--r--. 1 root root 10240 May 14 14:10 file1.tar
[root@node1 dir1]# tar -xvf file1.tar --to-command='cat | grep aaa'
file1.txt
aaaaaaaa
[root@node1 dir1]#
4.6.文件屬性的處理
4.6.1.保留歸檔中文件訪問時間,無效果
[root@node1 dir1]# stat file1.txt File: ‘file1.txt’Size: 219712350 Blocks: 429128 IO Block: 4096 regular file
Device: 803h/2051d Inode: 35033734 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-05-14 16:06:13.430271347 +0800
Modify: 2025-05-14 16:03:46.000000000 +0800
Change: 2025-05-14 16:07:43.513273581 +0800Birth: -
[root@node1 dir1]# tar --atime-preserve='replace' -cvf file1.1.tar file1.txt
file1.txt
[root@node1 dir1]# tar --atime-preserve='replace' -xvf file1.1.tar
file1.txt
[root@node1 dir1]# stat file1.txt File: ‘file1.txt’Size: 219712350 Blocks: 429128 IO Block: 4096 regular file
Device: 803h/2051d Inode: 35033734 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-05-14 16:08:02.103274042 +0800 # 訪問時間改變了,無效果
Modify: 2025-05-14 16:03:46.000000000 +0800
Change: 2025-05-14 16:08:02.258274045 +0800Birth: -
[root@node1 dir1]#
4.6.2.歸檔時,指定組權限
[root@node1 dir2]# tar --group=qwer -cvf dir3.tar dir1/ # 歸檔時指定組權限歸檔
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 dir2]# ll
total 36
drwxr-xr-x. 4 root root 49 May 14 04:49 dir1 # dir1 是root組權限
-rw-r--r--. 1 root root 10240 May 14 05:01 dir1.tar
-rw-r--r--. 1 root root 10240 May 14 14:19 dir2.tar
-rw-r--r--. 1 root root 10240 May 14 16:53 dir3.tar
[root@node1 dir2]#
[root@node1 dir2]#
[root@node1 dir2]# tar --group=qwer -xvf dir3.tar # 提取歸檔后的目錄文件組權限就會改變
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 dir2]# ll
total 36
drwxr-xr-x. 4 root qwer 49 May 14 04:49 dir1
-rw-r--r--. 1 root root 10240 May 14 05:01 dir1.tar
-rw-r--r--. 1 root root 10240 May 14 14:19 dir2.tar
-rw-r--r--. 1 root root 10240 May 14 16:53 dir3.tar
[root@node1 dir2]#
4.6.3.創建歸檔時,修改Modify時間(mtime)
[root@node1 dir1]# stat file1.txt File: ‘file1.txt’Size: 41 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 35033735 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-05-14 16:12:36.390280843 +0800
Modify: 2025-05-14 16:10:53.000000000 +0800 # 文件原來的mtime時間
Change: 2025-05-14 16:12:36.390280843 +0800Birth: -
[root@node1 dir1]# tar --mtime='2025-05-14 16:01:01' -cvf file1.tar file1.txt
file1.txt
[root@node1 dir1]# tar -xvf file1.tar
file1.txt
[root@node1 dir1]# stat file1.txt File: ‘file1.txt’Size: 41 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 35033735 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-05-14 17:27:39.489392499 +0800
Modify: 2025-05-14 16:01:01.000000000 +0800 # 歸檔提取后mtime時間是創建歸檔時指定的時間
Change: 2025-05-14 17:27:39.489392499 +0800Birth: -
[root@node1 dir1]#
4.6.4.提取歸檔時,不保留文件的mtime時間
[root@node1 dir1]# stat file1.txt File: ‘file1.txt’Size: 41 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 35033735 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-05-14 17:33:54.282401793 +0800
Modify: 2025-05-14 16:01:01.000000000 +0800 # 正常歸檔提取的mtime時間
Change: 2025-05-14 17:33:54.282401793 +0800Birth: -
[root@node1 dir1]# tar -mxvf file1.tar
file1.txt
[root@node1 dir1]# stat file1.txt File: ‘file1.txt’Size: 41 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 35033735 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2025-05-14 17:34:26.442402590 +0800
Modify: 2025-05-14 17:34:26.442402590 +0800 # 指定-m參數歸檔提取的mtime時間
Change: 2025-05-14 17:34:26.442402590 +0800Birth: -
[root@node1 dir1]#
4.6.5.提取歸檔文件時是否保留歸檔中文件原有的擁有者(owner)、用戶組、用戶
[qwer@node1 dir1]$ ll dir1/
total 4
drwxr-xr-x. 3 qwer qwer 36 May 14 05:00 dir11
drwxr-xr-x. 3 qwer qwer 36 May 14 05:00 dir12
-rw-r--r--. 1 qwer qwer 17 May 14 04:49 file1.txt
[qwer@node1 dir1]$ tar -cvf dir1.tar dir1/ # 普通用戶壓縮目錄,整個目錄的用戶組和用戶權限都是qwer用戶
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[qwer@node1 dir1]$
-----------------------------------------------------
[root@node1 opt]# tar --no-same-owner -xvf dir1.tar # 提取歸檔后文件擁有者(owner)為當前用戶,與歸檔中文件擁有者(owner)不同
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 opt]# ll dir1
total 4
drwxr-xr-x. 3 root root 36 May 14 05:00 dir11
drwxr-xr-x. 3 root root 36 May 14 05:00 dir12
-rw-r--r--. 1 root root 17 May 14 04:49 file1.txt
[root@node1 opt]#
-----------------------------------------------------
[root@node1 opt]# tar --same-owner -xvf dir1.tar # 提取歸檔后文件屬性與歸檔中的文件擁有者(owner)相同
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 opt]# ll dir1
total 4
drwxr-xr-x. 3 qwer qwer 36 May 14 05:00 dir11
drwxr-xr-x. 3 qwer qwer 36 May 14 05:00 dir12
-rw-r--r--. 1 qwer qwer 17 May 14 04:49 file1.txt
[root@node1 opt]#
-----------------------------------------------------
[root@node1 opt]# tar --no-same-permissions -xvf dir1.tar # 提取歸檔后文件屬性保留歸檔前的用戶組和用戶權限
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 opt]# ll dir1/
total 4
drwxr-xr-x. 3 qwer qwer 36 May 14 05:00 dir11
drwxr-xr-x. 3 qwer qwer 36 May 14 05:00 dir12
-rw-r--r--. 1 qwer qwer 17 May 14 04:49 file1.txt
[root@node1 opt]#
4.6.6.歸檔時,用戶/組權限以數字方式保留,提取歸檔時顯示數字對應的用戶(與之前的用戶可能不一樣)或者顯示數字(數字對應的用戶不存在時)
[root@node1 dir1]# cat /etc/passwd
... ...
u1:x:1001:1001::/home/u1:/bin/bash
[root@node1 opt]# ll
total 0
drwxr-xr-x. 4 u1 u1 49 May 14 04:49 dir1
... ...
[root@node1 opt]# tar --numeric-owner -cvf dir1.tar dir1/ # 歸檔時,指定用戶/組權限使用數字方式保留
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 opt]#
[root@node1 opt]# useradd u2
[root@node1 opt]# useradd u3
[root@node1 opt]# userdel u1
[root@node1 opt]# useradd u1
[root@node1 dir1]# cat /etc/passwd
... ...
u3:x:1002:1002::/home/u2:/bin/bash
u4:x:1003:1003::/home/u3:/bin/bash
u1:x:1004:1004::/home/u1:/bin/bash
[root@node1 opt]# tar --numeric-owner -xvf dir1.tar # tar -xvf dir1.tar # 以數字保留歸檔中文件的用戶/組權限,解壓后如果數字對應的用戶不存在就顯示數字
dir1/
dir1/dir11/
dir1/dir11/dir21/
dir1/dir11/file1.txt
dir1/dir12/
dir1/dir12/dir22/
dir1/dir12/file1.txt
dir1/file1.txt
[root@node1 opt]# ll
total 12
drwxr-xr-x. 4 1001 1001 49 May 14 04:49 dir1
... ...
[root@node1 opt]#
4.6.7.創建歸檔時,指定用戶權限
[root@node1 dir1]# ll
total 4
-rw-r--r--. 1 root root 41 May 14 17:34 file1.txt
[root@node1 dir1]# tar --owner='qwer' -cvf file1.tar file1.txt
file1.txt
[root@node1 dir1]# ll
total 16
-rw-r--r--. 1 root root 10240 May 14 18:35 file1.tar
-rw-r--r--. 1 root root 41 May 14 17:34 file1.txt
[root@node1 dir1]# tar -xvf file1.tar
file1.txt
[root@node1 dir1]# ll
total 16
-rw-r--r--. 1 root root 10240 May 14 18:35 file1.tar
-rw-r--r--. 1 qwer root 41 May 14 17:34 file1.txt # 提取歸檔后文件為創建歸檔時指定的用戶權限
[root@node1 dir1]#
4.7.設備選擇和切換
4.7.1.帶有冒號的歸檔文件視為本地文件
[root@node1 dir1]# tar -cvf file:1.tar file1.txt # 歸檔名稱定義帶有冒號,執行會卡住,Ctrl+c強制退出
^C
[root@node1 dir1]# tar --force-local -cvf file:1.tar file1.txt # 強制視為本地文件
file1.txt
[root@node1 dir1]# ll
total 16
-rw-r--r--. 1 root root 10240 May 14 19:19 file:1.tar
-rw-r--r--. 1 root root 20 May 14 19:18 file1.txt
[root@node1 dir1]#
4.8.檔案格式選擇
4.8.1.給歸檔設置一個標簽
[root@node1 dir1]# tar -cvf file1.tar file1.txt
file1.txt
[root@node1 dir1]# tar -V 'This is label' -cvf file1_1.tar file1.txt
This is label
file1.txt
[root@node1 dir1]# tar -tf file1.tar
file1.txt
[root@node1 dir1]# tar -tf file1_1.tar
This is label # 元數據中可以看到歸檔是自定義的標簽
file1.txt
[root@node1 dir1]#
4.9.壓縮選項?
4.9.1.按照歸檔文件的后綴名進行歸檔或者壓縮
[root@node1 dir1]# tar -cvf file1.1.tar file1.txt
file1.txt
[root@node1 dir1]# tar -zcvf file1.2.tar file1.txt
file1.txt
[root@node1 dir1]# tar -acvf file1.3.tar file1.txt
file1.txt
[root@node1 dir1]# tar -acvf file1.4.tar.gz file1.txt
file1.txt
[root@node1 dir1]# ll
total 644964
-rw-r--r--. 1 root root 219719680 May 15 19:51 file1.1.tar
-rw-r--r--. 1 root root 639173 May 15 19:51 file1.2.tar.gz
-rw-r--r--. 1 root root 219719680 May 15 19:53 file1.3.tar
-rw-r--r--. 1 root root 639173 May 15 19:53 file1.4.tar.gz
-rw-r--r--. 1 root root 219712350 May 15 19:50 file1.txt
[root@node1 dir1]#
4.9.2.選擇壓縮方式進行歸檔
[root@node1 dir1]# tar -zcvf dir2.tar.gz dir2/
dir2/
dir2/file4.txt
dir2/file56.tat
dir2/file5.txt
dir2/file6.txt
dir2/file7.txt
[root@node1 dir1]# tar -jcvf dir2.tar.bz2 dir2/
dir2/
dir2/file4.txt
dir2/file56.tat
dir2/file5.txt
dir2/file6.txt
dir2/file7.txt
[root@node1 dir1]# tar -Jcvf dir2.tar.xz dir2/
dir2/
dir2/file4.txt
dir2/file56.tat
dir2/file5.txt
dir2/file6.txt
dir2/file7.txt
[root@node1 dir1]# tar --lzip -cvf dir2.tar.lz dir2/
tar (child): lzip: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
dir2/
dir2/file4.txt
[root@node1 dir1]# tar --lzma -cvf dir2.tar.lzma dir2/
tar (child): lzma: Cannot execdir2/
dir2/file4.txt
: No such file or directory
tar (child): Error is not recoverable: exiting now
[root@node1 dir1]#
[root@node1 dir1]# tar --lzop -cvf dir2.tar.lzop dir2/
dir2/
dir2/file4.txt
dir2/file56.tat
dir2/file5.txt
dir2/file6.txt
dir2/file7.txt
[root@node1 dir1]# tar -Zcvf dir2.tar.Z dir2/
dir2/
dir2/file4.txt
dir2/file56.tat
dir2/file5.txt
dir2/file6.txt
dir2/file7.txt
[root@node1 dir1]#
[root@node1 dir1]# ll
total 2388
drwxr-xr-x. 2 root root 92 May 15 20:31 dir2
-rw-r--r--. 1 root root 10782 May 15 20:32 dir2.tar.bz2
-rw-r--r--. 1 root root 639568 May 15 20:31 dir2.tar.gz
-rw-r--r--. 1 root root 0 May 15 20:46 dir2.tar.lz # 執行報錯,壓縮歸檔文件大小為0
-rw-r--r--. 1 root root 0 May 15 20:46 dir2.tar.lzma # 執行報錯,壓縮歸檔文件大小為0
-rw-r--r--. 1 root root 1100615 May 15 20:47 dir2.tar.lzop
-rw-r--r--. 1 root root 32500 May 15 20:40 dir2.tar.xz
-rw-r--r--. 1 root root 651647 May 15 20:47 dir2.tar.Z
[root@node1 dir1]# # 可更具其大小判斷壓縮比
4.10.本地文件選擇
4.10.1.系統上如已經存在,可選擇備份(使用數字備份)
[root@node1 dir1]# ll
total 24
-rw-r--r--. 1 root root 14 May 15 20:58 file1.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file2.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file3.txt
-rw-r--r--. 1 root root 10240 May 15 21:49 file.tar
[root@node1 dir1]# tar -tf file.tar
file1.txt
file2.txt
file3.txt
[root@node1 dir1]# tar --backup=t -xvf file.tar
file1.txt
Renaming `file1.txt' to `file1.txt.~1~'
file2.txt
Renaming `file2.txt' to `file2.txt.~1~'
file3.txt
Renaming `file3.txt' to `file3.txt.~1~'
[root@node1 dir1]# ll
total 36
-rw-r--r--. 1 root root 14 May 15 20:58 file1.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file1.txt.~1~ # 已存在文件進行備份,使用數字后綴
-rw-r--r--. 1 root root 14 May 15 20:58 file2.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file2.txt.~1~
-rw-r--r--. 1 root root 14 May 15 20:58 file3.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file3.txt.~1~
-rw-r--r--. 1 root root 10240 May 15 21:49 file.tar
[root@node1 dir1]#
4.10.2.提取歸檔內容到指定的目錄(默認提取是當前目錄)
[root@node1 dir1]# ll dir2/
total 0
[root@node1 dir1]# tar -C dir2/ -xvf file.tar
file1.txt
file2.txt
file3.txt
[root@node1 dir1]# ll dir2/
total 12
-rw-r--r--. 1 root root 14 May 15 20:58 file1.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file2.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file3.txt
[root@node1 dir1]#
4.10.3.提取/創建歸檔時排除指定的文件
[root@node1 dir1]# ll
total 12
-rw-r--r--. 1 root root 14 May 15 20:58 file1.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file2.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file3.txt
[root@node1 dir1]# tar --exclude=file2.txt -cvf file.tar file*.txt
file1.txt
file3.txt
[root@node1 dir1]#
------------------------------------------------------------------
[root@node1 dir1]# ll dir2/
total 0
[root@node1 dir1]# tar -C dir2/ --exclude=file2.txt -xvf file.tar
file1.txt
file3.txt
[root@node1 dir1]# ll dir2/
total 8
-rw-r--r--. 1 root root 14 May 15 20:58 file1.txt
-rw-r--r--. 1 root root 14 May 15 20:58 file3.txt
[root@node1 dir1]#
4.10.4.壓縮時是否保留符號鏈接(軟鏈接/硬鏈接)
[root@node1 Desktop]# ll dir1
total 8
lrwxrwxrwx. 1 root root 5 May 16 04:41 dir -> dir2/
drwxr-xr-x. 2 root root 6 May 16 04:40 dir2
drwxr-xr-x. 2 root root 6 May 16 04:41 dir3
-rw-r--r--. 1 root root 12 May 16 04:40 file1.txt
-rw-r--r--. 1 root root 58 May 16 04:40 file2.txt
lrwxrwxrwx. 1 root root 9 May 16 04:41 file.txt -> file1.txt
[root@node1 Desktop]# tar --dereference -cvf dir1.tar dir1/
dir1/
dir1/file1.txt
dir1/file2.txt
dir1/dir2/
dir1/dir3/
dir1/file.txt
dir1/dir/
[root@node1 Desktop]# tar -tvf dir1.tar
drwxr-xr-x root/root 0 2025-05-16 04:41 dir1/
-rw-r--r-- root/root 12 2025-05-16 04:40 dir1/file1.txt
-rw-r--r-- root/root 58 2025-05-16 04:40 dir1/file2.txt
drwxr-xr-x root/root 0 2025-05-16 04:40 dir1/dir2/
drwxr-xr-x root/root 0 2025-05-16 04:41 dir1/dir3/
hrw-r--r-- root/root 0 2025-05-16 04:40 dir1/file.txt link to dir1/file1.txt
drwxr-xr-x root/root 0 2025-05-16 04:40 dir1/dir/
[root@node1 Desktop]# rm -rf dir1/
[root@node1 Desktop]# tar xvf dir1.tar
dir1/
dir1/file1.txt
dir1/file2.txt
dir1/dir2/
dir1/dir3/
dir1/file.txt
dir1/dir/
[root@node1 Desktop]# ll dir1
total 12
drwxr-xr-x. 2 root root 6 May 16 04:40 dir
drwxr-xr-x. 2 root root 6 May 16 04:40 dir2
drwxr-xr-x. 2 root root 6 May 16 04:41 dir3
-rw-r--r--. 2 root root 12 May 16 04:40 file1.txt
-rw-r--r--. 1 root root 58 May 16 04:40 file2.txt
-rw-r--r--. 2 root root 12 May 16 04:40 file.txt
[root@node1 Desktop]#
--------------------------------------------------
[root@node1 dir1]# ln file1.txt file.txt
[root@node1 dir1]# ll
total 12
drwxr-xr-x. 2 root root 6 May 16 04:40 dir2
drwxr-xr-x. 2 root root 6 May 16 04:41 dir3
-rw-r--r--. 2 root root 12 May 16 04:40 file1.txt
-rw-r--r--. 1 root root 58 May 16 04:40 file2.txt
-rw-r--r--. 2 root root 12 May 16 04:40 file.txt
[root@node1 dir1]# cd ..
[root@node1 Desktop]# tar --hard-dereference -cvf dir1.tar dir1/
dir1/
dir1/file1.txt
dir1/file2.txt
dir1/dir2/
dir1/dir3/
dir1/file.txt
[root@node1 Desktop]#
[root@node1 Desktop]# tar -xvf dir1.tar
dir1/
dir1/file1.txt
dir1/file2.txt
dir1/dir2/
dir1/dir3/
dir1/file.txt
[root@node1 Desktop]# ll dir1
total 12
drwxr-xr-x. 2 root root 6 May 16 04:40 dir2
drwxr-xr-x. 2 root root 6 May 16 04:41 dir3
-rw-r--r--. 1 root root 20 May 16 04:52 file1.txt
-rw-r--r--. 1 root root 58 May 16 04:40 file2.txt
-rw-r--r--. 1 root root 20 May 16 04:52 file.txt
[root@node1 Desktop]# vi dir1/file.txt
[root@node1 Desktop]# cat dir1/file.txt
asjdlfjaldk
iaaaaaa
ibbbbbb
[root@node1 Desktop]# cat dir1/file1.txt # 已經不是硬鏈接,硬鏈接修改一個,另一個也會更新
asjdlfjaldk
iaaaaaa
[root@node1 Desktop]#
4.10.5.從指定的文件開始解壓
[root@node1 Desktop]# tar -tf dir1.2.tar.gz
dir1/
dir1/file1.txt
dir1/file2.txt
dir1/file3.txt
dir1/file4.txt
dir1/file56.tat
dir1/file5.txt
dir1/file6.txt
dir1/file7.txt
dir1/file8.txt
[root@node1 Desktop]# tar -zxvf dir1.2.tar.gz -K dir1/file56.tat
dir1/file56.tat
dir1/file5.txt
dir1/file6.txt
dir1/file7.txt
dir1/file8.txt
[root@node1 Desktop]# ll dir1
total 2097176
-rw-r--r--. 1 root root 10240 May 15 20:02 file56.tat
-rw-r--r--. 1 root root 28 May 15 20:02 file5.txt
-rw-r--r--. 1 root root 20 May 15 20:02 file6.txt
-rw-r--r--. 1 root root 2390 May 15 20:02 file7.txt
-rw-r--r--. 1 root root 2147483648 May 15 20:02 file8.txt
[root@node1 Desktop]#
4.10.6.僅歸檔某一個時間及其之后時間的的文件(s時間包括mtime、ctime)
[root@node1 Desktop]# stat dir1/* | grep Modify
Modify: 2025-05-15 20:02:15.000000000 +0800
Modify: 2025-05-15 20:02:15.000000000 +0800
Modify: 2025-05-15 20:02:15.000000000 +0800
Modify: 2025-05-16 05:14:29.701865574 +0800
Modify: 2025-05-16 05:12:46.702869002 +0800
[root@node1 Desktop]# tar --newer-mtime="2025-05-16 00:00:00" -cvf dir1.tar dir1/
dir1/
tar: dir1/file5.txt: file is unchanged; not dumped
tar: dir1/file6.txt: file is unchanged; not dumped
tar: dir1/file7.txt: file is unchanged; not dumped
dir1/file8.txt
dir1/file9.txt
[root@node1 Desktop]#[root@node1 Desktop]# stat dir1/* | grep Change
Change: 2025-05-16 06:00:42.509773290 +0800
Change: 2025-05-16 06:00:42.509773290 +0800
Change: 2025-05-16 06:00:42.509773290 +0800
Change: 2025-05-16 06:01:29.106771739 +0800
Change: 2025-05-16 06:01:39.172771404 +0800
[root@node1 Desktop]#
[root@node1 Desktop]# tar -N '2025-05-16 06:00:45' -cvf dir1.tar dir1/
dir1/
tar: dir1/file5.txt: file is unchanged; not dumped
tar: dir1/file6.txt: file is unchanged; not dumped
tar: dir1/file7.txt: file is unchanged; not dumped
dir1/file8.txt
dir1/file9.txt
[root@node1 Desktop]#
4.10.7.在文件中指定(-T)/排除(-X)需要提取/創建歸檔的文件名稱
[root@node1 dir1]# ll
total 24
-rw-r--r--. 1 root root 28 May 15 20:02 file5.txt
-rw-r--r--. 1 root root 20 May 15 20:02 file6.txt
-rw-r--r--. 1 root root 2390 May 15 20:02 file7.txt
-rw-r--r--. 1 root root 15 May 16 05:14 file8.txt
-rw-r--r--. 1 root root 14 May 16 05:12 file9.txt
-rw-r--r--. 1 root root 20 May 16 05:27 filelist.txt
[root@node1 dir1]# cat filelist.txt
file5.txt
file7.txt
[root@node1 dir1]# tar -cvf dir1.tar -T filelist.txt
file5.txt
file7.txt
[root@node1 dir1]# tar --no-null -cvf dir1.tar -T filelist.txt
file5.txt
file7.txt
[root@node1 dir1]#
[root@node1 dir1]# tar --null -cvf dir1.tar -T filelist.txt
tar: file5.txt\nfile7.txt\n: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
[root@node1 dir1]#
--------------------------------------------------------
[root@node1 dir1]# ll
total 24
-rw-r--r--. 1 root root 28 May 15 20:02 file5.txt
-rw-r--r--. 1 root root 20 May 15 20:02 file6.txt
-rw-r--r--. 1 root root 2390 May 15 20:02 file7.txt
-rw-r--r--. 1 root root 25 May 16 06:01 file8.txt
-rw-r--r--. 1 root root 81 May 16 06:01 file9.txt
-rw-r--r--. 1 root root 20 May 16 06:36 filelist
[root@node1 dir1]# cat filelist
file6.txt
file9.txt
[root@node1 dir1]# tar -X filelist -cvf file.tar file*.txt
file5.txt
file7.txt
file8.txt
[root@node1 dir1]#
4.10.8.按照目錄的層級進行歸檔
[root@node1 Desktop]# ll dir1
total 20
drwxr-xr-x. 2 root root 23 May 16 05:39 dir1
drwxr-xr-x. 2 root root 23 May 16 05:39 dir2
-rw-r--r--. 1 root root 28 May 15 20:02 file5.txt
-rw-r--r--. 1 root root 20 May 15 20:02 file6.txt
-rw-r--r--. 1 root root 2390 May 15 20:02 file7.txt
-rw-r--r--. 1 root root 15 May 16 05:14 file8.txt
-rw-r--r--. 1 root root 14 May 16 05:12 file9.txt
[root@node1 Desktop]# ll dir1/dir1
total 0
-rw-r--r--. 1 root root 0 May 16 05:39 file1.txt
[root@node1 Desktop]# ll dir1/dir2
total 0
-rw-r--r--. 1 root root 0 May 16 05:39 file2.txt
[root@node1 Desktop]#
[root@node1 Desktop]# tar --no-recursion -cvf dir1.tar dir1/ # 僅歸檔指定的當前目錄層級,其內部的文件或者目錄都不會歸檔
dir1/ # 上面指定dir1,就歸檔該目錄,其下的文件目錄不會歸檔
[root@node1 Desktop]# tar --no-recursion -cvf dir1.tar dir1/* # 這里的dir1/*,時dir1的下面一層,再下一次就不會被歸檔
dir1/dir1/
dir1/dir2/
dir1/file5.txt
dir1/file6.txt
dir1/file7.txt
dir1/file8.txt
dir1/file9.txt
[root@node1 Desktop]#
4.10.9.帶有根目錄開始的目錄或文件,-P參數歸檔不會去除根目錄,看以下測試效果
[root@node1 Desktop]# tar -cvPf back.tar /root/Desktop/dir1
/root/Desktop/dir1/
/root/Desktop/dir1/file5.txt
/root/Desktop/dir1/file6.txt
/root/Desktop/dir1/file7.txt
/root/Desktop/dir1/file8.txt
/root/Desktop/dir1/file9.txt
[root@node1 Desktop]#
[root@node1 Desktop]#
[root@node1 Desktop]# tar -cvf back1.tar /root/Desktop/dir1
tar: Removing leading `/' from member names # 沒有-P參數
/root/Desktop/dir1/
/root/Desktop/dir1/file5.txt
/root/Desktop/dir1/file6.txt
/root/Desktop/dir1/file7.txt
/root/Desktop/dir1/file8.txt
/root/Desktop/dir1/file9.txt
[root@node1 Desktop]#
[root@node1 Desktop]# tar -xvPf back.tar
/root/Desktop/dir1/
/root/Desktop/dir1/file5.txt
/root/Desktop/dir1/file6.txt
/root/Desktop/dir1/file7.txt
/root/Desktop/dir1/file8.txt
/root/Desktop/dir1/file9.txt
[root@node1 Desktop]#
[root@node1 Desktop]# tar -xvf back1.tar # 沒有-P參數
root/Desktop/dir1/
root/Desktop/dir1/file5.txt
root/Desktop/dir1/file6.txt
root/Desktop/dir1/file7.txt
root/Desktop/dir1/file8.txt
root/Desktop/dir1/file9.txt
[root@node1 Desktop]#
4.11.文件名轉換?
4.11.1.指定解壓的目錄層級
[root@node1 dir1]# tar --strip-components=1 -xvf Desktop.tar
root/Desktop/dir1/
root/Desktop/dir1/file5.txt
root/Desktop/dir1/file6.txt
root/Desktop/dir1/file7.txt
root/Desktop/dir1/file8.txt
root/Desktop/dir1/file9.txt
root/Desktop/dir1/filelist
root/Desktop/dir1/file.tar
[root@node1 dir1]# ll
total 20
drwxr-xr-x. 3 root root 18 May 16 06:56 Desktop # 去除1層后是Desktop
-rw-r--r--. 1 root root 20480 May 16 06:54 Desktop.tar
[root@node1 dir1]#
[root@node1 dir1]# tar --strip-components=2 -xvf Desktop.tar
root/Desktop/dir1/
root/Desktop/dir1/file5.txt
root/Desktop/dir1/file6.txt
root/Desktop/dir1/file7.txt
root/Desktop/dir1/file8.txt
root/Desktop/dir1/file9.txt
root/Desktop/dir1/filelist
root/Desktop/dir1/file.tar
[root@node1 dir1]# ll
total 20
-rw-r--r--. 1 root root 20480 May 16 06:54 Desktop.tar
drwxr-xr-x. 2 root root 123 May 16 06:36 dir1 # 去除2層后是dir1
[root@node1 dir1]#
4.11.2.sed正則修改歸檔中文件的名稱
[root@node1 dir1]# ll
total 12
-rw-r--r--. 1 root root 10240 May 16 06:58 file.tar
[root@node1 dir1]# tar -tf file.tar
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt
[root@node1 dir1]# tar -xvf file.tar --transform='s/file/test/g'
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt
[root@node1 dir1]# ll
total 32
-rw-r--r--. 1 root root 10240 May 16 06:58 file.tar
-rw-r--r--. 1 root root 28 May 15 20:02 test5.txt
-rw-r--r--. 1 root root 20 May 15 20:02 test6.txt
-rw-r--r--. 1 root root 2390 May 15 20:02 test7.txt
-rw-r--r--. 1 root root 25 May 16 06:01 test8.txt
-rw-r--r--. 1 root root 81 May 16 06:01 test9.txt
[root@node1 dir1]#
4.12.文件名匹配選項
4.12.1.匹配規則(忽略大小寫、嚴格大小寫、通配符、逐字符匹配)
[root@node1 dir1]# ll
total 20
-rw-r--r--. 1 root root 28 May 15 20:02 test5.txt
-rw-r--r--. 1 root root 20 May 15 20:02 test6.txt
-rw-r--r--. 1 root root 2390 May 15 20:02 test7.txt
-rw-r--r--. 1 root root 25 May 16 06:01 test8.txt
-rw-r--r--. 1 root root 81 May 16 06:01 test9.txt
[root@node1 dir1]# tar -cvf file.tar test* --ignore-case --exclude=Test7.txt # 忽略大小寫匹配
test5.txt
test6.txt
test8.txt
test9.txt
[root@node1 dir1]# tar -cvf file.tar test* --no-ignore-case --exclude=Test7.txt # 嚴格大小寫匹配
test5.txt
test6.txt
test7.txt
test8.txt
test9.txt
[root@node1 dir1]# tar -cvf file.tar test* --wildcards --exclude=test7.* # 通配符匹配
test5.txt
test6.txt
test8.txt
test9.txt
[root@node1 dir1]# tar -cvf file.tar test* --no-wildcards --exclude=test7.* # 逐字符匹配
test5.txt
test6.txt
test7.txt
test8.txt
test9.txt
[root@node1 dir1]#
4.12.2.顯示進度
[root@node1 Desktop]# tar -zxvf dir1.2.tar.gz -C dir2/ --checkpoint=10000 # 指定顯示進度的頻率,值越大越不頻繁
dir1/
dir1/file1.txt
tar: Read checkpoint 10000
tar: Read checkpoint 20000
dir1/file2.txt
tar: Read checkpoint 30000
tar: Read checkpoint 40000
... ...
[root@node1 Desktop]# tar -zxvf dir1.2.tar.gz -C dir2/ --checkpoint=10000 --checkpoint-action=echo="%u files processed" # 自定義進度顯示
dir1/
dir1/file1.txt
tar: 10000 files processed
tar: 20000 files processed
dir1/file2.txt
tar: 30000 files processed
tar: 40000 files processed
... ...
[root@node1 Desktop]# tar -zxvf dir1.2.tar.gz -C dir2/ --checkpoint=10000 --checkpoint-action=ttyout="%s %u \r" # 末尾可\r \n \t
... ...
[root@node1 Desktop]#
4.12.3.如果鏈接歸檔錯誤會輸出信息
[root@node1 dir1]# ln test5.txt test.txt
[root@node1 dir1]# tar -cvlf test.tar *
test5.txt
test6.txt
test7.txt
test8.txt
test9.txt
test.txt
[root@node1 dir1]# tar -tvf test.tar
-rw-r--r-- root/root 28 2025-05-15 20:02 test5.txt
-rw-r--r-- root/root 20 2025-05-15 20:02 test6.txt
-rw-r--r-- root/root 2390 2025-05-15 20:02 test7.txt
-rw-r--r-- root/root 25 2025-05-16 06:01 test8.txt
-rw-r--r-- root/root 81 2025-05-16 06:01 test9.txt
hrw-r--r-- root/root 0 2025-05-15 20:02 test.txt link to test5.txt
[root@node1 dir1]#
4.12.4.查看替換文件名稱
[root@node1 dir1]# tar -tf test.tar --transform='s/test/aaa/g'
test5.txt
test6.txt
test7.txt
test8.txt
test9.txt
test.txt
[root@node1 dir1]# tar -tf test.tar --transform='s/test/aaa/g' --show-transformed-names
aaa5.txt
aaa6.txt
aaa7.txt
aaa8.txt
aaa9.txt
aaa.txt
[root@node1 dir1]# cd /opt
[root@node1 opt]# tar -tf file.tar --strip-components=2
root/Desktop/dir1/
root/Desktop/dir1/test5.txt
root/Desktop/dir1/test6.txt
root/Desktop/dir1/test7.txt
root/Desktop/dir1/test8.txt
root/Desktop/dir1/test9.txt
root/Desktop/dir1/file.tar
[root@node1 opt]# tar -tf file.tar --strip-components=2 --show-transformed-names
dir1
dir1/test5.txt
dir1/test6.txt
dir1/test7.txt
dir1/test8.txt
dir1/test9.txt
dir1/file.tar
[root@node1 opt]#