今天无聊,于是把之前写的一个具有解压文件功能的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!