• Centos常用命令之:压缩与解压缩


      在Linux中,压缩文件的扩展名主要是:[*.tar,*.tar.gz,*.tgz,*.gz,*.Z,*.bz2],虽然,我们知道,在LInux中,文件的扩展名没有什么作用,但是由于在Linux中支持的压缩命令非常的多,为了在解压缩的时候知道用那种方式来解压缩,就需要带上特定的扩展名。

    扩展名对应的压缩方式:

    ◇*.Z:compress程序压缩的文件  

    ◇*.gz:gzip程序压缩的文件

    ◇*.bz2:bzip2程序压缩的文件

    ◇*.tar:tar程序打包的数据,并没有压缩过

    ◇*.tar.gz:tar程序打包的文件,并且经过gzip程序的压缩

    ◇*.tar.bz2:tar程序打包的文件,并且经过bzip2程序的压缩

    其中compress是比较老的一款压缩软件,已经被gzip取代了,用gzip也可以解开compress压缩的文件。

    但是由于这些压缩方式都只能针对单个文件。这时候打包软件[tar]就显得很重要了。

    compress

    用法:compress [-rcV] 文件或目录 ← 压缩命令

    默认情况下,压缩后的文件后缀为.Z,并且压缩前的源文件会消失。

    -r:可以连同目录下的文件也同时给与压缩

    -c:将压缩数据输出成为standard output(输出到屏幕)

    -v:可以秀出压缩后的文件资讯以及压缩过程中的一些档名变化

    默认情况下,我们的Centos中并没用安装compress这个软件,我们可以用下面的命令来在线安装

    yum install ncompress

    实例:

    [root@localhost stu]# ll
    总用量 12
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:46 lin2
    [root@localhost stu]# compress -v lin
    lin:  -- replaced with lin.Z Compression: 27.95%
    [root@localhost stu]# ll
    总用量 12
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:46 lin2
    -rw-r--r--. 1 fuwh fuwh 2064 7月  26 10:45 lin.Z

    如果想要压缩后的文件还保留的话,可以用[-c]选项

    [root@localhost stu]# ll
    总用量 12
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:46 lin2
    -rw-r--r--. 1 fuwh fuwh 2064 7月  26 10:45 lin.Z
    [root@localhost stu]# compress -c lin1 > lin11.Z
    [root@localhost stu]# ll
    总用量 16
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 root root 2064 7月  26 10:58 lin11.Z
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:46 lin2
    -rw-r--r--. 1 fuwh fuwh 2064 7月  26 10:45 lin.Z
    [root@localhost stu]#

    如果要解压缩的话,就用uncompress命令

    [root@localhost stu]# ll
    总用量 16
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 root root 2064 7月  26 10:58 lin11.Z
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:46 lin2
    -rw-r--r--. 1 fuwh fuwh 2064 7月  26 10:45 lin.Z
    [root@localhost stu]# uncompress lin11.Z
    [root@localhost stu]# ll
    总用量 16
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 root root 2865 7月  26 10:58 lin11
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:46 lin2
    -rw-r--r--. 1 fuwh fuwh 2064 7月  26 10:45 lin.Z
    [root@localhost stu]#

    gzip,zcat

    使用gzip压缩的文件扩展名为.gz,还可以解开copmress压缩的文件。

    使用方法:gzip [-cdtv#] 文件名

    -c:将压缩的数据输出到屏幕上

    -d:解压缩的参数,也可以使用gunzip命令

    -t:可以用来检验压缩档的一致性,看看文件有无错误

    -v:可以显示出文件的压缩比率

    -#:压缩等级,-1最快,但是压缩比最差,-9最慢,但是压缩比最好,默认是-6

    zcat可以用来读取压缩后的文件内容

    实例:

    [root@localhost stu]# ll
    总用量 16
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 root root 2865 7月  26 10:58 lin11
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:46 lin2
    [root@localhost stu]# gzip -v lin
    lin:     45.1% -- replaced with lin.gz
    [root@localhost stu]# ll
    总用量 16
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 root root 2865 7月  26 10:58 lin11
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:46 lin2
    -rw-r--r--. 1 fuwh fuwh 1596 7月  26 10:45 lin.gz
    [root@localhost stu]# gzip -c lin2 > lin2.gz
    [root@localhost stu]# ll
    总用量 20
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 root root 2865 7月  26 10:58 lin11
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:46 lin2
    -rw-r--r--. 1 root root 1597 7月  26 11:13 lin2.gz
    -rw-r--r--. 1 fuwh fuwh 1596 7月  26 10:45 lin.gz
    [root@localhost stu]# zcat lin.gz
    ■◇□※
    
    ■terterm
    连不上的原因:防火墙未关闭
    ....后面内容省略....
    [root@localhost stu]# gunzip lin2.gz
    gzip: lin2 already exists; do you wish to overwrite (y or n)? y
    [root@localhost stu]# gzip -d lin.gz
    [root@localhost stu]# ll
    总用量 16
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 root root 2865 7月  26 10:58 lin11
    -rw-r--r--. 1 root root 2865 7月  26 11:13 lin2
    [root@localhost stu]#

    bzip2,bzcat

    bzip2的压缩比率比gzip还要好,用法和gzip基本上一样。压缩的后缀名为.bz2。

    用法:bzip2 [-cdkzv#] 文件名

    -c:将压缩的过程产生的数据输出到屏幕上

    -d:解压缩的参数,也可以使用bunzip2来解压缩。

    -k:保留原始文件

    -z:压缩的参数

    -v:可以显示压缩比率

    -#:与gzip一样压缩比参数

    可以使用bzcat来读取压缩后的文件。

    实例:

    [root@localhost stu]# ll
    总用量 16
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 root root 2865 7月  26 10:58 lin11
    -rw-r--r--. 1 root root 2865 7月  26 11:13 lin2
    [root@localhost stu]# bzip2 -zv lin
      lin:      1.694:1,  4.722 bits/byte, 40.98% saved, 2865 in, 1691 out.
    [root@localhost stu]# ll
    总用量 16
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 root root 2865 7月  26 10:58 lin11
    -rw-r--r--. 1 root root 2865 7月  26 11:13 lin2
    -rw-r--r--. 1 fuwh fuwh 1691 7月  26 10:45 lin.bz2
    [root@localhost stu]# bzip2 -cvk lin2 > lin2.bz2
      lin2:     1.694:1,  4.722 bits/byte, 40.98% saved, 2865 in, 1691 out.
    [root@localhost stu]# ll
    总用量 20
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 root root 2865 7月  26 10:58 lin11
    -rw-r--r--. 1 root root 2865 7月  26 11:13 lin2
    -rw-r--r--. 1 root root 1691 7月  26 11:27 lin2.bz2
    -rw-r--r--. 1 fuwh fuwh 1691 7月  26 10:45 lin.bz2
    [root@localhost stu]# bzip2 -d lin.bz2
    [root@localhost stu]# ll
    总用量 20
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin
    -rw-r--r--. 1 fuwh fuwh 2865 7月  26 10:45 lin1
    -rw-r--r--. 1 root root 2865 7月  26 10:58 lin11
    -rw-r--r--. 1 root root 2865 7月  26 11:13 lin2
    -rw-r--r--. 1 root root 1691 7月  26 11:27 lin2.bz2
    -rw-r--r--. 1 root root    0 7月  26 11:27 lin3
    [root@localhost stu]#

    tar

    前面的压缩命令都是对单个文件的压缩,tar命令主要用来将多个文件或目录打包成一个大文件。

    使用方法:

    tar [-j | -z] [cv] [-f 创建的档名] filename  <-- 打包与压缩

    tar [-j | -z] [tv] [-f 创建的档名]    <-- 查看档名

    tar [-j | -z] [xv] [-f 创建的档名] [-C 目录] 解压缩

    -c:创建打包文件,可搭配-v来查看过程中被打包的档名

    -t:查看打包文件的内容含有哪些档名

    -x:解打包或解压缩的功能,可搭配-C在特定目录中解开,需要注意,-c,-t,-x不能一起出现

    -j:通过bizp2的支持来进行压缩/解压缩,此时的档名为*.tar.bz2

    -z:通过gzip的支持来进行压缩/解压缩,此时的档名为*.tar.gz

    -v:在压缩/解压缩的过程中,将正在处理的档名显示出来

    -f filename:-f后面要立刻接要被处理的档名。所以如果和其他选项一起写的时候,需要写在最后面。

    -C 目录:在解压缩的时候指定解压缩的目录

    例子:

    [root@localhost fuwh]# ll
    总用量 8
    drwxrwxr-x. 2 fuwh fuwh 4096 7月  26 11:55 stu
    drwxrwxr-x. 3 fuwh fuwh 4096 7月  25 10:27 stu2
    [root@localhost fuwh]# tar -zcv -f stu2.tar.gz stu2
    stu2/
    stu2/stu22/
    stu2/stu22/aa.txt
    [root@localhost fuwh]# tar -jcv -f stu2.tar.bz2 stu
    stu/
    stu/lin
    stu/lin11
    stu/lin1
    stu/lin2
    stu/lin3
    [root@localhost fuwh]# ll
    总用量 16
    drwxrwxr-x. 2 fuwh fuwh 4096 7月  26 11:55 stu
    drwxrwxr-x. 3 fuwh fuwh 4096 7月  25 10:27 stu2
    -rw-r--r--. 1 root root 2439 7月  26 11:57 stu2.tar.bz2
    -rw-r--r--. 1 root root  165 7月  26 11:57 stu2.tar.gz
    [root@localhost fuwh]#

    通常,我们在网上下一些开发用的软件,都是tar.gz格式的。比如:

    这个时候,我们就可以用 tar -zxvf 文件名 来解压缩了。

    [root@localhost data]# ll
    总用量 53424
    -rw-r--r--. 1 fuwh fuwh 54703519 7月  26 12:02 server-jre-8                                                                                u141-linux-x64.tar.gz
    [root@localhost data]# tar -zxvf server-jre-8u141-linux-x64                                                                                .tar.gz
    jdk1.8.0_141/
    jdk1.8.0_141/THIRDPARTYLICENSEREADME.txt
    jdk1.8.0_141/lib/
    jdk1.8.0_141/lib/jexec
    
    。。。中略。。。
    
    [root@localhost data]# ll
    总用量 53428
    drwxr-xr-x. 7 uucp  143     4096 7月  12 20:45 jdk1.8.0_141
    -rw-r--r--. 1 fuwh fuwh 54703519 7月  26 12:02 server-jre-8                                                                                u141-linux-x64.tar.gz
    [root@localhost data]#
  • 相关阅读:
    Bacula Plugins
    getopt、getopt_long命令参数
    Notepad++ 快捷键
    make命令
    Linux目录结构
    rhel安装输入法
    libtool编译
    install和cp
    dlopen动态链接库操作
    结构体赋值
  • 原文地址:https://www.cnblogs.com/zerotomax/p/7238492.html
Copyright © 2020-2023  润新知