当前位置: 首页 > news >正文

Linux基础命令(三):文件压缩及解压缩命令

文件压缩及解压缩命令

  1. tar — 打包和压缩
    tar 是一个用于打包文件的工具,常常用来将多个文件或目录打包成一个单独的文件。它本身不进行压缩,但可以与压缩工具(如 gzip 或 bzip2)一起使用。
    用法:
  • 打包文件(不压缩):tar -cf archive.tar /path/to/directory_or_files
    解释:
    c:创建一个新的归档文件。
    f:指定归档文件的名称。
    archive.tar:创建的归档文件名称。

  • 打包并使用 gzip 压缩:tar -czf archive.tar.gz /path/to/directory_or_files
    解释:
    z:表示使用 gzip 压缩归档文件。
    tar.gz:打包并压缩后的文件扩展名。

  • 打包并使用 bzip2 压缩:tar -cjf archive.tar.bz2 /path/to/directory_or_files
    解释:
    j:表示使用 bzip2 压缩归档文件。
    tar.bz2:打包并压缩后的文件扩展名。

  • 解包和解压文件:tar -xzf archive.tar.gz
    解释:
    x:表示解压缩。
    z:表示使用 gzip 解压缩。
    f:指定归档文件名。

实例:

[root@iZ2vch0mnibclcpxzrbu5rZ ~]#  tar -czf  test/backup.tar.gz  test/src/
[root@iZ2vch0mnibclcpxzrbu5rZ test]# ls
backup.tar.gz  file1.txt  file2.txt  output.txt  src  xaa  xab[root@iZ2vch0mnibclcpxzrbu5rZ ~]# tar -xzf test/backup.tar.gz -C test
[root@iZ2vch0mnibclcpxzrbu5rZ ~]# ls test/
backup.tar.gz  file1.txt  file2.txt  output.txt  src  test  xaa  xab
  1. gzip — 压缩工具
    gzip 是一种压缩工具,主要用于压缩单个文件。它常与 tar 配合使用来压缩整个目录。
    用法:
  • 压缩文件:gzip filename
    解释:
    filename:待压缩的文件。
    压缩后文件名为 filename.gz,原文件会被替换。
  • 压缩文件并保留原文件:gzip -k filename
    解释:
    -k:保留原文件(不会删除)。
    通过运行 gzip --help 查看 gzip 支持的选项,确认是否支持 -k 选项。如果 -k 不在支持的选项中,那么说明您的 gzip 版本不支持该选项。
    使用 -c 选项代替 -k
    gzip -c file2.txt > file2.txt.gz
  • 解压 .gz 文件:gunzip filename.gz或者gzip -d filename.gz

示例:

[root@iZ2vch0mnibclcpxzrbu5rZ test]# ls
file1.txt  file2.txt  file3.txt  output.txt  src  test  xaa  xab
[root@iZ2vch0mnibclcpxzrbu5rZ test]# gzip file1.txt
[root@iZ2vch0mnibclcpxzrbu5rZ test]# ls
file1.txt.gz  file2.txt  file3.txt  output.txt  src  test  xaa  xab
[root@iZ2vch0mnibclcpxzrbu5rZ test]# gzip -c file2.txt > file2.txt.gz
[root@iZ2vch0mnibclcpxzrbu5rZ test]# ls
file1.txt.gz  file2.txt  file2.txt.gz  file3.txt  output.txt  src  test  xaa  xab
[root@iZ2vch0mnibclcpxzrbu5rZ test]# gunzip file2.txt.gz
gzip: file2.txt already exists; do you wish to overwrite (y or n)? y
[root@iZ2vch0mnibclcpxzrbu5rZ test]# ls
file1.txt.gz  file2.txt  file3.txt  output.txt  src  test  xaa  xab
  1. unzip — 解压 .zip 文件
    unzip 是一个用于解压 .zip 文件的工具,适用于将 .zip 格式的压缩文件解压到指定目录。
    用法:
  • 解压文件:unzip archive.zip
    解释:
    archive.zip:待解压的 .zip 文件。
  • 解压到指定目录:unzip archive.zip -d /path/to/directory
    解释:
    -d:指定解压到的目标目录。
  • 查看 .zip 文件的内容:unzip -l archive.zip
  1. zip — 压缩工具
    zip 是一种压缩工具,用于将多个文件或目录压缩成 .zip 格式的压缩文件。zip 格式广泛应用于 Windows 系统中。
    用法:
  • 压缩文件或目录:zip archive.zip file1 file2 directory
    解释:
    archive.zip:压缩包的输出文件。
    file1, file2:待压缩的文件。
    directory:待压缩的目录。
  • 递归压缩目录中的所有文件:zip -r archive.zip directory
    解释:
    -r:递归压缩目录中的所有文件和子目录。
  • 查看 .zip 文件内容:zipinfo archive.zip
  • 解压 .zip 文件:unzip archive.zip

实例:

[root@iZ2vch0mnibclcpxzrbu5rZ test]# ls
file1.txt.gz  file2.txt  file3.txt  output.txt  src  test  xaa  xab
[root@iZ2vch0mnibclcpxzrbu5rZ test]# zip archive.zip file2.txt file3.txt adding: file2.txt (stored 0%)adding: file3.txt (stored 0%)
[root@iZ2vch0mnibclcpxzrbu5rZ test]# ls
archive.zip  file1.txt.gz  file2.txt  file3.txt  output.txt  src  test  xaa  xab[root@iZ2vch0mnibclcpxzrbu5rZ test]# ls
archive.zip  file1.txt.gz  file2.txt  file3.txt  output.txt  src  test  xaa  xab
[root@iZ2vch0mnibclcpxzrbu5rZ test]# cd ..
[root@iZ2vch0mnibclcpxzrbu5rZ ~]# ls
archive.zip  test
[root@iZ2vch0mnibclcpxzrbu5rZ ~]# unzip archive.zip -d test/test1
Archive:  archive.zipcreating: test/test1/test/src/creating: test/test1/test/src/cfg/
[root@iZ2vch0mnibclcpxzrbu5rZ ~]# cd test
[root@iZ2vch0mnibclcpxzrbu5rZ test]# ls
archive.zip  file1.txt.gz  file2.txt  file3.txt  output.txt  src  test  test1  xaa  xab
http://www.lryc.cn/news/500538.html

相关文章:

  • 目标跟踪算法:ByteTrack、卡尔曼滤波、匈牙利算法、高置信度检测目标、低置信度检测目标
  • [定昌linux系统]如何安装jdk8
  • 【Cadence32】PCB多层板电源、地平面层创建心得➕CM约束管理器Analyze分析显示设置➕“DP”报错DRC
  • 基于SpringBoot+Vue的新闻管理系统
  • 图的割点、割边(Tarjan算法)
  • 算法学习(十四)—— 二叉树的深度搜索(DFS)
  • 【vue2】封装自定义的日历组件(三)之基础添加月份的加减定位到最新月份的第一天
  • LabVIEW偏心圆筒流变仪测控系统
  • Runloop
  • SpringBoot的Bean类三种注入方式(附带LomBok注入)
  • 开源向量数据库介绍说明
  • 【前端】深度解析 JavaScript 中的 new 关键字与构造函数
  • 2024年华中杯数学建模C题基于光纤传感器的平面曲线重建算法建模解题全过程文档及程序
  • 使用 `typing_extensions.TypeAlias` 简化类型定义:初学者指南
  • 如何快速批量把 PDF 转为 JPG 或其它常见图像格式?
  • 如何在组织中塑造和强化绩效文化?
  • OllyDbg、CE简单介绍
  • Python函数——函数的返回值定义语法
  • 【Pandas】pandas isna
  • mysql 数据库表的大小
  • (6)JS-Clipper2之ClipperOffset
  • 如何在Ubuntu中利用repo和git地址下载获取imx6ull的BSP
  • Ruby On Rails 笔记5——常用验证下
  • JS听到了因果的回响
  • 【高中生讲机器学习】28. 集成学习之 Bagging 随机森林!
  • 硬件设计 | Altium Designer软件PCB规则设置
  • 【Elasticsearch】实现用户行为分析
  • python字符串处理基础操作总结
  • 电子商务人工智能指南 6/6 - 人工智能生成的产品图像
  • 【论文阅读】相似误差订正方法在风电短期风速预报中的应用研究