Linux常用命令(压缩命令)

来源:互联网 发布:唐筛标准年龄风险数据 编辑:程序博客网 时间:2024/05/24 08:33

一、常用压缩格式

  1. 常用压缩格式有:.zip .gz .bz2
  2. 常用压缩格式: .tar.gz .tar.bz2

二、按压缩格式来认识压缩命令

2.1 .zip格式

  • 压缩文件命令:

    zip 压缩文件名 源文件

  • 压缩目录命令:

    zip -r 压缩文件名 源目录

2.2 问题一

zip: command not found

因为没有安装zip,所以需要下载并安装zip

yum install zip

2.3 .zip格式解压缩

unzip [压缩文件]
解压缩.zip文件

举例:

[root@localhost zixuan]# ls -lh总用量 16Kdrwxr-xr-x. 2 root root 4.0K 522 10:32 laoshi-rw-r--r--. 1 root root  612 520 17:00 laoshi.zipdrwxr-xr-x. 2 root root 4.0K 518 16:09 learndrwxr-xr-x. 2 root root 4.0K 517 11:41 zm[root@localhost zixuan]# rm -rf laoshi[root@localhost zixuan]# lslaoshi.zip  learn  zm[root@localhost zixuan]# unzip laoshi.zipArchive:  laoshi.zip   creating: laoshi/ extracting: laoshi/one               extracting: laoshi/three             extracting: laoshi/two              [root@localhost zixuan]# ls -lh总用量 16Kdrwxr-xr-x. 2 root root 4.0K 520 16:59 laoshi-rw-r--r--. 1 root root  612 520 17:00 laoshi.zipdrwxr-xr-x. 2 root root 4.0K 518 16:09 learndrwxr-xr-x. 2 root root 4.0K 517 11:41 zm

2.4 .gz格式压缩

  1. 压缩为.gz格式的压缩文件,与源文件名一样,但是源文件会消失;

    .gzip [源文件]


其中one,two,three都是文件,举例:
[root@localhost laoshi]# lsone  three  two[root@localhost laoshi]# gzip one[root@localhost laoshi]# lsone.gz  three  two
  1. 压缩为.gz格式的压缩文件,并且保留源文件

    gzip -c laoshi > laoshi.gz


举例:
[root@localhost laoshi]# lsone.gz  three  two[root@localhost laoshi]# gzip -c two > two.gz[root@localhost laoshi]# lsone.gz  three  two  two.gz
  1. 压缩目录下所有的子文件,但是不能压缩目录,这样很不方便。

    gzip -r [目录]


举例:
gzip -r laoshi ,会压缩laoshi目录下面的所有子文件
[root@localhost zixuan]# ls laoshione.gz  three  two  two.gz[root@localhost zixuan]# gzip -r laoshigzip: laoshi/two.gz already exists; do you wish to overwrite (y or n)? y[root@localhost zixuan]# ls laoshi  learn  zm[root@localhost zixuan]# ls laoshione.gz  three.gz  two.gz

2.5 .gz格式解压缩

  1. 解压缩文件

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

  2. 解压缩目录,即解压缩目录下的所有.gz压缩文件

    gunzip -r [目录文件]

举例:

[root@localhost zixuan]# cd zm[root@localhost zm]# gunzip abc.gz[root@localhost zm]# ls abc  first  second[root@localhost zm]# cd ..[root@localhost zixuan]# gunzip laoshigzip: laoshi is a directory -- ignored[root@localhost zixuan]# gunzip -r laoshi[root@localhost zixuan]# ls -l laoshi总用量 0-rw-r--r--. 1 root root 0 520 16:59 one-rw-r--r--. 1 root root 0 520 16:59 three-rw-r--r--. 1 root root 0 520 16:59 two

2.6 .bz2格式压缩

bzip2命令还能压缩目录,只能压缩文件

1、压缩为.bz2格式,并且不保留源文件

bzip [源文件]

2、压缩之后保留源文件

bzip2 -k [源文件]

2.7 .bz2格式解压缩


  1. 解压缩文件,-k保留压缩文件

bzip2 -d [压缩文件]
bunzip2 [压缩文件]
举例:

[root@localhost zixuan]# ls laoshione  three  two[root@localhost zixuan]# cd laoshi[root@localhost laoshi]# bzip2 one[root@localhost laoshi]# lsone.bz2  three  two[root@localhost laoshi]# bzip2 -k two[root@localhost laoshi]# lsone.bz2  three  two  two.bz2[root@localhost laoshi]# bzip2 -dk one.bz2[root@localhost laoshi]# lsone  one.bz2  three  two  two.bz2[root@localhost laoshi]# rm tworm:是否删除普通空文件 "two"?y[root@localhost laoshi]# lsone  one.bz2  three  two.bz2[root@localhost laoshi]# bunzip2 two.bz2[root@localhost laoshi]# lsone  one.bz2  three  two

2.8 .tar.gz格式与.tar.bz2格式

前面提到的命令gzip与bzip2只能对文件或者子文件进行单独压缩,并不能对整个目录进行压缩,不能打包成一个压缩文件。这样对压缩工作来说,很不方便,那么有没有可以解决这个问题的命令呢?答案当然是有的。在这之前我们先来学习一个打包命令tar:

1、打包命令tar

这种将多个文件或目录打包成一个大文件的命令功能,我们称呼它为“打包命令”。

tar -cvf [打包文件名] [源文件]

tar命令中的选项有:

选项 含义 -c 新建打包文件,可搭配-v查看过程中被打包的文件名 -x 解打包或解压缩功能,可以搭配-C(大写C)指定解压目录 -t 查看打包文件的内容,查看有哪些文件 -z 通过gzip的支持进行压缩/解压缩,此时文件名最好为“*.tar.gz” -j 通过bzip2的支持进行压缩/解压缩,此时文件名最好为“*.tar.bz2” -f filename -f 后面要接被处理的文件名,建议-f 单独写一个参数 -v 在压缩/解压缩过程中,将正在处理的文件名显示出来 -C 在解压缩时,若要解压缩到特定目录就可以使用这个参数

2、解打包命令

tar -xvf 打包文件名

3、.tar.gz压缩格式

其实.tar.gz格式是先打包为.tar格式,再压缩为.gz格式

tar -zcvf 压缩包名.tar.gz 源文件
将源文件压缩成.tar.gz格式的文件

tar -zxvf 压缩包名.tar.gz
将压缩包名.tar.gz的文件解压

4、.tar.bz2压缩格式

其实.tar.bz2格式是先打包为.tar格式,再压缩为.bz2格式

tar -jcvf 压缩包名.tar.bz2 源文件
将源文件压缩成.tar.bz2格式的文件

tar -jxvf 压缩包名.tar.bz2
将压缩包名.tar.bz2的文件解压

5、综合举例

[root@localhost zixuan]# ls laoshione  three  two[root@localhost zixuan]# tar -cvf laoshi.tar laoshilaoshi/laoshi/onelaoshi/threelaoshi/two[root@localhost zixuan]# lslaoshi  laoshi.tar  learn  zm[root@localhost zixuan]# tar -tvf laoshi.tardrwxr-xr-x root/root         0 2017-05-22 11:43 laoshi/-rw-r--r-- root/root         0 2017-05-20 16:59 laoshi/one-rw-r--r-- root/root         0 2017-05-20 16:59 laoshi/three-rw-r--r-- root/root         0 2017-05-20 16:59 laoshi/two[root@localhost zixuan]# lslaoshi  laoshi.tar  learn  zm[root@localhost zixuan]# rm -rf laoshi[root@localhost zixuan]# lslaoshi.tar  learn  zm[root@localhost zixuan]# tar -xvf laoshi.tarlaoshi/laoshi/onelaoshi/threelaoshi/two[root@localhost zixuan]# lslaoshi  laoshi.tar  learn  zm[root@localhost zixuan]# tar -zcvf laoshi.tar.gz laoshilaoshi/laoshi/onelaoshi/threelaoshi/two[root@localhost zixuan]# lslaoshi  laoshi.tar  laoshi.tar.gz  learn  zm[root@localhost zixuan]# tar -jcvf laoshi.tar.bz2 laoshilaoshi/laoshi/onelaoshi/threelaoshi/two[root@localhost zixuan]# lslaoshi  laoshi.tar  laoshi.tar.bz2  laoshi.tar.gz  learn  zm[root@localhost zixuan]# tar -jtvf laoshi.tar.bz2drwxr-xr-x root/root         0 2017-05-22 11:43 laoshi/-rw-r--r-- root/root         0 2017-05-20 16:59 laoshi/one-rw-r--r-- root/root         0 2017-05-20 16:59 laoshi/three-rw-r--r-- root/root         0 2017-05-20 16:59 laoshi/two[root@localhost zixuan]# ls laoshione  three  two
阅读全文
0 0
原创粉丝点击