zip壓縮工具、tar打包 、打包並壓縮

zip壓縮工具

  1. zip 1.txt.zip 1.txt
  2. zip -r 123.zip 123/
  3. unzip 1.txt.zip
  4. unzip 123.zip -d ./123/
  5. unzip -l 123.zip
//安裝zip
[root@24centos7-01 test]# yum install -y zip
[root@24centos7-01 test]# yum install -y unzip

//壓縮
[root@24centos7-01 test]# zip 1.txt.zip 1.txt
  adding: 1.txt (deflated 69%)
[root@24centos7-01 test]# ls
1.txt  1.txt.zip  2.txt  test
[root@24centos7-01 test]# du -sh 1.txt.zip 
336K	1.txt.zip

//壓縮文件及目錄
[root@24centos7-01 test]# zip -r test.zip 2.txt test
  adding: 2.txt (deflated 69%)
  adding: test/ (stored 0%)
  adding: test/1.txt (deflated 69%)
  adding: test/2.txt (deflated 69%)
[root@24centos7-01 test]# ls
1.txt  1.txt.zip  2.txt  test  test.zip
[root@24centos7-01 test]# du -sh test.zip 
1004K	test.zip

//有同名文件或目錄時解壓
[root@24centos7-01 test]# unzip test.zip 
Archive:  test.zip
replace 2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A		//提示你是否覆蓋;是,否,所有,不,重命名
  inflating: 2.txt                   
  inflating: test/1.txt              
  inflating: test/2.txt  

//解壓到指定目錄
[root@24centos7-01 test]# unzip test.zip -d ./test/test
Archive:  test.zip
  inflating: ./test/test/2.txt       
   creating: ./test/test/test/
  inflating: ./test/test/test/1.txt  
  inflating: ./test/test/test/2.txt  

//查看壓縮包裏的文件列表
[root@24centos7-01 test]# unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
  1110943  11-09-2017 21:02   2.txt
        0  11-09-2017 22:16   test/
  1110943  11-09-2017 22:16   test/1.txt
  1110943  11-09-2017 22:16   test/2.txt
---------                     -------
  3332829                     4 files

tar打包

  1. tar -cvf 123.tar 123
  2. tar -cvf test.tar 1.txt 123
  3. tar -xvf test.tar
  4. tar -tv test.tar
  5. tar -cvf test.tar --exclude 1.txt --exclude 2 123
//將test目錄打包
[root@24centos7-01 test]# ls
1.txt  1.txt.zip  2.txt  test  test.zip
[root@24centos7-01 test]# tar -cvf test.tar test/
test/
test/1.txt
test/2.txt
test/test/
test/test/2.txt
test/test/test/
test/test/test/1.txt
test/test/test/2.txt
[root@24centos7-01 test]# ls
1.txt  1.txt.zip  2.txt  test  test.tar  test.zip

//將test目錄及其餘文件一塊兒打包
[root@24centos7-01 test]# tar -cvf test.tar test 1.txt 1.txt.zip
test/
test/1.txt
test/2.txt
test/test/
test/test/2.txt
test/test/test/
test/test/test/1.txt
test/test/test/2.txt
1.txt
1.txt.zip
[root@24centos7-01 test]# ls
1.txt  1.txt.zip  2.txt  test  test.tar  test.zip

//使用-t參數顯示tar包中的文件和目錄列表
[root@24centos7-01 test]# tar -tf test.tar
test/
test/1.txt
test/2.txt
test/test/
test/test/2.txt
test/test/test/
test/test/test/1.txt
test/test/test/2.txt
1.txt
1.txt.zip

//使用--exclude在打包的過程當中排除部分文件和目錄
[root@24centos7-01 test]# tar -cvf test.tar --exclude 1.txt test
test/
test/2.txt
test/test/
test/test/2.txt
test/test/test/
test/test/test/2.txt

//無--exclude參數
[root@24centos7-01 test]# tar -cvf test.tar  test
test/
test/1.txt
test/2.txt
test/test/
test/test/2.txt
test/test/test/
test/test/test/1.txt
test/test/test/2.txt

//多個--exclude狀況,不支持一個--exclude使用多參數
[root@24centos7-01 test]# tar -cvf test.tar --exclude 1.txt --exclude "*.txt" test
test/
test/test/
test/test/test/

打包並壓縮

tar -cvf建立tar包 -xvf解包 -tf查看列表 --exclude排除文件或目錄 -z表明gzip -j表明bz2 -J借用xz -C指定解壓的目標目錄 -v顯示檔案列表

  1. tar -zcvf test.tar.gz test
  2. tar -zxvf test.tar.gz
  3. tar -jcvf test.bz2 test
  4. tar -jxvf test.bz2
  5. tar -Jcvf test.xz test
  6. tar -Jxvf test.xz
  7. tar -tf test.bz2
  8. tar -tf test.gz
  9. tar -tf test.xz
//打包時使用gzip壓縮
[root@24centos7-01 test]# ls
1.txt  1.txt.zip  2.txt  test  test.zip
[root@24centos7-01 test]# tar -zcvf test.tar.gz test 1.txt 1.txt.zip test.zip 
test/
test/1.txt
test/2.txt
test/test/
test/test/2.txt
test/test/test/
test/test/test/1.txt
test/test/test/2.txt
1.txt
1.txt.zip
test.zip
[root@24centos7-01 test]# ls
1.txt  1.txt.zip  2.txt  test  test.tar.gz  test.zip
[root@24centos7-01 test]# du -sh test 1.txt 1.txt.zip test.zip
5.4M	test
1.1M	1.txt
336K	1.txt.zip
1004K	test.zip
[root@24centos7-01 test]# du -sh test.tar.gz
3.3M	test.tar.gz

//打包時使用bz2壓縮
[root@24centos7-01 test]# tar -jcvf test.tar.bz2 test 1.txt 1.txt.zip test.zip 
test/
test/1.txt
test/2.txt
test/test/
test/test/2.txt
test/test/test/
test/test/test/1.txt
test/test/test/2.txt
1.txt
1.txt.zip
test.zip
[root@24centos7-01 test]# ls
1.txt  1.txt.zip  2.txt  test  test.tar.bz2  test.zip
[root@24centos7-01 test]# du -sh test.tar.bz2 
2.8M	test.tar.bz2

//打包時使用xz壓縮
[root@24centos7-01 test]# tar -Jcvf test.tar.xz test 1.txt 1.txt.zip test.zip 
test/
test/1.txt
test/2.txt
test/test/
test/test/2.txt
test/test/test/
test/test/test/1.txt
test/test/test/2.txt
1.txt
1.txt.zip
test.zip
[root@24centos7-01 test]# du -sh test.tar.xz
612K	test.tar.xz

//查看文件列表,不區分壓縮格式
[root@24centos7-01 test]# tar -tf test.tar.bz2 
test/
test/1.txt
test/2.txt
test/test/
test/test/2.txt
test/test/test/
test/test/test/1.txt
test/test/test/2.txt
1.txt
1.txt.zip
test.zip
[root@24centos7-01 test]# tar -tf test.tar.xz 
test/
test/1.txt
test/2.txt
test/test/
test/test/2.txt
test/test/test/
test/test/test/1.txt
test/test/test/2.txt
1.txt
1.txt.zip
test.zip

//未壓縮tar包也支持-tf
[root@24centos7-01 test]# tar -tf test.tar
test/
test/1.txt
test/2.txt
test/test/
test/test/2.txt
test/test/test/
test/test/test/1.txt
test/test/test/2.txt

//指定解壓的目標目錄
[root@24centos7-01 test]# tar -zxvf test.tar.gz -C ../
test/
test/1.txt
test/2.txt
test/test/
test/test/2.txt
test/test/test/
test/test/test/1.txt
test/test/test/2.txt
相關文章
相關標籤/搜索