使用 tar 命令歸檔的包通常稱為 tar 包(tar 包文件都是以“.tar”結尾的)。
[root@localhost ~]#tar [選項] 源文件或目錄
此命令常用的選項及各自的含義如表 1 所示。| 選項 | 含義 |
|---|---|
| -c | 將多個文件或目錄進行打包。 |
| -A | 追加 tar 文件到歸檔文件。 |
| -f 包名 | 指定包的文件名。包的擴展名是用來給管理員識別格式的,所以一定要正確指定擴展名; |
| -v | 顯示打包文件過程; |
[root@localhost ~]# tar -cvf anaconda-ks.cfg.tar anaconda-ks.cfg
#把anacondehks.cfg打包為 anacondehks.cfg.tar文件
[root@localhost ~]# ll -d test/
drwxr-xr-x 2 root root 4096 6月 17 21:09 test/
#test是我們之前的測試目錄
[root@localhost ~]# tar -cvf test.tar test/
test/
test/test3
test/test2
test/test1
#把目錄打包為test.tar文件
tar命令也可以打包多個文件或目錄,只要用空格分開即可。例如:
[root@localhost ~]# tar -cvf ana.tar anaconda-ks.cfg /tmp/
#把anaconda-ks.cfg文件和/tmp目錄打包成ana.tar文件包
[root@localhost ~]#ll -d test test.tar
drwxr-xr-x 2 root root 4096 6月 17 21:09 test
-rw-r--r-- 1 root root 10240 6月 18 01:06 test.tar
#我們之前已經把test目錄打包成test.tar文件
[root@localhost ~]# gzip test.tar
[root@localhost ~]# ll test.tar.gz
-rw-r--r-- 1 root root 176 6月 18 01:06 test.tar.gz
#gzip命令會把test.tar壓縮成test.tar.gz
[root@localhost ~]#tar [選項] 壓縮包
| 選項 | 含義 |
|---|---|
| -x | 對 tar 包做解打包操作。 |
| -f | 指定要解壓的 tar 包的包名。 |
| -t | 只查看 tar 包中有哪些文件或目錄,不對 tar 包做解打包操作。 |
| -C 目錄 | 指定解打包位置。 |
| -v | 顯示解打包的具體過程。 |
[root@localhost ~]# tar -xvf anaconda-ks.cfg. tar
#解打包到當前目錄下
[root@localhost ~]# tar -xvf test.tar -C /tmp
#把文件包test.tar解打包到/tmp/目錄下
[root@localhost ~]# tar -tvf test.tar
drwxr-xr-x root/root 0 2016-06-17 21:09 test/
-rw-r-r- root/root 0 2016-06-17 17:51 test/test3
-rw-r-r- root/root 0 2016-06-17 17:51 test/test2
-rw-r-r- root/root 0 2016-06-17 17:51 test/test1
#會用長格式顯示test.tar文件包中文件的詳細信息
[root@localhost ~]#tar [選項] 壓縮包 源文件或目錄
此處常用的選項有以下 2 個,分別是:[root@localhost ~]# tar -zcvf tmp.tar.gz /tmp/
#把/temp/目錄直接打包壓縮為".tar.gz"格式,通過"-z"來識別格式,"-cvf"和打包選項一致
[root@localhost ~]# tar -zxvf tmp.tar.gz
#解壓縮與解打包".tar.gz"格式
[root@localhost ~]# tar -jcvf tmp.tar.bz2 /tmp/
#打包壓縮為".tar.bz2"格式,注意壓縮包文件名
[root@localhost ~]# tar -jxvf tmp.tar.bz2
#解壓縮與解打包".tar.bz2"格式
新聞熱點
疑難解答