0

0

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

WBOY

WBOY

发布时间:2023-05-17 16:56:47

|

3079人浏览过

|

来源于亿速云

转载

打包和压缩

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

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 : 允许使用绝对路径进行打包

    Extjs简单版酒店管理系统  bulid 081016
    Extjs简单版酒店管理系统 bulid 081016

    该系统采用VS2005+SQL2000+Extjs2.0开发由于学extjs 一月不到 属初学者,项目有很多不足地方请见谅(注释不标准按自己想法随意注释了一下)数据库脚本:压缩包目录下.DB.sql便是该项目为双用户:管理员 与营业员 角色登陆显示不同信息数据库方面一小部分功能运用存储过程或者直接附加DB_51aspx下Sql数据库文件

    下载
[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

相关专题

更多
windows查看端口占用情况
windows查看端口占用情况

Windows端口可以认为是计算机与外界通讯交流的出入口。逻辑意义上的端口一般是指TCP/IP协议中的端口,端口号的范围从0到65535,比如用于浏览网页服务的80端口,用于FTP服务的21端口等等。怎么查看windows端口占用情况呢?php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

601

2023.07.26

查看端口占用情况windows
查看端口占用情况windows

端口占用是指与端口关联的软件占用端口而使得其他应用程序无法使用这些端口,端口占用问题是计算机系统编程领域的一个常见问题,端口占用的根本原因可能是操作系统的一些错误,服务器也可能会出现端口占用问题。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

1104

2023.07.27

windows照片无法显示
windows照片无法显示

当我们尝试打开一张图片时,可能会出现一个错误提示,提示说"Windows照片查看器无法显示此图片,因为计算机上的可用内存不足",本专题为大家提供windows照片无法显示相关的文章,帮助大家解决该问题。

792

2023.08.01

windows查看端口被占用的情况
windows查看端口被占用的情况

windows查看端口被占用的情况的方法:1、使用Windows自带的资源监视器;2、使用命令提示符查看端口信息;3、使用任务管理器查看占用端口的进程。本专题为大家提供windows查看端口被占用的情况的相关的文章、下载、课程内容,供大家免费下载体验。

452

2023.08.02

windows无法访问共享电脑
windows无法访问共享电脑

在现代社会中,共享电脑是办公室和家庭的重要组成部分。然而,有时我们可能会遇到Windows无法访问共享电脑的问题。这个问题可能会导致数据无法共享,影响工作和生活的正常进行。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

2349

2023.08.08

windows自动更新
windows自动更新

Windows操作系统的自动更新功能可以确保系统及时获取最新的补丁和安全更新,以提高系统的稳定性和安全性。然而,有时候我们可能希望暂时或永久地关闭Windows的自动更新功能。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

780

2023.08.10

windows boot manager
windows boot manager

windows boot manager无法开机的解决方法:1、系统文件损坏,使用Windows安装光盘或USB启动盘进入恢复环境,选择修复计算机,然后选择自动修复;2、引导顺序错误,进入恢复环境,选择命令提示符,输入命令"bootrec /fixboot"和"bootrec /fixmbr",然后重新启动计算机;3、硬件问题,使用硬盘检测工具进行扫描和修复;4、重装操作系统。本专题还提供其他解决

1489

2023.08.28

windows锁屏快捷键
windows锁屏快捷键

windows锁屏快捷键是Windows键+L、Ctrl+Alt+Del、Windows键+D、Windows键+P和Windows键+R。本专题为大家提供windows相关的文章、下载、课程内容,供大家免费下载体验。

1632

2023.08.30

PS使用蒙版相关教程
PS使用蒙版相关教程

本专题整合了ps使用蒙版相关教程,阅读专题下面的文章了解更多详细内容。

23

2026.01.19

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 7.4万人学习

Git 教程
Git 教程

共21课时 | 2.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号