linux下的智能压缩/解压bash脚本

本文介绍了一个bash脚本,支持多种文件压缩格式(.gz、.bz2、.xz、.lzma、.tar和.gzip),并提供了解压缩功能。通过脚本测试,演示了如何使用该脚本进行文件压缩与解压缩。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         今天无聊,于是把之前写的一个具有解压文件功能的bash脚本修改了一下,并加入了压缩功能,支持多种压缩文件格式:.gz 、.bz2、  .xz、 .lzma 、.tar 和 .gzip而且都经过测试。脚本内容如下我把它命名为samrtzip.sh:

      以下是我测试的结果(压缩一个hello文件夹)

bash-4.1$ ./smartzip.sh hello
your file name is:hello
please make a choice:pack or unpack
1) pack
2) unpack
#? 1
you have selected pack file...
Now we will use tar to pack the file...
please make a choice...
1) gz
2) bz2
3) xz
4) lzma
5) tar
#? 2
packing hello...
hello/
hello/hello.c
hello/hello.mod.c
hello/hello.ko
hello/modules.order
hello/hello.mod.o
hello/.hello.mod.o.cmd
hello/.tmp_versions/
hello/.tmp_versions/hello.mod
hello/.hello.ko.cmd
hello/hello.o
hello/Makefile
hello/.hello.o.cmd
hello/Module.symvers
pack the file hello OK!

bash-4.1$ ls
hello           LSP  share     tftpboot
hello.tar.bz2  nfs  smartzip.sh


解压:

bash-4.1$ ./smartzip.sh hello.tar.bz2
your file name is:hello.tar.bz2
please make a choice:pack or unpack
1) pack
2) unpack
#? 2
you have selected unpack file...
current file type is: ###hello.tar.bz2: bzip2 compressed data, block size = 900k
unpack the file hello.tar.bz2 OK!

bash-4.1$ ls
hello       LSP    share         tftpboot
hello.tar  nfs    smartzip.sh  

bash-4.1$ ./smartzip.sh hello.tar
your file name is:hello.tar
please make a choice:pack or unpack
1) pack
2) unpack
#? 2
you have selected unpack file...
current file type is: ###hello.tar: POSIX tar archive (GNU)
hello/
hello/hello.c
hello/hello.mod.c
hello/hello.ko
hello/modules.order
hello/hello.mod.o
hello/.hello.mod.o.cmd
hello/.tmp_versions/
hello/.tmp_versions/hello.mod
hello/.hello.ko.cmd
hello/hello.o
hello/Makefile
hello/.hello.o.cmd
hello/Module.symvers
unpack the file hello.tar OK!









评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值