Linux-文件压缩解压

文件压缩解压

打包:是将多个文件变成一个总的文件,它的学名叫存档、归档。

  • 压缩:是将一个大文件(通常指归档)压缩变成一个小文件。

我们常常使用 tar 将多个文件归档为一个总的文件,称为 archive 。然后用 gzip 或 bzip2 命令将 archive 压缩为更小的文件。

tar
创建一个 tar 归档。
tar -cvf 打包后的文件夹名.tar 要打包的文件

[root@localhost home]# cd 203312032134/
[root@localhost 203312032134]# touch a.txt
[root@localhost 203312032134]# touch b.txt
[root@localhost 203312032134]# touch c.txt
[root@localhost 203312032134]# tar -cvf 2135.tar *.txt
a.txt
b.txt
c.txt
[root@localhost 203312032134]# ls
2135.tar  a.txt  b.txt  c.txt
[root@localhost 203312032134]# 

常用参数
-cvf 表示 create(创建)+ verbose(细节)+ file(文件),创建归档文件并显示操作细节;
tar -cvf 2135.tar *.txt

[root@localhost 203312032134]# tar -cvf 2135.tar *.txt
a.txt
b.txt
c.txt

-tf 显示归档里的内容,并不解开归档;
tar -tf 2135.tar

[root@localhost 203312032134]# tar -tf 2135.tar 
a.txt
b.txt
c.txt
[root@localhost 203312032134]# 

-rvf 追加文件到归档
tar -rvf 2135.tar d.txt

[root@localhost 203312032134]# touch d.txt
[root@localhost 203312032134]# tar -rvf 2135.tar d.txt
d.txt
[root@localhost 203312032134]# tar -tf 2135.tar 
a.txt
b.txt
c.txt
d.txt

-xvf 解开归档
tar -xvf 2135.tar

[root@localhost 203312032134]# ls
2135.tar  a.txt  b.txt  c.txt  d.txt
[root@localhost 203312032134]# rm *.txt
rm: remove regular empty file ‘a.txt’? y
rm: remove regular empty file ‘b.txt’? y
rm: remove regular empty file ‘c.txt’? y
rm: remove regular empty file ‘d.txt’? y
[root@localhost 203312032134]# ls
2135.tar
[root@localhost 203312032134]# tar -xvf 2135.tar 
a.txt
b.txt
c.txt
d.txt
[root@localhost 203312032134]# ls
2135.tar  a.txt  b.txt  c.txt  d.txt
[root@localhost 203312032134]# 

gzip / gunzip
“压缩/解压”归档,默认用 gzip 命令,压缩后的文件后缀名为 .tar.gz 。
gzip 2135.tar

[root@localhost 203312032134]# gzip 2135.tar 
[root@localhost 203312032134]# ls
2135.tar.gz  a.txt  b.txt  c.txt  d.txt

tar 归档+压缩
可以用 tar 命令同时完成归档和压缩的操作,就是给 tar 命令多加一个选项参数,使之完成归档操作后,还是调用 gzip 或 bzip2 命令来完成压缩操作。
tar -zcvf 2142.tar.gz *.txt 压缩

[root@localhost 203312032134]# tar -zcvf 2142.tar.gz *.txt
a.txt
b.txt
c.txt
d.txt
[root@localhost 203312032134]# ls
2135.tar.gz  2142.tar.gz  a.txt  b.txt  c.txt  d.txt

tar -zxvf 2142.tar.gz 解压

[root@localhost 203312032134]# ls
2135.tar.gz  2142.tar.gz  a.txt  b.txt  c.txt  d.txt
[root@localhost 203312032134]# rm -f *.txt
[root@localhost 203312032134]# ls
2135.tar.gz  2142.tar.gz
[root@localhost 203312032134]# tar -zxvf 2142.tar.gz 
a.txt
b.txt
c.txt
d.txt
[root@localhost 203312032134]# ls
2135.tar.gz  2142.tar.gz  a.txt  b.txt  c.txt  d.txt

zcat、zless、zmore

压缩文件的内容使用 zcat、zless、zmore 进行查看。

zip/unzip
安装方式

yum install zip 
yum install unzip

zip 压缩后的文件名.zip 想要压缩的目标文件
unzip 想要解压的文件夹

[root@localhost 203312032134]# zip 1.zip *.txt
  adding: a.txt (stored 0%)
  adding: b.txt (stored 0%)
  adding: c.txt (stored 0%)
  adding: d.txt (stored 0%)
[root@localhost 203312032134]# ls
1.zip  a.txt  b.txt  c.txt  d.txt
[root@localhost 203312032134]# rm -f *.txt
[root@localhost 203312032134]# unzip 1.zip
Archive:  1.zip
 extracting: a.txt                   
 extracting: b.txt                   
 extracting: c.txt                   
 extracting: d.txt                   
[root@localhost 203312032134]# ls
1.zip  a.txt  b.txt  c.txt  d.txt
[root@localhost 203312032134]# 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值