Linux壓縮命令(總結(jié))

Linux壓縮命令(總結(jié))

linux壓縮命令

linux常見的壓縮格式有.zip、.gz、.bz2、.tar、.tar.gz、.tar.bz2;常用的壓縮命令有zip、tar。這里列舉了各壓縮命令的使用示例。更多的用法請(qǐng)使用命令 –help查閱。

推薦:《Linux教程

zip

格式:

zip?[options]?目標(biāo)壓縮包名稱?待壓縮源文件 unzip?[-Z]?[options]?待壓縮源文件?[list]?[-x?xlist]?[-d?exdir]

常用命令:

#?壓縮文件 zip?myfile.zip?myfile #?壓縮文件夾(包含子目錄) zip?-r?mydir.zip?mydir #?壓縮當(dāng)前目錄所有文件 zip?mydir.zip?* #?解壓文件 unzip?mydir.zip

zip更多參數(shù):

-v?顯示操作詳細(xì)信息 -d?從壓縮包里刪除文件 -m?將文件剪切到壓縮包里,源文件將被刪除 -r?遞歸壓縮 -x?排除文件 -c?加一行備注 -z?加備注 -T?測(cè)試壓縮包完整性 -e?加密 -q?安靜模式 -1,?--fast?更快的壓縮速度 -9,?--best?更好的壓縮率 --help?查看幫助 -h2?查看更多幫助

unzip更多參數(shù):

-v?顯示操作詳細(xì)信息 -l?查看壓縮包內(nèi)容 -d?解壓到指定文件夾 -x?排除壓縮包內(nèi)文件 -t?測(cè)試壓縮包文件內(nèi)容 -z?查看備注 -o?覆蓋文件無(wú)需提示 -q?安靜模式 --help?查看幫助

示例:

$?ls t.md??t.php?t.php.zip
#?創(chuàng)建壓縮包 $?zip?-v?myfile.zip?t.* ??adding:?t.md??(in=8121)?(out=1051)?(deflated?87%) ??adding:?t.php?(in=740)?(out=319)?(deflated?57%) ??adding:?t.php.zip?????(in=1666)?(out=1666)?(stored?0%) total?bytes=10527,?compressed=3036?->?71%?savings #?測(cè)試壓縮包完整性 $?zip?-T?myfile.zip? test?of?myfile.zip?OK #?測(cè)試壓縮包文件內(nèi)容 $?unzip?-t?myfile.zip? Archive:??myfile.zip ????testing:?t.md?????????????????????OK ????testing:?t.php????????????????????OK ????testing:?t.php.zip????????????????OK No?errors?detected?in?compressed?data?of?myfile.zip. #?查看壓縮包里內(nèi)容 $?unzip?-l?myfile.zip? Archive:??myfile.zip ??Length??????Date????Time????Name ---------??----------?-----???---- ?????8121??06-08-2016?17:03???t.md ??????740??06-08-2016?17:02???t.php ?????1666??07-30-2016?17:38???t.php.zip ---------?????????????????????------- ????10527?????????????????????3?files #?從壓縮包里刪除文件t.php.zip??? $?zip?-d?myfile.zip?t.php.zip deleting:?t.php.zip #?從壓縮包里刪除文件t.php $?zip?-d?myfile.zip?t.php deleting:?t.php #?添加文件到壓縮包里 $?zip?-u?myfile.zip?t.php ??adding:?t.php?(deflated?57%) #?給壓縮包添加注釋?? $?zip?-z?myfile.zip enter?new?zip?file?comment?(end?with?.): test . #?查看壓縮包注釋 $?unzip?-z?myfile.zip? Archive:??myfile.zip test #?解壓到指定文件夾 $?unzip?myfile.zip?-d?my Archive:??myfile.zip test?. ??inflating:?my/t.md????????????????? ??inflating:?my/t.php #?排除文件不解壓 $?unzip?myfile.zip??-x?t.php?-d?my Archive:??myfile.zip test?. ??inflating:?my/t.md

gz

格式:

gzip?[options]?待壓縮源文件 gunzip?[options]??待解壓文件

不用寫最終的壓縮文件名,會(huì)自動(dòng)在后面加.gz后綴,同時(shí)刪除源文件。

常用命令:

#?壓縮1.log,同時(shí)會(huì)自動(dòng)刪除源文件 gzip?1.log #?解壓1.log.gz,同時(shí)會(huì)自動(dòng)刪除壓縮包 gzip?-d?1.log.gz #?壓縮1.log,保留源文件 gzip?-k?1.log #?解壓1.log.gz,保留壓縮包 gzip?-dk?1.log.gz #?查看壓縮包信息 gzip?-l?1.log.gz #?遞歸的對(duì)目錄里的每個(gè)文件單獨(dú)壓縮 gzip?-r?mydir

注意:gunzip與gzip -d等效,都可以解壓gz文件。

更多參數(shù):

-c,?--stdout?將壓縮后的內(nèi)容在標(biāo)準(zhǔn)輸出顯示出來(lái),保留原文件 -1,?--fast?更快的壓縮速度 -9,?--best?更好的壓縮率

示例:

#?壓縮1.log為1.log.gz,保留源文件 gzip?-c?1.log?>?1.log.gz

bz2

格式:

bzip2?[options]?待壓縮源文件 bunzip2?[options]??待解壓文件

常用命令:

#?壓縮1.log bzip2?1.log bzip2?-k?1.log #?解壓1.log.bz2 bzip2?-d?1.log.bz2 bzip2?-dk?1.log.bz2 bunzip2?1.log.bz2 bunzip2?-k?1.log.bz2

更多參數(shù):

-c,?--stdout?將壓縮后的內(nèi)容在標(biāo)準(zhǔn)輸出顯示出來(lái),保留原文件 -1,?--fast?更快的壓縮速度 -9,?--best?更好的壓縮率

tar

格式:

tar?[options]?目標(biāo)壓縮包名稱?待壓縮源文件

常用命令:

#?打包后,以gzip?壓縮 tar?zcvf?test.tar.gz?/test??#壓縮/test為test.tar.gz #?解壓test.tar.gz tar?zxvf?test.tar.gz? #?打包后,以bzip2?壓縮 tar?jcvf?test.tar.bz2?/test??#壓縮/test為test.tar.bz2 #?解壓test.tar.bz2 tar?jxvf?test.tar.bz2 #?僅打包,不壓縮 tar?cvf?test.tar?/test??#壓縮/test為test.tar #?解壓test.tar tar?xvf?test.tar #?查看壓縮包內(nèi)容列表 tar?tvf?test.tar.gz #?解壓到指定文件夾(目標(biāo)文件夾必須存在) $?tar?-zxvf?all.tar.gz?-C?my/ #?壓縮時(shí)排除某些目錄 $?tar?-zcvf?tomcat.tar.gz?--exclude=tomcat/logs?tomcat $?tar?-zcvf?tomcat.tar.gz?--exclude=tomcat/logs?--exclude=tomcat/libs?--exclude=tomcat/xiaoshan.txt?tomcat

常用參數(shù)說(shuō)明:

-c,?--create:?建立壓縮檔案 -x,?--extract,?--get:解壓 -t,?--list:查看內(nèi)容 -r,?--append:向壓縮歸檔文件末尾追加文件 -u,?--update:更新原壓縮包中的文件 -d,?--diff,?--compare?將壓縮包里的文件與文件系統(tǒng)進(jìn)行對(duì)比 ????--delete?從壓縮包里刪除

這幾個(gè)是獨(dú)立的命令,壓縮解壓都要用到其中一個(gè),可以和別的命令連用但只能用其中一個(gè)。下面的參數(shù)是根據(jù)需要在壓縮或解壓檔案時(shí)可選的:

-z,?--gzip,?--gunzip,?--ungzip:有g(shù)zip屬性的 -j,?--bzip2:有bz2屬性的 -Z,?--compress,?--uncompress:有compress屬性的 -v,?--verbose:顯示所有過程 -O,?--to-stdout:將文件解開到標(biāo)準(zhǔn)輸出 -C,?--directory=DIR:解壓到指定文件夾

最后的參數(shù)-f是必須的:

-f,?--file=ARCHIVE:?使用檔案名字,切記,這個(gè)參數(shù)是最后一個(gè)參數(shù),后面只能接檔案名。

查看命令幫助:

tar?--help tar?-? tar?--usage

更多示例:

#?比較壓縮包里文件與源文件變動(dòng) $?tar?--diff?-vf?1.log.tar?1.log 1.log 1.log:?Mod?time?differs 1.log:?Size?differs #?刪除壓縮包里的1.log $?tar?--delete?-vf?1.log.tar?1.log #?向壓縮歸檔文件里追加文件 $?tar?rvf?1.log.tar?1.log?2.log 1.log 2.log #?向壓縮歸檔文件里更新文件 $?tar?uvf?1.log.tar?1.log?2.log

說(shuō)明:不能向tar.gz和tar.bz2里追加或者更新文件:

$?tar?zrvf?all.tar.gz?3.log tar:?Cannot?update?compressed?archives Try?'tar?--help'?or?'tar?--usage'?for?more?information.

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊14 分享