Teng's blog Teng's blog
首页
Java
H5前端
GitHub (opens new window)
首页
Java
H5前端
GitHub (opens new window)
  • 介绍

  • 安装

  • 基础

    • Linux目录结构
    • vi与vim编辑器
    • 关机与重启
    • 用户登录与注销
    • 用户管理
    • 用户组管理
    • 运行级别
    • 帮助命令
    • 文件目录操作
    • 日期与时间操作
    • 搜索查找
    • 压缩与解压
      • gzip/gunzip 压缩与解压
      • zip/unzip 压缩与解压
      • tar 打包压缩
    • 文件组管理
    • 文件权限管理
    • 任务调度
    • 磁盘分区与挂载
    • 网络配置
    • 进程管理
    • 服务管理
    • RPM与YUM
    • SSH配置
    • 附-常用快捷键
  • shell

  • 面试与总结
  • System-Linux
  • 基础
Shetengteng
2022-02-26

压缩与解压

# gzip/gunzip 压缩与解压

只能压缩文件不能压缩目录,不保留原来的文件

gzip 用于压缩文件,gunzip 用于解压

基本语法

  • gzip 文件
    • 压缩文件,只能将文件压缩为 *.gz文件
  • gunzip 文件.gz
    • 解压缩文件命令

应用实例

  • gzip 压缩,将/home下的hello.txt文件进行压缩
    • gzip /home/hello.txt
  • gunzip 解压缩,将/home下的hello.txt.gz文件解压缩
    • gzip /home/hello.txt.gz
[root@hadoop100 test01]# gzip test03/
gzip: test03/ is a directory -- ignored
[root@hadoop100 test02]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 3月   6 23:31 test02
-rw-r--r--. 1 root root    0 3月  11 22:21 test.txt
[root@hadoop100 test02]# gzip test.txt 
[root@hadoop100 test02]# ll
总用量 8
drwxr-xr-x. 2 root root 4096 3月   6 23:31 test02
-rw-r--r--. 1 root root   29 3月  11 22:21 test.txt.gz
[root@hadoop100 test02]# gunzip test.txt.gz 
[root@hadoop100 test02]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 3月   6 23:31 test02
-rw-r--r--. 1 root root    0 3月  11 22:21 test.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

当使用gzip对文件进行压缩后,不会保留原来的文件

# zip/unzip 压缩与解压

该命令在linux和windows都通用,压缩目录并保留原文件

zip用于压缩文件,unzip用于解压缩

基本语法

  • zip [选项] xxx.zip 将要压缩的内容
    • 压缩文件和目录的命令
  • unzip [选项] xxx.zip
    • 解压缩文件

zip常用选项

  • -r 递归压缩,即压缩目录

unzip常用选项

  • -d <目录>
    • 指定解压后文件的存放目录

应用实例

  • 将/home下的所有文件进行压缩成mypackage.json
    • zip -r mypackage.zip /home/
  • 将 mypackage.zip 解压到 /opt/tmp目录下
    • unzip -d /opt/tmp/ mypackage.zip
[root@hadoop100 test02]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 3月   6 23:31 test02
-rw-r--r--. 1 root root    0 3月  11 22:21 test.txt
[root@hadoop100 test02]# zip mytest.zip test02/ test.txt
  adding: test02/ (stored 0%)
  adding: test.txt (stored 0%)
[root@hadoop100 test02]# ll
总用量 8
-rw-r--r--. 1 root root  308 3月  25 22:59 mytest.zip
drwxr-xr-x. 2 root root 4096 3月   6 23:31 test02
-rw-r--r--. 1 root root    0 3月  11 22:21 test.txt

[root@hadoop100 test02]# unzip mytest.zip -d /home/ziptest
Archive:  mytest.zip
   creating: /home/ziptest/test02/
 extracting: /home/ziptest/test.txt
 # 如果不指定文件夹,那么在当前文件夹下解压
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# tar 打包压缩

打包指令,最后打包后的文件是 .tar.gz的文件

基本语法

  • tar [选项] xxx.tar.gz 打包的内容
    • 打包目录,压缩后的文件格式.tar.gz

选项说明

  • -z 打包的同时压缩
  • -v 显示详细信息
  • -f 指定文件名
  • -x 解压.tar文件
  • -c 产生.tar 打包文件

应用实例

  • 压缩多个文件,将/home/a1.txt 和 /home/a2.txt 压缩成 a.tar.gz
    • home> tar -zcvf a.tar.gz a1.txt a2.txt
  • 将/home文件夹压缩成 myhome.tar.gz
    • home> tar -zcvf myhome.tar.gz /home/
  • 将a.tar.gz 解压到当前目录
    • tar -zxvf a.tar.gz
  • 将 myhome.tar.gz 解压到/opt/目录下
    • tar -zxvf myhome.tar.gz -C /opt/
    • 需要/opt/目录事先存在,否则会报错
示例:压缩多个文件
[root@hadoop100 ziptest]# tar -zcvf mytar.tar.gz test02/ test.txt 
test02/
test.txt
[root@hadoop100 ziptest]# ls
mytar.tar.gz  test02  test.txt
示例:解压到当前目录
[root@hadoop100 ziptest]# tar -zxvf mytar.tar.gz 
示例:解压到指定目录
[root@hadoop100 ziptest]# tar -zxvf mytar.tar.gz -C /home/tartest
1
2
3
4
5
6
7
8
9
10
Last Updated: 2022/03/20, 10:04:55
搜索查找
文件组管理

← 搜索查找 文件组管理→

Theme by Vdoing | Copyright © 2021-2022 Shetengteng | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式