天天看點

Linux檔案壓縮指令

gzip 壓縮檔案:

gzip 選項[檔案]

[email protected]:~/test$ ls

a.hard  a.hard.bz2  a.soft  testfile

[email protected]:~/test$ gzip a.hard

[email protected]:~/test$ ls

a.hard.bz2  a.hard.gz  a.soft  testfile

​ 

壓縮後的檔案格式:.gz

gzip

1、隻能壓縮檔案,不能壓縮目錄;

[email protected]:~/test$ mkdir tartest

[email protected]:~/test$ ls

a.hard  a.hard.bz2  a.soft  tartest  testfile

[email protected]:~/test$ gzip tartest/

gzip: tartest/ is a directory -- ignored

2、不保留源檔案;

解壓縮:gunzip 或 gzip -d

[email protected]:~/test$ gunzip a.hard.gz 

[email protected]:~/test$ ls

a.hard  a.hard.bz2  a.soft  testfile

[email protected]:~/test$ 

(不能直接壓縮的目錄可以先用tar指令進行打包,打包完成後再進行壓縮。)

tar 打包目錄

tar 選項 [cvf] [目錄]

-c 産生.tar打封包件

-v 顯示詳細資訊

-f 指定壓縮後的檔案名

-z 打包同時壓縮

壓縮後的檔案格式:.tar.gz

[email protected]:~/test$ ls

a.hard  a.hard.bz2  a.soft  tartest  testfile

[email protected]:~/test$ tar -zcvf tartest.tar.gz tartest

tartest/

tartest/newtarfile

[email protected]:~/test$ ls

a.hard  a.hard.bz2  a.soft  tartest  tartest.tar.gz  testfile

tar 解壓縮:

-x 解包.tar檔案

-v 顯示詳細資訊

-f 指定解壓檔案

-z 解壓縮

例如:

[email protected]:~/test$ rm -r tartest

[email protected]:~/test$ ls

a.hard  a.hard.bz2  a.soft  tartest.tar.gz  testfile

[email protected]:~/test$ tar zxvf tartest.tar.gz 

tartest/

tartest/newtarfile

[email protected]:~/test$ ls

a.hard  a.hard.bz2  a.soft  tartest  tartest.tar.gz  testfile

[email protected]:~/test$ 

指令file 可以判斷檔案的類型

[email protected]:~/test$ file a.hard

a.hard: ASCII English text

[email protected]:~/test$ file a.soft 

a.soft: broken symbolic link to `a'

[email protected]:~/test$ 

zip 壓縮檔案或目錄:

zip 選項 [-r][壓縮後檔案名稱][檔案或目錄]

-r 壓縮目錄

壓縮後檔案格式:.zip (預設win和linux通用格式)

[email protected]:~/test$ ls

a.hard  a.hard.bz2  a.soft  tartest  tartest.tar.gz  testfile

[email protected]:~/test$ touch ziptestfile

[email protected]:~/test$ ls

a.hard  a.hard.bz2  a.soft  tartest  tartest.tar.gz  testfile  ziptestfile

[email protected]:~/test$ zip ziptestfile.zip ziptestfile 

  adding: ziptestfile (stored 0%)

[email protected]:~/test$ ls

a.hard      a.soft   tartest.tar.gz  ziptestfile

a.hard.bz2  tartest  testfile        ziptestfile.zip

[email protected]:~/test$ 

unzip 解壓.zip的壓縮檔案

unzip [壓縮檔案]

[email protected]:~/test$ unzip ziptestfile.zip 

Archive:  ziptestfile.zip

replace ziptestfile? [y]es, [n]o, [A]ll, [N]one, [r]ename: y

 extracting: ziptestfile             

[email protected]:~/test$ 

bzip2 壓縮檔案:

bzip2 選項[-k][檔案]

-k 産生壓縮檔案後保留原檔案

壓縮後檔案格式:.bz2

[email protected]:~/test$ bzip2 -k a.hard

[email protected]:~/test$ ls

a.hard  a.hard.bz2  a.soft  testfile

[email protected]:~/test$ 

解壓縮指令

bunzip2 選項[-k][壓縮檔案]

繼續閱讀