Linux之文件打包,压缩,解压

打包和压缩

Linux中对文件进行打包,压缩有两种命令

zip:将文件进行压缩

tar:将文件进行打包(通过和其他命令结合,也能实现压缩的功能)

1、tar打包命令

在Linux中,tar命令是一个常用的工具,用于打包和解压文件。它在文件管理、备份和压缩方面扮演着重要角色。tar(tape archive)最初是为磁带设备设计的,但现在已经成为文件操作的标准工具之一。它能够将一组文件和目录打包成单个归档文件,也可以从归档文件中提取出文件和目录。通过结合不同的选项,你可以在tar命令中实现广泛的功能。

tar [选项] file.tar [file]

1.1、常用参数

参数功能
-c创建新的归档文件(打包)
-x从归档文件中提取文件(解包)
-f <文件名>指定归档名
-v显示操作的详细信息
-z通过gzip的方式压缩归档文件
-j通过bzip2的方式压缩归档文件
-C指定目录

 

1.2 常规操作

# 将file1的文件打包成一个文件
tar -cvf file.tar file1

# 将aa目录打包
tar -cvf aa.tar aa/

# 将aa目录打包并通过gzip的方式压缩
tar -zcvf aa.tar.gz aa/

#查看归档文件中的内容
tar -tvf aa.tar

# 将aa.tar解压到当前路径
tar -xvf aa.tar

# 将aa.tar解压到指定目录
tar -xvf aa.tar -C bb/

2、 zip压缩命令

zip file.zip [选项] 文件

2.1、 常用参数

参数功能
-r压缩文件夹
-q压缩文件时不显示压缩过程的详细信息。
-d从压缩文件中删除指定文件
-u 用于更新现有的ZIP文件,将新的文件或修改后的文件添加到ZIP存档中。
-f用于刷新(更新)现有ZIP文件中的指定文件。
-m用于移动(归档)文件到一个ZIP压缩文件中,并在移动后将源文件删除。
-e用于对ZIP文件进行加密。
-z为压缩文件添加注释。

2.2、常规操作

# 压缩某个文件
[root@192 ~]# zip aa.zip aa.tar
  adding: aa.tar (deflated 97%)
# 压缩某个目录
[root@192 ~]# zip aa.zip -r aa/
  adding: aa/ (stored 0%)
  adding: aa/file1 (deflated 85%)
  adding: aa/file5 (deflated 82%)
  adding: aa/file2 (deflated 82%)
  adding: aa/file.tar (deflated 96%)
# 静默压缩
[root@192 ~]# zip aa.zip -q -r aa/
[root@192 ~]# 

# 从压缩文件中删除某个文件
[root@192 aa]# zip file.zip -d file5
deleting: file5

# 往压缩文件中更新某个文件
[root@192 aa]# zip file.zip -u file5
  adding: file5 (deflated 82%)

# 往压缩文件中更新某个文件
[root@192 aa]# zip file.zip -f file5 
freshening: file5 (deflated 71%)

# 将某个文件添加到压缩文件中,并删除源文件
[root@192 aa]# zip file.zip -m file6
  adding: file6 (stored 0%)
[root@192 aa]# ll
总用量 16
-rw-r--r--. 1 root root 1901 6月   5 21:56 file1
-rw-r--r--. 1 root root   74 6月   5 21:51 file2
-rw-r--r--. 1 root root   86 6月   5 22:49 file5
-rw-r--r--. 1 root root 1400 6月   5 22:50 file.zip

# 压缩某个文件并进行加密
[root@192 aa]# zip test.zip file* -e
Enter password: 
Verify password: 
  adding: file1 (deflated 85%)
  adding: file2 (deflated 82%)
  adding: file5 (deflated 71%)

# 压缩某个文件并添加注释
[root@192 aa]# zip test.zip file* -z
  adding: file1 (deflated 85%)
  adding: file2 (deflated 82%)
  adding: file5 (deflated 71%)
enter new zip file comment (end with .):
zheshi^H^H
.

3、unzip解压命令

unzip是用来对zip文件进行解压的命令

unzip [选项] 压缩包名 源文件或源目录列表

3.1、 常用参数

参数功能
-l显示压缩文件内所包含的文件
-t检查压缩文件是否正确
-v执行时显示详细的信息
-z仅显示压缩文件的备注文字
-n解压缩时不要覆盖原有的文件
-P<密码>使用zip的密码选项

-q

执行时不显示任何信息
-d<目录>指定文件解压缩后所要存储的目录

3.2、常规操作

# 显示压缩包中的文件,但是不做解压
[root@192 aa]# unzip -l t.zip 
Archive:  t.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  06-05-2024 23:33   test/
     1901  06-05-2024 21:56   test/file1
       74  06-05-2024 21:51   test/file2
       86  06-05-2024 22:49   test/file5
---------                     -------
     2061                     4 files

# 解压文件压缩包到指定目录(如果目录不存在,则会创建一个目录)
[root@192 aa]# unzip  t.zip -d test/
Archive:  t.zip
  inflating: test/file1              
  inflating: test/file2              
  inflating: test/file5

# 解压路径压缩包到指定目录(如果目录不存在,则会创建一个目录)
先创建一个压缩包(相对路径)
[root@192 aa]# zip t.zip -r test/
  adding: test/ (stored 0%)
  adding: test/file1 (deflated 85%)
  adding: test/file2 (deflated 82%)
  adding: test/file5 (deflated 71%)

[root@192 aa]# ll
总用量 16
-rw-r--r--. 1 root root 1901 6月   5 21:56 file1
-rw-r--r--. 1 root root   74 6月   5 21:51 file2
-rw-r--r--. 1 root root   86 6月   5 22:49 file5
drwxr-xr-x. 2 root root   45 6月   5 23:33 test
-rw-r--r--. 1 root root  918 6月   5 23:35 t.zip

# 解压到指定目录
[root@192 aa]# unzip t.zip -d a/
Archive:  t.zip
   creating: a/test/
  inflating: a/test/file1            
  inflating: a/test/file2            
  inflating: a/test/file5            
[root@192 aa]# ll
总用量 16
drwxr-xr-x. 3 root root   18 6月   5 23:35 a
-rw-r--r--. 1 root root 1901 6月   5 21:56 file1
-rw-r--r--. 1 root root   74 6月   5 21:51 file2
-rw-r--r--. 1 root root   86 6月   5 22:49 file5
drwxr-xr-x. 2 root root   45 6月   5 23:33 test
-rw-r--r--. 1 root root  918 6月   5 23:35 t.zip
可以看到在a目录下,又创建了test目录
[root@192 aa]# ll a
总用量 0
drwxr-xr-x. 2 root root 45 6月   5 23:33 test

# 测试压缩文件的有效性
[root@192 aa]# unzip -t t.zip 
Archive:  t.zip
    testing: test/                    OK
    testing: test/file1               OK
    testing: test/file2               OK
    testing: test/file5               OK
No errors detected in compressed data of t.zip.

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半两风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值