前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux gzip 压缩/解压 详解[通俗易懂]

Linux gzip 压缩/解压 详解[通俗易懂]

作者头像
全栈程序员站长
发布2022-07-21 19:00:01
5.9K0
发布2022-07-21 19:00:01
举报

大家好,又见面了,我是你们的朋友全栈君。

gzip 是linux中常见的压缩/解压工具,最常见的使用对象是*.gz格式的文件,这里简单介绍下它最常见的用法,

GZIP(1) General Commands Manual GZIP(1)

NAME gzip, gunzip, zcat – compress or expand files

SYNOPSIS gzip [ -acdfhklLnNrtvV19 ] [–rsyncable] [-S suffix] [ name … ] gunzip [ -acfhklLnNrtvV ] [-S suffix] [ name … ] zcat [ -fhLV ] [ name … ]

OPTIONS -c –stdout –to-stdout 结果写到标准输出,原文件保持不变 -d –decompress –uncompress 解压 -k –keep 压缩或者解压过程中,保留原文件 -r –recursive -t –test 检查压缩文件的完整性 -v –verbose 显示每个文件的名子和压缩率 -# –fast –best 取值从-1(最快)到-9(最好),默认是-6

示例1,压缩文件 原文件名为file1.txt,压缩后原文件消失,压缩后文件名为file1.txt.gz root@ubuntu:/tmp# ls -l file1.* -rw-r–r– 1 root root 12383865 Aug 21 08:08 file1.txt root@ubuntu:/tmp# gzip file1.txt root@ubuntu:/tmp# ls -l file1.* -rw-r–r– 1 root root 134416 Aug 21 08:08 file1.txt.gz

示例2,解压文件 root@ubuntu:/tmp# gzip -d file1.txt.gz root@ubuntu:/tmp# ls -lh file1.* -rw-r–r– 1 root root 12M Aug 21 08:08 file1.txt

示例3,压缩的时候,显示压缩率 root@ubuntu:/tmp# gzip -v file1.txt file1.txt: 98.9% — replaced with file1.txt.gz

示例4,一条命令压缩多个文件,压缩之后,是各自分开的: root@ubuntu:/tmp# gzip file1.txt file2.txt root@ubuntu:/tmp# ls -l total 1348 -rw-r–r– 1 root root 134416 Aug 21 08:08 file1.txt.gz -rw-r–r– 1 root root 392 Aug 21 08:15 file2.txt.gz

示例5,压缩过程中,保留原文件 root@ubuntu:/tmp# gzip -k file1.txt root@ubuntu:/tmp# ls file1.* file1.txt file1.txt.gz

示例6,压缩到标准输出中 可以连接两个文件 root@ubuntu:/tmp# cat file1.txt file2.txt | gzip > foo.gz 或者 root@ubuntu:/tmp# gzip -c file1.txt file2.txt > foo.gz ======================================

gzip 压缩后的格式为:*.gz

这种压缩方式不能保存原文件;且不能压缩目录

命令举例: #压缩 [root@localhost tmp]# gzip buodo [root@localhost tmp]# ls buodo.gz #解压 [root@localhost tmp]# gunzip buodo.gz [root@localhost tmp]# ls buodo

tar 命令选项: -z(gzip) 用gzip来压缩/解压缩文件 -j(bzip2) 用bzip2来压缩/解压缩文件 -v(verbose) 详细报告tar处理的文件信息 -c(create) 创建新的档案文件 -x(extract) 解压缩文件或目录 -f(file) 使用档案文件或设备,这个选项通常是必选的。

命令举例: #压缩 [root@localhost tmp]# tar -zvcf buodo.tar.gz buodo [root@localhost tmp]# tar -jvcf buodo.tar.bz2 buodo

#解压 [root@localhost tmp]# tar -zvxf buodo.tar.gz [root@localhost tmp]# tar -jvxf buodo.tar.bz2

zip 与gzip相比:1)可以压缩目录; 2)可以保留原文件;

选项:

-r(recursive) 递归压缩目录内的所有文件和目录

命令举例: #压缩和解压文件 [root@localhost tmp]# zip boduo.zip boduo [root@localhost tmp]# unzip boduo.zip

#压缩和解压目录 [root@localhost tmp]# zip -r Demo.zip Demo adding: Demo/ (stored 0%) adding: Demo/Test2/ (stored 0%) adding: Demo/Test1/ (stored 0%) adding: Demo/Test1/test4 (stored 0%) adding: Demo/test3 (stored 0%) [root@localhost tmp]# unzip Demo.zip Archive: Demo.zip creating: Demo/ creating: Demo/Test2/ creating: Demo/Test1/ extracting: Demo/Test1/test4 extracting: Demo/test3

bzip2 压缩后的格式:.bz2 参数 -k 产生压缩文件后保留原文件

命令举例 #压缩 [root@localhost tmp]# bzip2 boduo [root@localhost tmp]# bzip2 -k boduo

#解压 [root@localhost tmp]# bunzip2 boduo.bz2

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/125065.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月4,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档