Linux压缩命令

.zip压缩与解压

.zip压缩

压缩文件

zip 压缩文件名 源文件

[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt
[root@localhost ~]# zip hello.zip hello.txt
  adding: hello.txt (stored 0%)
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt  hello.zip

压缩目录

zip -r 压缩文件名 源文件

[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt  hello.zip
[root@localhost ~]# zip -r folder_a.zip folder_a
  adding: folder_a/ (stored 0%)
  adding: folder_a/bbc.txt (stored 0%)
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  folder_a.zip  haha.txt  hello.txt  hello.zip

.zip解压

解压文件

unzip 压缩文件名

[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  folder_a.zip  haha.txt  hello.zip
[root@localhost ~]# unzip hello.zip 
Archive:  hello.zip
 extracting: hello.txt               
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  folder_a.zip  haha.txt  hello.txt  hello.zip
[root@localhost ~]# 

.gz压缩与解压

.gz压缩

压缩文件

gzip 源文件

压缩目录

gzip -r 源文件

注意:该方式压缩后,源文件/目录会消失

[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt
[root@localhost ~]# gzip hello.txt
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt.gz
[root@localhost ~]# gzip folder_a
gzip: folder_a is a directory -- ignored
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt.gz
[root@localhost ~]# gzip -r folder_a
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt.gz
[root@localhost ~]# 

.gz解压

gzip -d 压缩文件
gunzip 压缩文件

[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt.gz
[root@localhost ~]# gzip -d hello.txt.gz 
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt
[root@localhost ~]# gzip hello.txt 
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt.gz
[root@localhost ~]# gunzip hello.txt.gz 
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt
[root@localhost ~]# 

.bz2压缩与解压

.bz2压缩

注:.bz2只能压缩文件,不能压缩目录

压缩之后不保留源文件

bzip2 源文件

压缩后保留源文件

bzip2 -k 源文件

[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt
[root@localhost ~]# bzip2 hello.txt 
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt.bz2
[root@localhost ~]# bzip2 -k haha.txt 
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  haha.txt.bz2  hello.txt.bz2
[root@localhost ~]# 

.bz2解压

bzip -d 文件名
bunzip 文件名

[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  haha.txt.bz2  hello.txt.bz2
[root@localhost ~]# bzip2 -d haha.txt.bz2 
bzip2: Output file haha.txt already exists.
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  haha.txt.bz2  hello.txt.bz2
[root@localhost ~]# bunzip2 hello.txt.bz2 
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  haha.txt.bz2  hello.txt
[root@localhost ~]# 

.tar打包

打包,就是把几个文件打包成一起成为一个文件,经常用于对目录的打包。

tar -cvf 打包文件名 源文件

选项:

  • -c : 打包
  • -v : 显示过程
  • -f : 制定打包后的文件名
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt
[root@localhost ~]# tar -cvf folder_a.tar folder_a
folder_a/
folder_a/bbc.txt.gz
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  folder_a.tar  haha.txt  hello.txt
[root@localhost ~]# 

解打包

tar -xvf 文件名

[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a.tar  haha.txt  hello.txt
[root@localhost ~]# tar -xvf folder_a.tar 
folder_a/
folder_a/bbc.txt.gz
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  folder_a.tar  haha.txt  hello.txt
[root@localhost ~]# 

.tar.gz压缩格式

将文件先打包,再压缩成.gz格式

tar -zcvf 压缩包名.tar.gz 源文件

选项:

  • -z : 打包后压缩成.gz格式

解压缩

tar -zxvf 压缩包名

[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  haha.txt  hello.txt
[root@localhost ~]# tar -zcvf folder_a.tar.gz folder_a
folder_a/
folder_a/bbc.txt.gz
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  folder_a.tar.gz  haha.txt  hello.txt

[root@localhost ~]# rm -rf folder_a
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a.tar.gz  haha.txt  hello.txt

[root@localhost ~]# tar -zxvf folder_a.tar.gz 
folder_a/
folder_a/bbc.txt.gz
[root@localhost ~]# ls
abc.txt  anaconda-ks.cfg  folder_a  folder_a.tar.gz  haha.txt  hello.txt

.tar.bz2压缩格式

压缩:

tar -jcvf 压缩名.tar.bz2 源文件

解压缩:

tar -jxvf 压缩文件名

posted @ 2016-12-07 12:08  TabWeng  阅读(259)  评论(0)    收藏  举报