我是靠谱客的博主 清新心情,这篇文章主要介绍linux压缩和解压类(gzip、zip、tar),现在分享给大家,希望可以做个参考。

gzip/gunzip 压缩

  1. 基本语法
    gzip 文件 (功能描述:压缩文件,只能将文件压缩为*.gz文件)
    gunzip 文件.gz (功能描述:解压缩文件命令)

  2. 经验技巧
    (1)只能压缩文件不能压缩目录
    (2)不保留原来的文件

  3. 案例实操

    (1)gzip压缩

    [root@hadoop101 ~]# ls
    honge.java
    [root@hadoop101 ~]# gzip houge.txt
    [root@hadoop101 ~]# ls
    houge.txt.gz
    

    (2)gunzip解压缩文件

    [root@hadoop101 ~]# gunzip houge.txt.gz 
    [root@hadoop101 ~]# ls
    houge.txt
    

zip/unzip 压缩

  1. 基本语法
    zip [选项] XXX.zip 将要压缩的内容 (功能描述:压缩文件和目录的命令)
    unzip [选项] XXX.zip (功能描述:解压缩文件)
  2. 选项说明
zip选项功能
-r压缩目录
-d<目录>指定解压后文件的存放目录
  1. 经验技巧
    zip 压缩命令在window/linux都通用,可以压缩目录且保留源文件。
  2. 案例实操
    (1)压缩 1.txt 和2.txt,压缩后的名称为12.zip
[root@hadoop100 testzip]# touch 1.txt 2.txt
[root@hadoop100 testzip]# ls
1.txt  2.txt
[root@hadoop100 testzip]# zip 12.zip 1.txt 2.txt
  adding: 1.txt (stored 0%)
  adding: 2.txt (stored 0%)
[root@hadoop100 testzip]# ls
12.zip  1.txt  2.txt

(2)解压 12.zip,并替换当前目录下的1.txt和2.txt

[root@hadoop100 testzip]# unzip 12.zip 
Archive:  12.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: 1.txt                   
replace 2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: 2.txt                   
[root@hadoop100 testzip]# ll
总用量 4
-rw-r--r--. 1 root root 298 2月  24 16:26 12.zip
-rw-r--r--. 1 root root   0 2月  24 16:25 1.txt
-rw-r--r--. 1 root root   0 2月  24 16:25 2.txt

(3)解压12.zip到指定目录-d

[root@hadoop100 testzip]# unzip 12.zip -d 12
Archive:  12.zip
 extracting: 12/1.txt                
 extracting: 12/2.txt                
[root@hadoop100 testzip]# ls
12  12.zip  1.txt  2.txt
[root@hadoop100 testzip]# cd 12
[root@hadoop100 12]# ll
总用量 0
-rw-r--r--. 1 root root 0 2月  24 16:25 1.txt
-rw-r--r--. 1 root root 0 2月  24 16:25 2.txt

tar 打包

  1. 基本语法
    tar [选项] XXX.tar.gz 将要打包进去的内容 (功能描述:打包目录,压缩后的文件格式.tar.gz)
  2. 选项说明
选项功能
-z打包同时压缩
-c产生.tar打包文件
-v显示详细信息
-f指定压缩后的文件名
-x解包.tar文件

3.案例实操
(1)压缩多个文件

[root@hadoop100 testtar]# touch 1.txt 2.txt
[root@hadoop100 testtar]# ll
总用量 0
-rw-r--r--. 1 root root 0 2月  24 16:31 1.txt
-rw-r--r--. 1 root root 0 2月  24 16:31 2.txt
[root@hadoop100 testtar]# tar -zcvf 12.tar.gz 1.txt 2.txt
1.txt
2.txt
[root@hadoop100 testtar]# ll
总用量 4
-rw-r--r--. 1 root root 116 2月  24 16:31 12.tar.gz
-rw-r--r--. 1 root root   0 2月  24 16:31 1.txt
-rw-r--r--. 1 root root   0 2月  24 16:31 2.txt

(2)压缩目录

[root@hadoop100 testtar]# mkdir testtar
[root@hadoop100 testtar]# ll
总用量 8
-rw-r--r--. 1 root root  116 2月  24 16:31 12.tar.gz
-rw-r--r--. 1 root root    0 2月  24 16:31 1.txt
-rw-r--r--. 1 root root    0 2月  24 16:31 2.txt
drwxr-xr-x. 2 root root 4096 2月  24 16:32 testtar
[root@hadoop100 testtar]# tar -zcvf testtar.tar.gz .
./
./testtar/
./12.tar.gz
./1.txt
./2.txt
tar: .: 在我们读入文件时文件发生了变化
[root@hadoop100 testtar]# ll
总用量 12
-rw-r--r--. 1 root root  116 2月  24 16:31 12.tar.gz
-rw-r--r--. 1 root root    0 2月  24 16:31 1.txt
-rw-r--r--. 1 root root    0 2月  24 16:31 2.txt
drwxr-xr-x. 2 root root 4096 2月  24 16:32 testtar
-rw-r--r--. 1 root root  357 2月  24 16:33 testtar.tar.gz

(3)解压到当前目录

[root@hadoop100 testtar]# tar -zxvf testtar.tar.gz

(4)解压到指定目录

[root@hadoop100 testtar]# tar -zxvf testtar.tar.gz -C ./testtar
./
./testtar/
./12.tar.gz
./1.txt
./2.txt
[root@hadoop100 testtar]# cd testtar
[root@hadoop100 testtar]# ll
总用量 8
-rw-r--r--. 1 root root  116 2月  24 16:31 12.tar.gz
-rw-r--r--. 1 root root    0 2月  24 16:31 1.txt
-rw-r--r--. 1 root root    0 2月  24 16:31 2.txt
drwxr-xr-x. 2 root root 4096 2月  24 16:32 testtar

最后

以上就是清新心情最近收集整理的关于linux压缩和解压类(gzip、zip、tar)的全部内容,更多相关linux压缩和解压类(gzip、zip、tar)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部