Linux文件压缩工具有:gzip、bzip2、rar、7zip、lbzip2、xz、lrzip、PeaZip、arj等。
1.1 zip和unzip命令
zip: 既归档又压缩的工具,可以压缩目录.
zip FILE
unzip FILE
unzip -d FILE +路径
素材准备:
[root@localhost ~]# mkdir /test
[root@localhost ~]# cd /test
[root@localhost test]# for i in {1…5};do echo "test i " > t e s t i" > test i">testi.txt;done
[root@localhost test]# ls
test1.txt test2.txt test3.txt test4.txt test5.txt
[root@localhost test]# mkdir dir1
[root@localhost test]# cp /etc/fstab dir1/
实例1: 使用zip压缩文件test1.txt
[root@localhost test]# zip test1.zip test1.txt
adding: test1.txt (stored 0%)
[root@localhost test]# ls test1*
test1.txt test1.zip
压缩率为最高压缩test2.txt
[root@localhost test]# zip -9 test2.zip test2.txt
adding: test2.txt (stored 0%)
[root@localhost test]# ls test2*
test2.txt test2.zip
实例2: 讲当前目录dir1连同目录下文件一起压缩
[root@localhost test]# zip -r dir1.zip dir1/
adding: dir1/ (stored 0%)
adding: dir1/fstab (deflated 44%)
[root@localhost test]# ls dir1*
dir1.zip
dir1:
fstab
实例3: 向压缩文件中test1.zip中添加test2. txt文件
[root@localhost test]# zip -m test1.zip test2.txt
adding: test2.txt (stored 0%)
实例4: 删除压缩文件中的文件
[root@localhost test]# zip -d test1.zip test2.txt
deleting: test2.txt
实例5: 压缩文件时排除某个文件
[root@localhost test]# zip test.zip *.txt -x test1.txt
adding: test3.txt (stored 0%)
adding: test4.txt (stored 0%)
adding: test5.txt (stored 0%)
实例6: 解压文件test2.zip
[root@localhost test]# unzip test2.zip
Archive: test2.zip
extracting: test2.txt
实例7:将压缩文件text.zip在指定目录dir1下解压缩
[root@localhost test]# unzip test.zip -d dir1
Archive: test.zip
extracting: dir1/test3.txt
extracting: dir1/test4.txt
extracting: dir1/test