6.5 zip壓縮工具 6.6 tar打包 6.7 打包並壓縮

zip壓縮工具

zip命令能夠壓縮目錄和文件,-r 壓縮目錄。linux


zip使用方法

  • zip 1.txt.zip 1.txt //壓縮文件
  • zip -r 123.zip 123/ //壓縮目錄
  • unzip 1.txt.zip //解壓
  • unzip 123.zip -d /root/456/ //解壓文件,並指定解壓到那個目錄下
  • 不能查看壓縮文件的內容,只能查看內容列表
  • unzip -l 123.zip //查看壓縮文件的內容列表
  • zip壓縮文件後,源文件不消失
  • unzip解壓文件後,會直接覆蓋原文件和目錄,不會提示覆蓋信息

  1. 須要安裝zip包
[root@linux-128 d6z]# zip 1.txt.zip 1.txt
-bash: zip: 未找到命令
[root@linux-128 d6z]# yum install -y zip
  1. 使用zip工具壓縮文件1.txt文件。
[root@linux-128 d6z]# zip 1.txt.zip 1.txt
  adding: 1.txt (deflated 74%)
[root@linux-128 d6z]# ls
1.txt  1.txt.zip  2.txt  3.txt  4.txt  test
[root@linux-128 d6z]# du -sh 1.txt.zip
664K	1.txt.zip
  1. 使用zip工具壓縮目錄test
[root@linux-128 d6z]# zip -r test.zip test
  adding: test/ (stored 0%)
  adding: test/3.txt (deflated 74%)
  adding: test/4.txt (deflated 74%)
[root@linux-128 d6z]# ls
1.txt  1.txt.zip  2.txt  3.txt  4.txt  test  test.zip
[root@linux-128 d6z]# du -sh test.zip
1.3M	test.zip
[root@linux-128 d6z]# du -sh test
5.0M	test
  1. 解壓壓縮文件
[root@linux-128 d6z]# unzip 1.txt.zip
-bash: unzip: 未找到命令
[root@linux-128 d6z]# yum install -y unzip
[root@linux-128 d6z]# unzip 1.txt.zip
Archive:  1.txt.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A   //A表示所有
  inflating: 1.txt
[root@linux-128 d6z]# ls
1.txt  1.txt.zip  2.txt  3.txt  4.txt  test  test.zip
[root@linux-128 d6z]# unzip test.zip
Archive:  test.zip
replace test/3.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
  inflating: test/3.txt
  inflating: test/4.txt
  1. 指定解壓文件路徑
[root@linux-128 d6z]# unzip  test.zip  -d  /tmp/test
Archive:  test.zip
   creating: /tmp/test/test/
  inflating: /tmp/test/test/3.txt
  inflating: /tmp/test/test/4.txt
[root@linux-128 d6z]# ls /tmp/test
333  test  test1  test2
  1. 查看壓縮文件內容列表
[root@linux-128 d6z]# unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  11-10-2017 22:44   test/
  2572100  11-10-2017 22:43   test/3.txt
  2572100  11-10-2017 22:44   test/4.txt

tar打包

  1. tar自己是一個大包工具,能夠把目錄打包成一個文件,它把全部的文件整合成一個大文件,方便複製或者移動。
  2. 命令格式:tar [-zjxcvfpP] filename.tar

tar參數

  • -z表示同時使用gzip壓縮
  • -j表示同時用bzip壓縮
  • -J表示同時用xz壓縮
  • -c表示創建一個tar包或者壓縮文件包
  • -x表示解包或者解壓
  • -v表示可視化
  • -f後面跟文件名(-f filename,表示壓縮後的文件名爲filename)注意:若是多個參數組合的狀況下,-f要寫在最後面。
  • -t表示查看tar包裏的文件
  • --exclude filename 表示在打包或壓縮時,不要將某個文件不包含在裏面。
  • 打包或者解包,源文件都存在。

tar使用方法:

  1. tar -cvf 123.tar 123 // 打包目錄123
  2. tar -cvf aming.tar 1.txt 123 //打包目錄123和文件1.txt
  3. tar -xvf aming.tar //解包
  4. tar -tf aming.tar //查看打包文件
  5. tar -cvf aming.tar --exclude 1.txt --exclude 2 123 //打包目錄123,單不包括文件1.txt和2

  1. 打包目錄test和文件1.txt,2.txt
[root@linux-128 d6z]# tar -cvf test.tar test 1.txt 2.txt
test/
test/3.txt
test/4.txt
1.txt
2.txt
[root@linux-128 d6z]# ls
1.txt  1.txt.zip  2.txt  3.txt  4.txt  test  test.tar  test.zip
  1. 查看打包文件test.tar的內容
[root@linux-128 d6z]# tar -tf test.tar
test/
test/3.txt
test/4.txt
1.txt
2.txt
  1. 解包文件test.tar
[root@linux-128 d6z]# tar -xvf test.tar
test/
test/3.txt
test/4.txt
1.txt
2.txt
  1. 打包目錄test和文件1.txt 2.txt ,可是不包含文件3.txt
[root@linux-128 d6z]# tar -cvf test.tar --exclude 3.txt  test 1.txt 2.txt
test/
test/4.txt
1.txt
2.txt
  1. 打包目錄test和文件1.txt 2.txt ,可是不包含文件3.txt和4.txt
[root@linux-128 d6z]# tar -cvf test.tar --exclude 3.txt --exclude  test 1.txt 2.txt
1.txt
2.txt

打包並壓縮

  • tar 命令還能夠在打包的同時支持gzip壓縮,bzip壓縮和xz壓縮

打包並壓縮的使用方法:

  • tar -zcvf 123.tar.gz 123
  • tar -zxvf 123.tar.gz
  • tar -jcvf 123.bz2 123
  • tar -jxvf 123.bz2
  • tar -Jcvf 123.xz 123
  • tar -Jxvf 123.xz
  • tar -tf 123.bz2 / tar -tf 123.gz / tar -tf 123.xz

  1. 打包文件並使用gzip壓縮
[root@linux-128 d6z]# tar -zcvf test.tar.gz test 1.txt 2.txt
test/
test/3.txt
test/4.txt
1.txt
2.txt
[root@linux-128 d6z]# ls
1.txt  1.txt.zip  2.txt  3.txt  4.txt  test  test.tar  test.tar.gz  test.zip
  1. 查看打包文件
[root@linux-128 d6z]# tar -tf test.tar.gz
test/
test/3.txt
test/4.txt
1.txt
2.txt
解包
[root@linux-128 d6z]# tar -zxvf test.tar.gz
test/
test/3.txt
test/4.txt
1.txt
2.txt
  1. 打包文件並使用bzip2壓縮
[root@linux-128 d6z]# tar -jcvf test.tar.bz2 test 1.txt 2.txt
test/
test/3.txt
test/4.txt
1.txt
2.txt
解包
[root@linux-128 d6z]# tar -jxvf test.tar.bz2
test/
test/3.txt
test/4.txt
1.txt
2.txt
  1. 打包文件並使用xz壓縮
[root@linux-128 d6z]# tar -Jcvf test.tar.xz test 1.txt 2.txt
test/
test/3.txt
test/4.txt
1.txt
2.txt
解包
[root@linux-128 d6z]# tar -Jxvf test.tar.xz
test/
test/3.txt
test/4.txt
1.txt
2.txt
  1. 查看打包文件
[root@linux-128 d6z]# tar tf test.tar.xz
test/
test/3.txt
test/4.txt
1.txt
2.txt

[root@linux-128 d6z]# du -sh test.tar.gz test.tar.bz2 test.tar.xz
2.6M	test.tar.gz
988K	test.tar.bz2
64K	test.tar.xz

注意:打包後文件越小,耗時越長,佔用cpu資源越多。數組

相關文章
相關標籤/搜索