*gzip工具不能壓縮目錄,只能壓縮文件
壓縮:gzip filename

[root@localhost test01]# ll -h *   #查看壓縮前all.txt文件大小
-rw-r--r-- 1 root root 4.2M 9月   7 13:44 all.txt
[root@localhost test01]# gzip all.txt   #壓縮all.txt文件
[root@localhost test01]# ll -h *    #查看壓縮后all.txt文件大小
-rw-r--r-- 1 root root 1.2M 9月   7 13:44 all.txt.gz

解壓:gzip -d filename

[root@localhost test01]# ls
all.txt.gz
[root@localhost test01]# gzip -d all.txt.gz 
[root@localhost test01]# ls
all.txt

解壓:gunzip -d filename

[root@localhost test01]# ls
all.txt.gz
[root@localhost test01]# gunzip -d all.txt.gz 
[root@localhost test01]# ls
all.txt

指定壓縮率:gzip -n filename (n的范圍:1-9,壓縮等級(jí)9壓縮率最高,壓縮速度也就最慢,對(duì)cup資源的消耗也就相對(duì)過(guò)高,壓縮等級(jí)1壓縮率最低,壓縮速度也就最快,對(duì)cpu資源的消耗相對(duì)過(guò)低,默認(rèn)等級(jí)為6)

[root@localhost test01]# gzip -9 all.txt
[root@localhost test01]# file all.txt.gz  #file查看文件最后一列壓縮等級(jí)為最大壓縮率
all.txt.gz: gzip compressed data, was all.txt, from Unix, last modified: Sat Sep  7 13:44:13 2019, max compression

查看壓縮文件內(nèi)容:(在不解壓的情況下查看壓縮文件內(nèi)容使用zcat命令)

[root@localhost test01]# zcat all.txt.gz 

-c 參數(shù):在壓縮或解壓時(shí)保留源文件

[root@localhost test01]# gzip -c all.txt > all.txt.gz
[root@localhost test01]# ls
all.txt  all.txt.gz
[root@localhost test01]# gzip -d -c all.txt.gz > all2.txt
[root@localhost test01]# ls
all2.txt  all.txt  all.txt.gz

bzip2:
*與gzip類(lèi)似,不能壓縮目錄,只能壓縮文件,壓縮率比gzip高
安裝bzip2工具:

[root@localhost test01]# yum -y install bzip2

壓縮:bzip2 filename
解壓:bizp2 -d filename 或 bunzip2 -d filename
查看壓縮文件內(nèi)容:bzcat filename
*與gzip一樣可以指定壓縮率,但bzip2默認(rèn)壓縮等級(jí)為9,同樣可以使用-c參數(shù)

xz:
與gzip、bzip2類(lèi)似,不能壓縮目錄,只能壓縮文件,壓縮率比gzip、bzip2高*
壓縮:xz filename
解壓:xz -d filename 或 unxz -d filename
查看壓縮文件內(nèi)容:xzcat filename
與gzip、bzip一樣可以指定壓縮率,默認(rèn)壓縮等級(jí)最高,同樣可以使用-c參數(shù)
gzip、bzip2、xz在解壓時(shí)使用-c參數(shù)不僅可以保留源文件,還可以重命名解壓文件*

zip:
*zip可以壓縮目錄和文件,在解壓時(shí)可以指定解壓路徑,但不能重命名解壓內(nèi)容
安裝:

[root@localhost ~]# yum -y install zip

壓縮文件:zip 壓縮文件名 源文件名 (源文件可以是多個(gè)文件)

[root@localhost test01]# ls
filetest.txt  test02  test.sh
[root@localhost test01]# zip abc.zip filetest.txt test.sh 
  adding: filetest.txt (deflated 85%)
  adding: test.sh (deflated 79%)
[root@localhost test01]# ls  #將filetest.txt test.sh兩個(gè)文件添加到壓縮文件abc.zip
abc.zip  filetest.txt  test02  test.sh

壓縮目錄:zip -r 壓縮文件名 源文件名 (源文件可以是多個(gè)目錄和文件)

[root@localhost test01]# ls
abc.zip  filetest.txt  test02  test.sh
[root@localhost test01]# zip -r linuxtest.zip test02/ filetest.txt 
  adding: test02/ (stored 0%)
  adding: test02/all.txt (deflated 71%)
  adding: filetest.txt (deflated 85%)
[root@localhost test01]# ls
abc.zip  filetest.txt  linuxtest.zip  test02  test.sh

*zip壓縮或解壓文件或目錄后,會(huì)自動(dòng)保留源文件

解壓:unzip filename

[root@localhost test01]# ls
abc.zip  filetest.txt  linuxtest.zip  test02  test.sh
[root@localhost test01]# rm -rf filetest.txt test.sh 
[root@localhost test01]# ls
abc.zip  linuxtest.zip  test02
[root@localhost test01]# unzip abc.zip 
Archive:  abc.zip
  inflating: filetest.txt            
  inflating: test.sh                 
[root@localhost test01]# ls
abc.zip  filetest.txt  linuxtest.zip  test02  test.sh

將壓縮文件中的內(nèi)容解壓到指定目錄: unzip filename -d 目標(biāo)目錄路徑

[root@localhost test01]# ls
abc.zip  filetest.txt  linuxtest.zip  test02  test.sh
[root@localhost test01]# unzip linuxtest.zip -d /root/mytest/
Archive:  linuxtest.zip
   creating: /root/mytest/test02/
  inflating: /root/mytest/test02/all.txt  
  inflating: /root/mytest/filetest.txt  
[root@localhost test01]# ls /root/mytest/
filetest.txt  test02

查看壓縮文件中的文件列表: unzip -l filename
*與gzip、bzip2、xz不同,unzip只能查看文件列表,不能查看文件中的內(nèi)容

[root@localhost test01]# unzip -l linuxtest.zip 
Archive:  linuxtest.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  09-07-2019 15:18   test02/
  4340076  09-07-2019 13:44   test02/all.txt
     2943  09-07-2019 15:26   filetest.txt
---------                     -------
  4343019                     3 files

tar:
*tar工具將多個(gè)文件或目錄打包到一個(gè)文件中(比如要壓縮一個(gè)目錄,里面有很多小文件,可以使用tar將該目錄先打包成一個(gè)文件再壓縮),增加傳輸速度,對(duì)文件大小改變不會(huì)太大,tar打包時(shí)可以同時(shí)打包多個(gè)目錄加文件

打包:tar -cvf 打包文件名 源文件

[root@localhost test01]# ls
test02  test.sh
[root@localhost test01]# tar -cvf testfile.tar test02/ test.sh 
test02/
test02/all.txt
test02/filetest.txt
test.sh
[root@localhost test01]# ls  #將目錄/test02和文件test.sh都打包為testfile.tar文件
test02  testfile.tar  test.sh

解包:tar -xvf 目標(biāo)文件

[root@localhost test01]# ls
test02  testfile.tar  test.sh
[root@localhost test01]# rm -rf test02 test.sh 
[root@localhost test01]# ls
testfile.tar
[root@localhost test01]# tar -xvf testfile.tar 
test02/
test02/all.txt
test02/filetest.txt
test.sh
[root@localhost test01]# ls
test02  testfile.tar  test.sh

查看tar文件的文件列表:tar -tf 目標(biāo)文件

[root@localhost test01]# tar -tf testfile.tar 
test02/
test02/all.txt
test02/filetest.txt
test.sh

打包時(shí)過(guò)濾指定文件:- -exclude
過(guò)濾指定文件:

[root@localhost test01]# ls test02/
all.txt  filetest.txt  test.sh
[root@localhost test01]# tar -cvf testfile.tar --exclude filetest.txt test02/ 
test02/
test02/all.txt
test02/test.sh
[root@localhost test01]# ls
test02  testfile.tar
[root@localhost test01]# tar -tf testfile.tar 
test02/
test02/all.txt
test02/test.sh

過(guò)濾指定類(lèi)型的文件:

[root@localhost test01]# tar -cvf testfile.tar --exclude *.txt test02/ 
test02/
test02/test.sh
[root@localhost test01]# ls
test02  testfile.tar
[root@localhost test01]# tar -tf testfile.tar 
test02/
test02/test.sh

可以使用多個(gè)- -exclude:

[root@localhost test01]# tar -cvf testfile.tar --exclude filetest.txt --exclude test.sh test02/ 
test02/
test02/all.txt
[root@localhost test01]# ls
test02  testfile.tar
[root@localhost test01]# tar -tf testfile.tar 
test02/
test02/all.txt

tar在打包的同時(shí)支持壓縮:

1.打包的同時(shí)壓縮成gzip包:-zcvf

[root@localhost test01]# du -sh test02/
4.2M    test02/
[root@localhost test01]# tar -zcvf testfile.tar.gz test02/
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh
[root@localhost test01]# du -sh testfile.tar.gz 
1.2M    testfile.tar.gz

解壓tar.gz包:-zxvf

[root@localhost test01]# tar -zxvf testfile.tar.gz 
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh

2.打包的同時(shí)壓縮成bzip2包: -jcvf

[root@localhost test01]# tar -jcvf testfile.tar.bz2 test02/
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh
[root@localhost test01]# du -sh testfile.tar.bz2 
1.2M    testfile.tar.bz2

解壓tar.bz2包: -jxvf

[root@localhost test01]# tar -jxvf testfile.tar.bz2 
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh

3.打包的同時(shí)壓縮成xz包: -Jcvf

[root@localhost test01]# tar -Jcvf testfile.tar.xz test02/
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh
[root@localhost test01]# du -sh testfile.tar.xz 
252K    testfile.tar.xz

解壓tar.xz包: -Jxvf

[root@localhost test01]# tar -Jxvf testfile.tar.xz 
test02/
test02/all.txt
test02/filetest.txt
test02/test.sh

更多關(guān)于云服務(wù)器域名注冊(cè),虛擬主機(jī)的問(wèn)題,請(qǐng)?jiān)L問(wèn)三五互聯(lián)官網(wǎng):www.shinetop.cn

贊(0)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡(luò)內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。郵箱:3140448839@qq.com。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明出處:三五互聯(lián)知識(shí)庫(kù) » linux壓縮和打包工具gzip_bzip2_xz_zip_tar

登錄

找回密碼

注冊(cè)