zip壓縮工具,tar打包工具

zip壓縮工具

zip打包工具跟前面說到的gzip,bz2,xz 工具最大的不同是zip能夠壓縮目錄。若是沒有安裝,須要使用yum install -y zip 來安裝。安裝完以後就能夠直接使用了,跟以前提到的壓縮工具第二個不同的地方是使用格式:zip XX.zip filename 這裏的XX.zip是壓縮後的壓縮包名字。工具

[root@localhost tmp]# zip test.zip 1.txt
  adding: 1.txt (deflated 73%)
[root@localhost tmp]# du -h test.zip 
2.0M	test.zip
[root@localhost tmp]# ls
1.txt  1.txt.bz2  222  2.txt  3.txt  44.txt  form154853.pdf  test.zip

若是要使用zip壓縮目錄,則須要加-r的參數,應用以下code

[root@localhost tmp]# zip -r test3.zip 222
  adding: 222/ (stored 0%)
  adding: 222/1.txt.gz (deflated 0%)
  adding: 222/1.txt (stored 0%)
  adding: 222/11.txt (deflated 73%)
  adding: 222/333.txt (deflated 3%)
[root@localhost tmp]# du -sh test3.zip 
3.9M	test3.zip
[root@localhost tmp]# ls
1.txt  1.txt.bz2  222  2.txt  3.txt  44.txt  form154853.pdf  test1.zip  test3.zip  test.zip

使用zip還發現一個不同的地方就是默認它不會刪除掉原文件。orm

zip的解壓縮unzip

要解壓縮zip壓縮包須要使用unzip命令,默認沒有安裝,使用yum install -y unzip 命令安裝。這個命令是能夠指定目錄的-d參數,可是不能更改解開的文件的名字ip

[root@localhost tmp]# unzip test.zip -d test     指定目錄(就算是目錄不存在)
Archive:  test.zip
  inflating: test/1.txt              
[root@localhost tmp]# ls
1.txt  1.txt.bz2  222  2.txt  3.txt  44.txt  form154853.pdf  test  test1.zip  test3.zip  test.zip                              也會產生-d後面跟的目錄
[root@localhost tmp]# tree /tmp/test
/tmp/test
└── 1.txt

0 directories, 1 file

只用zip壓縮的包不能使用zcat ,zmore 等命令查看包裏文件的內容,可是能夠使用unzip -l 來查看包裏的文件列表form

[root@localhost tmp]# unzip -l test1.zip 
Archive:  test1.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  06-23-2018 01:18   222/
  2009225  06-23-2018 00:44   222/1.txt.gz
        0  06-23-2018 00:53   222/1.txt
  7487139  06-23-2018 00:54   222/11.txt
       32  06-23-2018 01:18   222/333.txt
---------                     -------
  9496396                     5 files

tar打包工具

tar打包工具能夠把多個目錄+多個文件打包成一個文件 用法 tar -cvf XXX.tar filename filename 這裏面的filename能夠是文件也能夠是目錄,若是使用tar -cf參數就什麼都不提示了,沒有了可視化的打包過程。pdf

[root@localhost tmp]# tar -cvf dabao.tar 1.txt 222 2.txt 1.txt.bz2 
1.txt
222/
222/1.txt.gz
222/1.txt
222/11.txt
222/333.txt
2.txt
1.txt.bz2
[root@localhost tmp]# tar -tf dabao.tar 
1.txt
222/
222/1.txt.gz
222/1.txt
222/11.txt
222/333.txt
2.txt
1.txt.bz2
[root@localhost tmp]# du -sh dabao.tar 
25M	dabao.tar

tar解包 tar -xvf

要解開tar打包的文件須要使用tar -xvf 命令test

[root@localhost mnt]# cp /tmp/dabao.tar /mnt 複製到該目錄下是爲了看起來比較清晰
[root@localhost mnt]# ls
dabao.tar
[root@localhost mnt]# tar -xvf dabao.tar  tar -xvf 解包
1.txt
222/
222/1.txt.gz
222/1.txt
222/11.txt
222/333.txt
2.txt
1.txt.bz2
[root@localhost mnt]# ls				解包後的結果
1.txt  1.txt.bz2  222  2.txt  dabao.tar

tar打包的時候能夠過濾掉指定的文件可視化

[root@localhost mnt]# ls
1.txt  1.txt.bz2  222  2.txt  dabao.tar
[root@localhost mnt]# 
[root@localhost mnt]# tar -cvf dabao1.tar --exclude "*.txt" 222 1.txt.bz2 過濾掉全部「.txt」結尾的文件
222/
222/1.txt.gz
1.txt.bz2

打包並壓縮

  • tar -zcvf 壓縮包名.gz 文件名 (打包並使用gzip工具壓縮)
  • tar -zxvf 壓縮包名.gz (解壓縮)
  • tar -jcvf 壓縮包名.bz2 文件名 (打包並使用bz2工具壓縮)
  • tar -jxvf 壓縮包名.bz2 (解壓縮)
  • tar -Jcvf 壓縮包名.xz 文件名 (打包並使用xz工具壓縮)
  • tar -Jxvf 壓縮包名.xz (解壓縮)
[root@localhost mnt]# tar -zcvf gz.gz 222 1.txt 打包並使用gzip壓縮
222/
222/1.txt.gz
222/1.txt
222/11.txt
222/333.txt
1.txt
[root@localhost mnt]# ls
1.txt  1.txt.bz2  222  2.txt  dabao1.tar  dabao.tar  gz.gz
[root@localhost mnt]# tar -jcvf gz.bz2 222 1.txt 打包並使用bz2壓縮
222/
222/1.txt.gz
222/1.txt
222/11.txt
222/333.txt
1.txt
[root@localhost mnt]# tar -Jcvf gz.xz 222 1.txt 打包並使用xz壓縮
222/
222/1.txt.gz
222/1.txt
222/11.txt
222/333.txt
1.txt

解壓縮

[root@localhost mnt]# tar -zxvf gz.gz 
222/
222/1.txt.gz
222/1.txt
222/11.txt
222/333.txt
1.txt
[root@localhost mnt]# tar -jxvf gz.bz2
222/
222/1.txt.gz
222/1.txt
222/11.txt
222/333.txt
1.txt
[root@localhost mnt]# tar -Jxvf gz.xz 
222/
222/1.txt.gz
222/1.txt
222/11.txt
222/333.txt
1.txt
相關文章
相關標籤/搜索