当前位置:首页 > 文章列表 > 文章 > linux > linux文件打包与压缩的方法是什么

linux文件打包与压缩的方法是什么

来源:亿速云 2024-04-09 17:27:35 0浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《linux文件打包与压缩的方法是什么》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

打包和压缩

将文件或文件夹合并成一个包,然后通过压缩算法进行数据压缩,减小包的体积,方便网络传输。

windows:
  zip
  rar

linux:
  zip
  tar
  gz
  bz2
  tar.gz
  tar.bz2

压缩算法:
  gzip
  bzip2

zip

是一个Windows和Linux中常用打包压缩工具,支持的压缩算法是zip。

zip工具需要安装
  yum install zip

zip压缩一个文件

# 格式
  zip [参数] 压缩包名称  文件路径

[root@abc ~]# zip 123.zip 123.log 
  adding: 123.log (deflated 87%)
[root@abc ~]# ls -l

zip压缩文件夹

# 需要一个-r参数去递归压缩文件夹下的所有内容
[root@abc ~]# zip -r dir.zip dir/
  adding: dir/ (stored 0%)
  adding: dir/one/ (stored 0%)
  adding: dir/123.log (deflated 87%)

zip的静默输出

# -q:参数就是不输出任何打包信息
[root@abc opt]# zip -r -q etc.zip /etc/
[root@abc opt]# ls -l

zip解压命令(unzip)

# 格式
  unzip [参数] 压缩包路径

# unzip解压命令只能解压由zip打包的压缩文件
[root@abc ~]# unzip dir.zip 
Archive:  dir.zip
  inflating: dir/123.log             
[root@abc ~]# 

# 其他压缩包由unzip解压时随即报错。
[root@abc opt]# unzip nginx-.tar.gz
Archive:  nginx-.tar.gz
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of nginx-.tar.gz or
        nginx-.tar.gz.zip, and cannot find nginx-.tar.gz.ZIP, period.


# 查看压缩包中压缩那些内容,不解压?
# 只查看压缩包内容不解压需要使用 -l 参数
[root@abc opt]# unzip -l dir.zip 
Archive:  dir.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  03-11-2021 12:04   dir/
---------                     -------
        0                     1 file

# 解压到指定目录(-d)
[root@abc ~]# unzip -d /root/  etc.zip 
[root@abc opt]# cd /root/
[root@abc ~]# ls
]        anaconda-ks.cfg  dir.zip  index.html           test.pdf.gz  xxxeth0xxx           系统优化.md
123.log  demo.txt         etc      nginx-0.1.22.tar.gz  test.txt     上传与下载.md
123.zip  dir              eth0xxx  test                 xxxeth0      文件管理_(高级).pdf

# 静默输出(-q)
[root@abc ~]# rm -rf etc
[root@abc ~]# unzip -q -d /root/ /opt/etc.zip 
[root@abc ~]# ls -l

tar

tar压缩支持多种压缩算法

tar.gz gzip (用的最多)

tar.bz2 bzip2

gzip

通过gzip压缩算法,将文件压缩一定体积,有利于传输, 不支持打包

[root@abc ~]# ls -l
total 4828
-rw-r--r--   1 root root  244977 Mar 10 12:12 index.html
[root@abc ~]# gzip index.html 
[root@abc ~]# ls -l
gzip压缩一个目录
[root@abc etc]# gzip -r /etc
[root@abc etc]# ls

gzip解压(-d)

[root@abc ~]# ls -l
-rw-r--r--   1 0 0   22652 Mar 10 12:12 index.html.gz
[root@abc ~]# gzip -d index.html.gz 
[root@abc ~]# ls -l

bzip2

使用bzip2 压缩算法来压缩一定体积的文件。

[root@abc ~]# ls -l
total 4828
-rw-r--r--   1 root root  646165 Mar  9 10:31 123.log     
[root@abc ~]# bzip2 123.log 
[root@abc ~]# ls -l
total 4240
-rw-r--r--   1 root root       0 Mar 10 12:04 ]

bzip2解压(-d)

bzip2解压是针对于bzip2压缩的压缩包来进行解压。

[root@abc ~]# ls -l
total 4240
-rw-r--r--   1 root root   42210 Mar  9 10:31 123.log.bz2
[root@abc ~]# bzip2 -d 123.log.bz2 
[root@abc ~]# ls -l

tar

tar其实是一个打包工具,不具备压缩功能,但是可以使用参数调用压缩工具来进行解压。

tar参数
  -c : 创建压缩
  -f ; 指定压缩包名称
  -z : 使用gzip压缩工具进行压缩
  -j : 使用bzip2压缩工具进行压缩
  -J : 使用xz压缩工具进行压缩
  -t : 显示压缩包内容,不解压
  -v : 显示压缩过程
  -P : 允许使用绝对路径进行压缩
  -x : 解压
  -C : 指定解压路径
  -h : 打包软连接
  --exclude : 排除某些文件
  --exclude-from :
参数
  • -c : 创建压缩包

  • -f : 指定压缩包名称

[root@abc ~]# tar -c -f test.tar 123.log 
[root@abc ~]# ls -l
  • -z : 指定使用gzip压缩工具进行压缩

[root@abc ~]# tar  -c -z -f test-one.tar 123.log 
[root@abc ~]# ls -l 
total 5084
-rw-r--r--   1 root root   85279 Mar 11 15:56 test-one.tar

# 注:使用-z参数,不会自动添加.gz后缀

[root@abc ~]# tar -c -z -f anaconda.tar.gz  anaconda-ks.cfg 
[root@abc ~]# ls -l
  • -j : 指定使用bzip2压缩工具进行压缩

[root@abc ~]# tar -c -j -f 123-bask-one.tar 123.log 
[root@abc ~]# ls -l
  • -J : 指定使用xz压缩工具进行压缩

[root@abc test-tar]# tar -c -J  -f etc.tar.xz /etc/
[root@abc ~]# ls -l
  • -t : 查看压缩包内容

[root@abc ~]# tar -t -f 123-bak.tar.bz2 
123.log
[root@abc ~]#
  • -v : 显示压缩包压缩过程

[root@abc ~]# tar -x -v -f etc.tar -C /opt/
  • -P : 允许使用绝对路径进行打包

[root@abc ~]# tar -c -P -f 123-three.tar /etc/passwd
[root@abc ~]# tar -c -f 123-three.tar /etc/passwd
tar: Removing leading `/' from member names
[root@abc ~]#
  • -x : 解压

# tar解压是按照原来的路径进行解压
[root@abc test]# tar -x -f etc.tar 

# tar会自动识别压缩功能
  • -C : 指定解压路径

[root@abc ~]# tar -x -f etc.tar -C /opt/
tar: Removing leading `/' from member names
[root@abc ~]# cd /opt/
[root@abc opt]# ls
abc23  dir  dir.zip  etc  nginx-0.1.22.tar.gz  nginx-.tar.gz  xxx
[root@abc opt]#
  • –exclude : 排除某些文件

[root@abc test-tar]# tar -c -f abc.tar ./* --exclude=abc7 --exclude=abc5   --exclude=abc1 
[root@abc test-tar]# tar -t -f abc.tar 
./abc2
./abc3
./abc4
./abc6
./abc8
./abc9
[root@abc test-tar]#
  • –exclude-from : 根据某个文件列表排除多个文件

[root@abc test-tar]# cat list.txt 
abc995
abc996
abc997
abc998
abc999
[root@abc test-tar]# tar -c -f abc.tar ./* --exclude-from=list.txt
  • -h : 打包软连接

[root@abc test-tar]# tar -c -h -f bin-h.tar /bin

以上就是《linux文件打包与压缩的方法是什么》的详细内容,更多关于Linux的资料请关注golang学习网公众号!

版本声明
本文转载于:亿速云 如有侵犯,请联系study_golang@163.com删除
一文搞懂如何基于 GenAI 提升编码效能一文搞懂如何基于 GenAI 提升编码效能
上一篇
一文搞懂如何基于 GenAI 提升编码效能
小鹏何小鹏谈自动驾驶竞争:与华为各有千秋,未来18个月将有巨大提升
下一篇
小鹏何小鹏谈自动驾驶竞争:与华为各有千秋,未来18个月将有巨大提升
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    542次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    508次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    497次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    484次学习
查看更多
AI推荐
  • PPTFake答辩PPT生成器:一键生成高效专业的答辩PPT
    PPTFake答辩PPT生成器
    PPTFake答辩PPT生成器,专为答辩准备设计,极致高效生成PPT与自述稿。智能解析内容,提供多样模板,数据可视化,贴心配套服务,灵活自主编辑,降低制作门槛,适用于各类答辩场景。
    6次使用
  • SEO标题Lovart AI:全球首个设计领域AI智能体,实现全链路设计自动化
    Lovart
    SEO摘要探索Lovart AI,这款专注于设计领域的AI智能体,通过多模态模型集成和智能任务拆解,实现全链路设计自动化。无论是品牌全案设计、广告与视频制作,还是文创内容创作,Lovart AI都能满足您的需求,提升设计效率,降低成本。
    6次使用
  • 美图AI抠图:行业领先的智能图像处理技术,3秒出图,精准无误
    美图AI抠图
    美图AI抠图,依托CVPR 2024竞赛亚军技术,提供顶尖的图像处理解决方案。适用于证件照、商品、毛发等多场景,支持批量处理,3秒出图,零PS基础也能轻松操作,满足个人与商业需求。
    26次使用
  • SEO标题PetGPT:智能桌面宠物程序,结合AI对话的个性化陪伴工具
    PetGPT
    SEO摘要PetGPT 是一款基于 Python 和 PyQt 开发的智能桌面宠物程序,集成了 OpenAI 的 GPT 模型,提供上下文感知对话和主动聊天功能。用户可高度自定义宠物的外观和行为,支持插件热更新和二次开发。适用于需要陪伴和效率辅助的办公族、学生及 AI 技术爱好者。
    24次使用
  • 可图AI图片生成:快手可灵AI2.0引领图像创作新时代
    可图AI图片生成
    探索快手旗下可灵AI2.0发布的可图AI2.0图像生成大模型,体验从文本生成图像、图像编辑到风格转绘的全链路创作。了解其技术突破、功能创新及在广告、影视、非遗等领域的应用,领先于Midjourney、DALL-E等竞品。
    51次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码