tar命令用來將多個文件歸檔成一個文件。用法為:tar [選項] [文件]
[root@osker ~]# tar --help Usage: tar [OPTION...] [FILE]... GNU `tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive. ...
GUN官方文檔:https://www.gnu.org/software/tar/manual/tar.pdf
測試環境
[root@backup ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@backup ~]# uname -r 3.10.0-957.el7.x86_64 [root@backup ~]# rpm -qa tar tar-1.26-35.el7.x86_64
1.打包並壓縮
[root@osker ~]# tar -czvf ./osker.tar.gz /osker/* tar: Removing leading `/' from member names /osker/foo1.txt /osker/foo2.txt /osker/foo3.txt [root@osker ~]# ll -rw-r--r-- 1 root root 135 Apr 12 20:03 osker.tar.gz
-c, --create create a new archive
-c或--create 建立新的備份文件
-z, --gzip, --gunzip, --ungzip filter the archive through gzip
-z或--gzip或--ungzip 通過gzip指令處理備份文件
-j或--bzip2 支持bzip2解壓文件
-J或--xz 支持xz解壓文件
-v, --verbose verbosely list files processed
-v或--verbose 顯示指令執行過程
-f, --file=ARCHIVE use archive file or device ARCHIVE
-f或--file=備份文件 指定備份文件
2.查看壓縮包
[root@osker ~]# tar -tf osker.tar.gz osker/foo1.txt osker/foo2.txt osker/foo3.txt
-t, --list list the contents of an archive
-t或--list 列出備份文件的內容
3.解壓
[root@osker ~]# tar -xf osker.tar.gz [root@osker ~]# tree ./osker ./osker ├── foo1.txt ├── foo2.txt └── foo3.txt 0 directories, 3 files #使用-C,來指定解壓的位置 [root@osker ~]# tar -xf osker.tar.gz -C /opt/ [root@osker ~]# tree /opt /opt └── osker ├── foo1.txt ├── foo2.txt └── foo3.txt 1 directory, 3 files
-x, --extract, --get extract files from an archive
-x或--extract或--get 從備份文件中還原文件
-C, --directory=DIR change to directory DIR
-C <目錄> 解壓到指定的目錄
4.打包壓縮時候排除某些文件
詳見鏈接:https://www.cnblogs.com/osker/p/12427447.html
需使用以下參數:
--exclude=PATTERN exclude files, given as a PATTERN
-X, --exclude-from=FILE exclude patterns listed in FILE
-T, --files-from=FILE get names to extract or create from FILE
[root@localhost ~]# cat tar-include /root [root@localhost ~]# cat tar-exclude /root/rootbak.tar /root/tmp.tar.bz2 [root@localhost ~]# tar czvf mybackup.tgz -T /root/tar-include -X /root/tar-exclude
5.不移除文件名稱前的“/”號
平時打包的時候如果使用絕對路徑的時候,打包會有提示:tar: Removing leading `/' from member names。加上-P參數就不會提示了,但打包的時候會將“/”目錄加進去,如果解壓這個壓縮包的時候當前目錄為“/”就比較危險。
下面這一個參數慎用!!! 下面這一個參數慎用!!! 下面這一個參數慎用!!!
[root@osker ~]# tar -czvPf foo.tar.gz /tmp /tmp/ /tmp/.font-unix/ /tmp/.Test-unix/ /tmp/.XIM-unix/ /tmp/.ICE-unix/ /tmp/.X11-unix/ /tmp/gen.txt [root@osker ~]# tar -czvf foo.tar.gz /tmp tar: Removing leading `/' from member names /tmp/ /tmp/.font-unix/ /tmp/.Test-unix/ /tmp/.XIM-unix/ /tmp/.ICE-unix/ /tmp/.X11-unix/ /tmp/gen.txt
-P, --absolute-names don't strip leading `/'s from file names
-P或--absolute-names:文件名使用絕對名稱,不移除文件名稱前的“/”號;
大家還是忘了它吧!!!
6.打包軟連接的源文件
打包壓縮時候遇到文件中有軟連接文件的時候,建議使用-h參數,以免打包后軟鏈接失效!
[root@osker ~]# ll osker total 0 lrwxrwxrwx 1 root root 11 Apr 12 21:08 e.ln -> /root/e.txt -rw-r--r-- 1 root root 0 Apr 12 20:03 foo1.txt -rw-r--r-- 1 root root 0 Apr 12 20:03 foo2.txt -rw-r--r-- 1 root root 0 Apr 12 20:03 foo3.txt [root@osker ~]# cat osker/e.ln osker/excd.txt [root@osker ~]# tar -czvhf ./soft.tar.gz ./osker ./osker/ ./osker/foo1.txt ./osker/foo2.txt ./osker/foo3.txt ./osker/e.ln [root@osker ~]# tar -xf ./soft.tar.gz -C /opt [root@osker ~]# tar -czvf ./osker.tar.gz ./osker ./osker/ ./osker/foo1.txt ./osker/foo2.txt ./osker/foo3.txt ./osker/e.ln [root@osker ~]# tar -xf ./osker.tar.gz -C /tmp [root@osker ~]# rm -f e.txt [root@osker ~]# cat /opt/osker/e.ln osker/excd.txt [root@osker ~]# cat /tmp/osker/e.ln cat: /tmp/osker/e.ln: No such file or directory
#注意:h需要寫到f前面。
-h, --dereference follow symlinks; archive and dump the files they point to
-h或--dereference 歸檔時,打包軟連接的源文件
7.保留文件的原本屬性
平時打包壓縮文件的時候多數都要用來備份的,這個時候就需要保留文件的原本屬性,就需要用到-p參數。
[root@osker ~]# tar -czvpf ./etc.tar.gz /etc
#注意:p需要寫到f前面。
-p, --preserve-permissions, --same-permissions extract information about file permissions,(default for superuser)
-p或--same-permissions或--same-permissions 用原來的文件權限還原文件;
8.打包的時候只將較指定日期更新的文件保存到備份文件里
-N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE
-N <日期格式> 或 --newer=<日期時間>:只將較指定日期更新的文件保存到備份文件里
[root@instance-16 ~]# ls -l --time-style=full-iso total 12 -rw-r--r--. 1 root root 483 2020-04-11 09:01:29.905704747 +0800 3.txt -rw-r--r--. 1 root root 1080 2020-04-12 15:43:03.207986554 +0800 passwd drwxr-xr-x. 2 root root 6 2020-04-12 15:15:24.432926152 +0800 qiu -rw-r--r--. 1 root root 151 2020-03-27 06:45:33.571820634 +0800 test.txt -rw-r--r--. 1 root root 0 2020-04-13 07:17:12.856865010 +0800 tt.txt [root@instance-16 ~]# tar -N '2020-04-12' -czvf /tmp/o.tar ./* tar: Option --after-date: Treating date `2020-04-12' as 2020-04-12 00:00:00 tar: ./3.txt: file is unchanged; not dumped ./passwd ./qiu/ tar: ./test.txt: file is unchanged; not dumped ./tt.txt
可以從提示中看到有2個文件沒有被打包進去。
9.添加文件到已有的打包文件中
-r, --append append files to the end of an archive
-r 添加文件到已經壓縮的文件
[root@backup test]# tar -tf fo2.tar foo2.txt anaconda-ks.cfg [root@backup test]# tar -rvf fo2.tar ~/foo foo1.txt foo2.txt foo3.txt [root@backup test]# tar -rvf fo2.tar ~/foo1.txt tar: Removing leading `/' from member names /root/foo1.txt [root@backup test]# tar -tf fo2.tar foo2.txt anaconda-ks.cfg root/foo1.txt
#注意:只能將新文件更新到打包文件里,不能更新到壓縮文件。
10.添加改變了的文件到已經存在的打包文件中
-u, --update only append files newer than copy in archive
-u 添加改變了的文件到已經存在的打包文件中
[root@osker ~]# tar -cvf os.tar ./osker ./osker/ ./osker/1.txt ./osker/2.txt ./osker/3.txt [root@osker ~]# echo "up" > osker/3.txt [root@osker ~]# tar -uvf os.tar ./osker ./osker/3.txt
#注意:只能將新文件更新到打包文件里,不能更新到壓縮文件。
11.從歸檔中刪除文件
--delete delete from the archive (not on mag tapes!)
--delete 從歸檔中刪除文件
[root@backup ~]# tar -tf /opt/foo.tar ./anaconda-ks.cfg ./del.txt ./foo1.txt ./foo2.txt ./foo3.txt [root@backup ~]# cd /opt [root@backup opt]# tar --delete del.txt -vf foo.tar tar: del.txt: Not found in archive tar: Exiting with failure status due to previous errors [root@backup opt]# tar --delete ./del.txt -vf foo.tar [root@backup opt]# tar -tf foo.tar ./anaconda-ks.cfg ./foo1.txt ./foo2.txt ./foo3.txt
可以看到del.txt已經沒有在foo.tar包中了。
12.新增歸檔文件到另一個歸檔文件
-A, --catenate, --concatenate append tar files to an archive
-A或--catenate:新增歸檔文件到另一個歸檔文件
[root@backup ~]# tar -cf test/fo1.tar anaconda-ks.cfg [root@backup ~]# tar -cf test/fo2.tar foo2.txt [root@backup ~]# cd test [root@backup test]# tar -A fo1.tar -vf fo2.tar [root@backup test]# tar -tf fo2.tar foo2.txt anaconda-ks.cfg
可以看到fo1.tar中得anaconda-ks.cfg已經添加到fo2.tar中了。
13.解壓時候,將文件得mtime修改為當前時間
-m, --touch don't extract file modified time
-m或--touch 解壓時候,將文件得mtime修改為當前時間
[root@backup test]# tar -xf fo2.tar [root@backup test]# ll total 24 -rw------- 1 root root 1392 Apr 19 2019 anaconda-ks.cfg -rw-r--r-- 1 root root 20480 Apr 13 18:45 fo2.tar -rw-r--r-- 1 root root 0 Apr 13 18:22 foo2.txt [root@backup test]# rm foo2.txt anaconda-ks.cfg -f [root@backup test]# tar -mxf fo2.tar [root@backup test]# ll total 24 -rw------- 1 root root 1392 Apr 13 18:53 anaconda-ks.cfg -rw-r--r-- 1 root root 20480 Apr 13 18:45 fo2.tar -rw-r--r-- 1 root root 0 Apr 13 18:53 foo2.txt
可以看到加-m參數后解壓出來得文件mtime為當前時間。
