tar 1.0 linux 打包压缩

打包压缩

首先需要知道 linux 不是依据文件的扩展名来区分文件类型的。
但是 linux 中文件有扩展名又是必要的,因为可以帮助程序员区分文件类型,也就是这个是给人看的,机器不看这个。

一、.zip 格式

“.zip”是Windows中最常用的压缩格式,Linux也可以正确识别“.zip”格式,这可以方便地和Windows系统通用压缩文件。

1.压缩

[root@localhost ~]# zip [选项] 压缩包名 源文件或源目录
选项:
    -r:    压缩目录
  • 命令名称:zip。
  • 英文原意:package and compress (archive) files。
  • 所在路径:/usr/bin/zip。
  • 执行权限:所有用户。
  • 功能描述:压缩文件或目录。

因为 zip 是与 Windows一样,所以他需要我们自己写压缩包名

  1. 压缩文件
[root@VM-0-7-centos test]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 19 21:16 myfile.txt
[root@VM-0-7-centos test]# zip myfile_bak.zip myfile.txt 
  adding: myfile.txt (stored 0%)
[root@VM-0-7-centos test]# ll
total 4
-rw-r--r-- 1 root root 170 Aug 19 22:02 myfile_bak.zip
-rw-r--r-- 1 root root   0 Aug 19 21:16 myfile.txt
  1. 压缩多个文件
zip name.zip 文件1 文件2 文件3 
[root@VM-0-7-centos test]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 19 22:03 myfile1.txt
-rw-r--r-- 1 root root 0 Aug 19 22:03 myfile2.txt
-rw-r--r-- 1 root root 0 Aug 19 21:16 myfile.txt
[root@VM-0-7-centos test]# zip myfile_bak.zip myfile.txt myfile1.txt myfile2.txt 
  adding: myfile.txt (stored 0%)
  adding: myfile1.txt (stored 0%)
  adding: myfile2.txt (stored 0%)
[root@VM-0-7-centos test]# ll
total 4
-rw-r--r-- 1 root root   0 Aug 19 22:03 myfile1.txt
-rw-r--r-- 1 root root   0 Aug 19 22:03 myfile2.txt
-rw-r--r-- 1 root root 470 Aug 19 22:04 myfile_bak.zip
-rw-r--r-- 1 root root   0 Aug 19 21:16 myfile.txt
  1. 压缩目录
zip -r name.zip 目录名
[root@VM-0-7-centos ~]# ll
total 4
drwxr-xr-x 3 root root 4096 Aug 19 22:06 test
[root@VM-0-7-centos ~]# zip -r test_document.zip test
  adding: test/ (stored 0%)
  adding: test/myfile2.txt (stored 0%)
  adding: test/haha/ (stored 0%)
  adding: test/myfile1.txt (stored 0%)
  adding: test/myfile_bak.zip (stored 0%)
  adding: test/myfile.txt (stored 0%)
[root@VM-0-7-centos ~]# ll
total 8
drwxr-xr-x 3 root root 4096 Aug 19 22:06 test
-rw-r--r-- 1 root root 1422 Aug 19 22:07 test_document.zip

2.解压缩

命令格式如下:

[root@localhost ~]# unzip [选项] 压缩包名
选项:
    -d:    指定解压缩位置

“.zip”格式的解压缩命令是unzip,其基本信息如下。

  • 命令名称:unzip。
  • 英文原意:list, test and extract compressed files in a ZIP archive。
  • 所在路径:/usr/bin/unzip。
  • 执行权限:所有用户。
  • 功能描述:列表、测试和提取压缩文件中的文件。

不论是文件压缩包,还是目录压缩包,都可以直接解压缩,例如:

[root@localhost ~]# unzip dir1.zip
Archive:  dir1.zip
creating: dir1/
#解压缩

也可以手工指定解压缩位置,例如:

[root@localhost ~]# unzip -d /tmp/ ana.zip
Archive:  ana.zip
inflating: /tmp/anaconda-ks.cfg
#把压缩包解压到指定位置

压缩的时候是不能够指定位置的,只有解压缩的时候可以。

二、 .gz 格式

1.压缩

这个命令的格式如下:

[root@localhost ~]# gzip [选项] 源文件
选项:
    -c:    将压缩数据输出到标准输出中,可以用于保留源文件
    -d:    解压缩
    -r:    压缩目录
    -v:    显示压缩文件的信息
    -数字:  用于指定压缩等级,-1压缩等级最低,压缩比最差;-9压缩比最高。默认压缩比是-6

“.gz”格式是Linux中最常用的压缩格式,使用gzip命令进行压缩,其基本信息如下。

  • 命令名称:gzip。
  • 英文原意:compress or expand files。
  • 所在路径:/bin/gzip。
  • 执行权限:所有用户。
  • 功能描述:压缩文件或目录。
  1. 压缩
    gzip 直接压缩,源文件消失
[root@VM-0-7-centos test]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 20 08:59 info.log
[root@VM-0-7-centos test]# gzip info.log 
[root@VM-0-7-centos test]# ll
total 4
# 压缩文件生成,源文件消失了
-rw-r--r-- 1 root root 29 Aug 20 08:59 info.log.gz
  1. 保留源文件压缩
    压缩并保留源文件
[root@VM-0-7-centos test]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 20 09:02 error.log
# 使用 -c选项,但是不让压缩数据输出到屏幕上,而是重定向到压缩文件中
# 这样可以在压缩文件的同时不删除源文件
[root@VM-0-7-centos test]# gzip -c error.log > error.log.gz
[root@VM-0-7-centos test]# ll
total 4
-rw-r--r-- 1 root root  0 Aug 20 09:02 error.log
-rw-r--r-- 1 root root 30 Aug 20 09:03 error.log.gz
  1. 压缩目录
# 建立测试目录,并在里面建立几个测试文件
[root@localhost ~]# mkdir test
[root@localhost ~]# touch test/test1
[root@localhost ~]# touch test/test2
[root@localhost ~]# touch test/test3

[root@VM-0-7-centos ~]# gzip -r test

# 查看发现test目录依然存在,并没有变为压缩文件
[root@VM-0-7-centos ~]# ll
total 4
drwxr-xr-x 2 root root 4096 Aug 20 09:06 test

# 原来 gzip 命令不会打包目录,而是把目录下所有的子文件分别压缩
[root@VM-0-7-centos ~]# cd test
[root@VM-0-7-centos test]# ll
total 12
-rw-r--r-- 1 root root 26 Aug 20 09:05 test1.gz
-rw-r--r-- 1 root root 26 Aug 20 09:05 test2.gz
-rw-r--r-- 1 root root 26 Aug 20 09:05 test3.gz

在Linux中,打包和压缩是分开处理的。而 gzip 命令只会压缩,不能打包,所以才会出现没有打包目录,而只把目录下的文件进行压缩的情况。

2.解压缩

如果要解压缩“.gz”格式,使用 “gzip -d压缩包” 和 “gunzip压缩包” 命令都可以。
●命令名称:gunzip。
●英文原意:compress or expand files。
●所在路径:/bin/gunzip。
●执行权限:所有用户。
●功能描述:解压缩文件或目录。

  1. 直接解压缩
[root@VM-0-7-centos test]# gunzip test1.gz 

要想解压缩“.gz”格式,还可以使用“gzip -d”命令

[root@localhost ~]# gzip -d test1.gz 
  1. 解压目录

如果要解压缩目录下的内容,则依然使用“-r”选项
依旧只能解压缩,而不能解打包

gunzip -r test
# 先看 test 目录下是三个压缩包
[root@VM-0-7-centos test]# ll
total 12
-rw-r--r-- 1 root root 26 Aug 20 09:05 test1.gz
-rw-r--r-- 1 root root 26 Aug 20 09:05 test2.gz
-rw-r--r-- 1 root root 26 Aug 20 09:05 test3.gz

# 解压
[root@VM-0-7-centos test]# cd ..
[root@VM-0-7-centos ~]# gunzip -r test

# 查看
[root@VM-0-7-centos ~]# ll
total 4
drwxr-xr-x 2 root root 4096 Aug 20 09:29 test
[root@VM-0-7-centos ~]# cd test
[root@VM-0-7-centos test]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 20 09:05 test1
-rw-r--r-- 1 root root 0 Aug 20 09:05 test2
-rw-r--r-- 1 root root 0 Aug 20 09:05 test3

3.查看“.gz”格式压缩的文本文件内容

如果我们压缩的是一个纯文本文件,则可以直接使用zcat命令在不解压缩的情况下查看这个文本文件中的内容

[root@VM-0-7-centos test]# zcat test1.gz 

三、 .bz2 格式

1.压缩

bzip2 命令的格式。

[root@localhost ~]# bzip2 [选项] 源文件
选项:
    -d:    解压缩
    -k:    压缩时,保留源文件
    -v:    显示压缩的详细信息
    -数字:  这个参数和gzip命令的作用一样,用于指定压缩等级,-1压缩等级最低,
            压缩比最差;-9压缩比最高

注意,gzip 只是不会打包目录,但是如果使用 “-r” 选项,则可以分别压缩目录下的每个文件;而 bzip2 命令则根本不支持压缩目录,也没有“-r”选项。

“.bz2”格式是Linux的另一种压缩格式,从理论上来讲,“.bz2”格式的算法更先进、压缩比更好;而“.gz”格式相对来讲压缩的时间更快。

  • 命令名称:bzip2。
  • 英文原意:a block-sorting file compressor。
  • 所在路径:/usr/bin/bzip2。
  • 执行权限:所有用户。
  • 功能描述:.bz2格式的压缩命令。
  1. 压缩

不需要压缩后的包名,自动生成 .bz2 扩展名。直接压缩也不会保留源文件

[root@VM-0-7-centos test]# bzip2 test2
# 查看,同样没有保留源文件
[root@VM-0-7-centos test]# ll
total 8
-rw-r--r-- 1 root root 26 Aug 20 09:05 test1.gz
-rw-r--r-- 1 root root 14 Aug 20 09:05 test2.bz2
-rw-r--r-- 1 root root  0 Aug 20 09:05 test3
  1. 压缩的同时保留源文件

使用“-k”选项来保留源文件,而不用像gzip命令一样使用输出重定向来保留源文件

[root@VM-0-7-centos test]# bzip2 -k test3
[root@VM-0-7-centos test]# ll
total 12
-rw-r--r-- 1 root root 26 Aug 20 09:05 test1.gz
-rw-r--r-- 1 root root 14 Aug 20 09:05 test2.bz2
-rw-r--r-- 1 root root  0 Aug 20 09:05 test3
-rw-r--r-- 1 root root 14 Aug 20 09:05 test3.bz2

2.解压缩

“.bz2”格式可以使用 “bzip2 -d压缩包” 命令来进行解压缩,也可以使用 “bunzip2压缩包” 命令来进行解压缩。

命令格式如下:

[root@localhost ~]# bunzip2 [选项] 源文件
选项:
    -k:    解压缩时,保留源文件
  • 命令名称:bunzip2。
  • 英文原意:a block-sorting file compressor。
  • 所在路径:/usr/bin/bunzip2。
  • 执行权限:所有用户。
  • 功能描述:.bz2格式的解压缩命令。

解压缩

[root@VM-0-7-centos test]# bunzip2 test2.bz2

还可以使用 “bzip2 -d压缩包” 命令来进行解压缩,例如:

[root@VM-0-7-centos test]# bzip2 -d test2.bz2

3.查看“.bz2”格式压缩的文本文件内容

和“.gz”格式一样,“.bz2” 格式压缩的纯文本文件也可以不解压缩直接查看,使用的命令是 bzcat。

[root@VM-0-7-centos test]# bzcat test2.bz2

四、打包

之前的 gzip 和 bzip2 都只能压缩,不能打包,在 linux 中打包和压缩是分开进行的。

1.打包

[root@localhost ~]# tar [选项] [-f 压缩包名] 源文件或目录
选项:
    -c:    打包
    -f:    指定压缩包的文件名。压缩包的扩展名是用来给管理员识别格式的,所以一定
            要正确指定扩展名
    -v:    显示打包文件过程

“.tar” 格式的打包和解打包都使用 tar 命令,区别只是选项不同。

  • 命令名称:tar。
  • 英文原意:tar。
  • 所在路径:/bin/tar。
  • 执行权限:所有用户。
  • 功能描述:打包与解打包命令。
  1. 打包
# 将 test1 打包为 test1.tar
[root@VM-0-7-centos test]# tar -cvf test1.tar test1
test1
[root@VM-0-7-centos test]# ll
total 12
-rw-r--r-- 1 root root     0 Aug 20 10:03 test1
-rw-r--r-- 1 root root 10240 Aug 20 10:04 test1.tar
-rw-r--r-- 1 root root     0 Aug 20 10:03 test2
-rw-r--r-- 1 root root     0 Aug 20 10:03 test3

选项“-cvf”一般是习惯用法,记住打包时需要指定打包之后的文件名,而且要用“.tar”作为扩展名。

  1. 打包目录
[root@VM-0-7-centos ~]# ll
total 4
drwxr-xr-x 2 root root 4096 Aug 20 10:04 test
[root@VM-0-7-centos ~]# tar -cvf test.tar test
test/
test/test3
test/test2
test/test1
test/test1.tar
[root@VM-0-7-centos ~]# ll
total 24
drwxr-xr-x 2 root root  4096 Aug 20 10:04 test
-rw-r--r-- 1 root root 20480 Aug 20 10:08 test.tar
  1. 打包多个文件和目录
# 目录和文件同时可以打包
tar -cvf name.tar 目录或文件1 目录或文件2 目录或文件3
[root@VM-0-7-centos test]# ll
total 4
drwxr-xr-x 2 root root 4096 Aug 20 10:10 doc
-rw-r--r-- 1 root root    0 Aug 20 10:03 test1
-rw-r--r-- 1 root root    0 Aug 20 10:03 test2
-rw-r--r-- 1 root root    0 Aug 20 10:03 test3
[root@VM-0-7-centos test]# tar -cvf myfile.tar doc test1 test2
doc/
test1
test2
[root@VM-0-7-centos test]# ll
total 16
drwxr-xr-x 2 root root  4096 Aug 20 10:10 doc
-rw-r--r-- 1 root root 10240 Aug 20 10:10 myfile.tar
-rw-r--r-- 1 root root     0 Aug 20 10:03 test1
-rw-r--r-- 1 root root     0 Aug 20 10:03 test2
-rw-r--r-- 1 root root     0 Aug 20 10:03 test3
  1. 打包后压缩

压缩命令不能直接压缩目录,我们就先用 tar 命令把目录打成数据包,然后再用 gzip 命令或 bzip2 命令压缩。

[root@VM-0-7-centos ~]# ll
total 4
drwxr-xr-x 3 root root 4096 Aug 20 10:15 test

# 先将 test 目录打包
[root@VM-0-7-centos ~]# tar -cvf myfile.tar test
test/
test/test3
test/test2
test/test1
test/doc/
[root@VM-0-7-centos ~]# ll
total 16
-rw-r--r-- 1 root root 10240 Aug 20 10:15 myfile.tar
drwxr-xr-x 3 root root  4096 Aug 20 10:15 test

# 压缩
[root@VM-0-7-centos ~]# gzip myfile.tar 
[root@VM-0-7-centos ~]# ll
total 8
-rw-r--r-- 1 root root  195 Aug 20 10:15 myfile.tar.gz
drwxr-xr-x 3 root root 4096 Aug 20 10:15 test

2.解打包

“.tar”格式的解打包也需要使用 tar 命令,但是选项不太一样。

[root@localhost ~]# tar [选项] 压缩包
选项:
    -x:        解打包
    -f:        指定压缩包的文件名
    -v:        显示打包文件过程
    -t:        测试,就是不解打包,只是查看包中有哪些文件
    -C 目录:    指定解打包位置

其实解打包和打包相比,只是把打包选项“-cvf”更换为“-xvf”。

-rw-r--r-- 1 root root 10240 Aug 20 10:15 myfile.tar
[root@VM-0-7-centos ~]# tar -xvf myfile.tar 
test/
test/test3
test/test2
test/test1
test/doc/
[root@VM-0-7-centos ~]# ll
total 16
-rw-r--r-- 1 root root 10240 Aug 20 10:15 myfile.tar
drwxr-xr-x 3 root root  4096 Aug 20 10:15 test

如果想要指定解压位置,则需要使用“-C(大写)”选项。例如:

tar -xvf myfile.tar -C ./tmp
#把文件包 myfile.tar 解打包到 ./tmp/ 目录下
[root@VM-0-7-centos ~]# ll
total 16
-rw-r--r-- 1 root root 10240 Aug 20 10:15 myfile.tar
drwxr-xr-x 2 root root  4096 Aug 20 10:27 tmp
[root@VM-0-7-centos ~]# tar -xvf myfile.tar -C ./tmp
test/
test/test3
test/test2
test/test1
test/doc/
[root@VM-0-7-centos ~]# ll
total 16
-rw-r--r-- 1 root root 10240 Aug 20 10:15 myfile.tar
drwxr-xr-x 3 root root  4096 Aug 20 10:27 tmp
[root@VM-0-7-centos ~]# cd tmp
[root@VM-0-7-centos tmp]# ll
total 4
drwxr-xr-x 3 root root 4096 Aug 20 10:15 test

3. 仅仅查看 包 中有哪些文件

tar -tvf myfile.tar 
[root@VM-0-7-centos ~]# ll
total 16
-rw-r--r-- 1 root root 10240 Aug 20 10:15 myfile.tar
drwxr-xr-x 3 root root  4096 Aug 20 10:27 tmp
[root@VM-0-7-centos ~]# tar -tvf myfile.tar 
drwxr-xr-x root/root         0 2021-08-20 10:15 test/
-rw-r--r-- root/root         0 2021-08-20 10:03 test/test3
-rw-r--r-- root/root         0 2021-08-20 10:03 test/test2
-rw-r--r-- root/root         0 2021-08-20 10:03 test/test1
drwxr-xr-x root/root         0 2021-08-20 10:10 test/doc/
[root@VM-0-7-centos ~]# ll
total 16
-rw-r--r-- 1 root root 10240 Aug 20 10:15 myfile.tar
drwxr-xr-x 3 root root  4096 Aug 20 10:27 tmp
#会用长格式显示test.tar文件包中文件的详细信息

五、直接打包并压缩

之前要先打包成“.tar”格式,再压缩成“.tar.gz”或“.tar.bz2”格式,太麻烦了。

其实tar命令是可以同时打包压缩的。

tar -zcvf 压缩包 源文件或目录
tar -zxvf 压缩包
# 等等,第四节的选项这儿都可以用,试试便知
[root@localhost ~]# tar [选项] 压缩包 源文件或目录
选项:
    -z:    压缩和解压缩“.tar.gz”格式
    -j:    压缩和解压缩“.tar.bz2”格式
  1. 压缩 .tar.gz 格式
[root@VM-0-7-centos ~]# tar -zcvf tmp.tar.gz tmp
#把/tmp/目录直接打包压缩为“.tar.gz”格式,通过“-z”来识别格式,“-cvf”和打包选项一致
  1. 解压缩 .tar.gz 格式
[root@VM-0-7-centos ~]# tar -zxvf tmp.tar.gz 
#解压缩与解打包“.tar.gz”格式
  1. 压缩 .tar.gz 格式
[root@VM-0-7-centos ~]# tar -jcvf tmp.tar.bz2 tmp
  1. 解压缩 .tar.gz 格式
[root@VM-0-7-centos ~]# tar -jxvf tmp.tar.bz2

把文件直接压缩成“.tar.gz”和“.tar.bz2”格式,才是Linux中最常用的压缩方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值