zip 压缩工具 (可以压缩目录)
概念: zip 压缩包在 Windows 和 Linux 中都比较常用,
它可以压缩目录和文件, 压缩目录时, 需要指定目录下的文件.
zip 后面先跟目标文件名 (即压缩后的自定义的压缩包名),
然后跟要压缩的文件或者目录;
安装源码包: yum -y install zip
特点: 压缩完了源文件不删除.
选项:
-r 压缩目录
zip 压缩文件
[root@localhost d6z]# zip 2.txt.zip 2.txt #先跟目标文件名, 再跟指定文件路径.
adding: 2.txt (deflated 75%)
[root@localhost d6z]# ls
1.txt 2.txt 2.txt.zip dior1
压缩目录和文件
[root@localhost d6z]# zip -r dior1.zip 2.txt dior1/ #压缩文件和目录
- updating: dior1/ (stored 0%)
- updating: dior1/1.txt (stored 0%)
- adding: 2.txt (deflated 75%)
[root@localhost d6z]# ls
1.txt 2.txt 2.txt.zip dior1 dior1.zip
解压 zip
[root@localhost d6z]# yum -y install unzip #安装源码包
[root@localhost d6z]# unzip dior1.zip
- Archive: dior1.zip
- replace dior1/1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: #是否要覆盖, 因为压缩的时候并没有把源文件删除, 是否需要覆盖 替换. A 表示全部 yes
指定解压 zip 到哪里去
[root@localhost d6z]# mkdir test #创建目录
[root@localhost d6z]# unzip 2.txt.zip -d test/ #解压缩 2.txt.zip 目录,-d 指定到 test 目录下.
- Archive: 2.txt.zip
- inflating: test/2.txt
zip 不可以查看文件的内容, 不过可以查看目录下都有哪些文件, 示例如下:
[root@localhost d6z]# unzip -l dior1.zip
- Archive: dior1.zip
- Length Date Time Name
- 0 04-14-2018 23:20 dior1/
- 7 04-14-2018 23:20 dior1/1.txt
- 215600 04-14-2018 23:18 2.txt
- 215607 3 files
tar 打包工具
概念: tar 本身就是一个打包工具, 可以把目录打包成一个文件;
它把所有文件整合成一个大文件, 方便复制或者移动;
命令格式: tar [zjxcvfpP] filename tar
常用选项:
-z 表示同时用 gzip 压缩
-j 表示同时用 bzip2 压缩
-J 表示同时用 xz 压缩
-x 表示解包或者解压缩
-t 表示查看 tar 包里面文件
-c 表示建立一 tar 包或者压缩文件包
-v 表示可视化
-f 后面跟文件名 (-f filename, 表示压缩后的文件名为 filename;
或者压缩文件 filename, 如果是多个参数的话, 需要把 - f 写到最前面.
用到的命令
- tar -cvf 123.tar 123
- tar -cvf
打包一个目录
[root@localhost d6z]# tar -cvf dior1.tar dior1
dior1/
dior1/1.txt
[root@localhost d6z]# ls
1.txt 2.txt 2.txt.zip dior1 dior1.tar dior1.zip test
解包
[root@localhost d6z]# tar -cvf dior1.tar dior1
dior1/
dior1/1.txt
[root@localhost d6z]# ls
1.txt 2.txt 2.txt.zip dior1 dior1.tar dior1.zip test
查看文件包里的内容
[root@localhost d6z]# tar -tf dior1.tar
dior1/
dior1/1.txt
打包的时候过滤掉所有. txt 的文件 --exclude
[root@localhost d6z]# tar -cvf dior1.tar --exclude ".txt" dior1
dior1/ #建立一个 dior1.tar 的压缩包, --exclude 过滤所有 ".txt" 的文件 , 打包 dior1
打包并压缩
加上选项 "z" 表示压缩成 gzip 压缩包
[root@localhost d6z]# tar -zcvf dior2.tar.gz --exclude "*.txt" dior1
dior1/
[root@localhost d6z]# ls
1.txt 2.txt 2.txt.zip dior1 dior1.tar dior1.tar.gz dior1.zip dior2.tar.gz test
注: 选项 - zcvf 表示 z 打包的同事并且解压, 建一个 tar 包 或压缩我呢见包, v 可视化, f 后面需要跟文件名, 表示压缩后的文件名
加上 j, 表示打包的同时使用 bzip2 压缩
加上 J, 表示打包的同时使用 xz 压缩
来源: http://www.bubuko.com/infodetail-2562602.html