1.tar數組
tar命令格式 [-zjxcvfpP] filename tarbash
-z:表示同時用gzip壓縮。插件
-j:表示同時用bzip2壓縮。code
-J:表示同時用xz壓縮。ip
-x:表示解包或者解壓縮。ast
-t:表示查看tar包裏的文件。class
-c:表示創建一個tar包或者壓縮文件包。test
-v:表示可視化。可視化
-f:後面跟文件名(即-f filename,表示壓縮後的文件名爲filename,或者解壓文件filename。多個參數組合的狀況下,請把-f參數寫到最後。)打包
-P:表示使用原文件的屬性。(不經常使用)
-p:表示可使用絕對路徑。(不經常使用)
--exclude filename:表示在打包或壓縮時,不要將filename文件包括在內。(不經常使用)
[root@localhost ~]# cd test -bash: cd: test: 沒有那個文件或目錄 [root@localhost ~]# cd /tmp/8/test [root@localhost test]# xz -d 1.txt.xz xz: 1.txt.xz: 沒有那個文件或目錄 [root@localhost test]# ls 1.txt [root@localhost test]# xz -z 1.txt.xz xz: 1.txt.xz: 沒有那個文件或目錄 [root@localhost test]# xz -z 1.txt [root@localhost test]# ls 1.txt.xz [root@localhost test]# xz -d 1.txt.xz [root@localhost test]# mkdir test111 [root@localhost test]# touch test111/2.txt [root@localhost test]# echo "nihao" >!$ echo "nihao" >test111/2.txt [root@localhost test]# echo "nihao" >test111/2.txt [root@localhost test]# cp 1.txt test111/
[root@localhost test]# tree . . ├── 1.txt └── test111 ├── 1.txt └── 2.txt 1 directory, 3 files [root@localhost test]# tar -cvf test111.tar test111 test111/ test111/2.txt test111/1.txt [root@localhost test]# ls 1.txt test111 test111.tar
tar打包文件
[root@localhost test]# rm -f test111.tar [root@localhost test]# tar -cf test.tar test111 1.txt [root@localhost test]# ls 1.txt test111 test.tar
打包解包原文件不會刪除
[root@localhost test]# rm -rf test111 [root@localhost test]# ls 1.txt test.tar [root@localhost test]# tar -xvf test.tar test111/ test111/2.txt test111/1.txt 1.txt
--exclude選項:能夠排除文件,也能夠刪除文件。
[root@localhost test]# tar -cvf test111.tar --exclude 1.txt test111 test111/ test111/2.txt
[root@localhost test]# mkdir test111/test222 [root@localhost test]# tar -cvf test111.tar --exclude test222 test111 test111/ test111/2.txt test111/1.txt
zip命令
zip命令壓縮目錄和文件,壓縮目錄,須要指定目錄下的文件。
[root@localhost test]# zip 1.txt.zip 1.txt adding: 1.txt (deflated 64%) [root@localhost test]# zip test111.zip test111/* adding: test111/1.txt (deflated 64%) adding: test111/2.txt (stored 0%) adding: test111/test222/ (stored 0%)
zip後面先跟目標文件名,即壓縮後的自定義壓縮包名,而後跟要壓縮的文件或者目錄。安裝zip目錄命令:yum install -y zip
zip -r:一併壓縮二級目錄下的文件。
[root@localhost test]# zip -r test111.zip test111/ updating: test111/1.txt (deflated 64%) updating: test111/2.txt (stored 0%) updating: test111/test222/ (stored 0%) adding: test111/ (stored 0%)
unzip:解壓縮命令。安裝unzip命令:yum install -y unzip。
[root@localhost test]# unzip 1.txt.zip Archive: 1.txt.zip replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y inflating: 1.txt [root@localhost test]# yum install -y unzip 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.163.com * updates: mirrors.163.com 軟件包 unzip-6.0-19.el7.x86_64 已安裝而且是最新版本 無須任何處理
打包並壓縮
tar命令支持gzip壓縮、bzip2壓縮和xz壓縮。
壓縮成gzip格式文件
[root@localhost test]# tar -czvf test111.tar.gz test111 test111/ test111/2.txt test111/1.txt test111/test222/
使用-tf選項,能夠查看包或者壓縮包的文件列表。
[root@localhost test]# tar -tf test111.tar.gz test111/ test111/2.txt test111/1.txt test111/test222/ [root@localhost test]# tar -tf test.tar test111/ test111/2.txt test111/1.txt 1.txt
使用-zxvf選項,能夠解壓tar.gz格式的壓縮包。
[root@localhost test]# rm -rf test111 [root@localhost test]# ls 1.txt 1.txt.zip test111.tar test111.tar.gz test111.zip test.tar [root@localhost test]# tar -zxvf test111.tar.gz test111/ test111/2.txt test111/1.txt test111/test222/ [root@localhost test]# ls 1.txt 1.txt.zip test111 test111.tar test111.tar.gz test111.zip test.tar
打包的同時使用bzip2壓縮
使用-cjvf選項來壓縮:
[root@localhost test]# tar -cjvf test111.tar.bz2 test111 test111/ test111/2.txt test111/1.txt test111/test222/
使用-tf選項來查看壓縮包的文件列表:
[root@localhost test]# tar -tf test111.tar.bz2 test111/ test111/2.txt test111/1.txt test111/test222/
使用-jxvf t:
[root@localhost test]# tar -jxvf test111.tar.bz2 test111/ test111/2.txt test111/1.txt test111/test222/