您当前所在位置:首页linuxlinux文件打包与压缩是怎么操作的?

linux文件打包与压缩是怎么操作的?

更新:2024-04-23 17:33:39编辑:jiang归类:linux人气:27

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
windows:
  zip
  rar
 
linux:
  zip
  tar
  gz
  bz2
  tar.gz
  tar.bz2
 
压缩算法:
  gzip
  bzip2

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

1
2
zip工具需要安装
  yum install zip

zip压缩一个文件

1
2
3
4
5
6
# 格式
  zip [参数] 压缩包名称  文件路径
 
[root@abc ~]# zip 123.zip 123.log 
  adding: 123.log (deflated 87%)
[root@abc ~]# ls -l

zip压缩文件夹

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

zip的静默输出

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

zip解压命令(unzip)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 格式
  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

tartar压缩支持多种压缩算法

tar.gz gzip (用的最多)

tar.bz2 bzip2

gzip

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

1
2
3
4
5
[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压缩一个目录

1
2
[root@abc etc]# gzip -r /etc
[root@abc etc]# ls

gzip解压(-d)

1
2
3
4
[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 压缩算法来压缩一定体积的文件。

1
2
3
4
5
6
7
[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压缩的压缩包来进行解压。

1
2
3
4
5
[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其实是一个打包工具,不具备压缩功能,但是可以使用参数调用压缩工具来进行解压。

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

参数

-c : 创建压缩包

-f : 指定压缩包名称

1
2
[root@abc ~]# tar -c -f test.tar 123.log 
[root@abc ~]# ls -l

-z : 指定使用gzip压缩工具进行压缩

1
2
3
4
5
6
7
8
9
[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压缩工具进行压缩

1
2
[root@abc ~]# tar -c -j -f 123-bask-one.tar 123.log 
[root@abc ~]# ls -l

-J : 指定使用xz压缩工具进行压缩

1
2
[root@abc test-tar]# tar -c -J  -f etc.tar.xz /etc/
[root@abc ~]# ls -l

-t : 查看压缩包内容

1
2
3
[root@abc ~]# tar -t -f 123-bak.tar.bz2 
123.log
[root@abc ~]#

-v : 显示压缩包压缩过程

1
[root@abc ~]# tar -x -v -f etc.tar -C /opt/

-P : 允许使用绝对路径进行打包

1
2
3
4
[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 : 解压

1
2
3
4
# tar解压是按照原来的路径进行解压
[root@abc test]# tar -x -f etc.tar 
 
# tar会自动识别压缩功能

-C : 指定解压路径

1
2
3
4
5
6
[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 : 排除某些文件

1
2
3
4
5
6
7
8
9
[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 : 根据某个文件列表排除多个文件

1
2
3
4
5
6
7
[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 : 打包软连接

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


我告诉你msdn版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

linux
linux关机命令哪个好?linux关机命令有哪些 linux如何查看数据库版本信息

游客 回复需填写必要信息