hellopasswd
常见的压缩文件
- windows系统中,常见的压缩文件有 .rar,.zip,.7z
- linux系统中,常见的压缩文件有 .zip,.gz, .bz2,.xz,.tar.gz,.tar.bz2,.tar.xz
使用压缩工具压缩文件有两个好处,一是可以减少存储空间,二是减少传输时间
gzip压缩工具
常用
-f/--force #强制压缩。不理会是否存在符号链接
-l/--list #列出压缩文件的相关信息
-n/--no-name #压缩文件时,不保存原来文件名称和时间
-N/--name #压缩文件时,保存原来文件名称和时间
-q/--quiet #不显示警告信息
-r/--recursive #递归
-v/--verbose #显示执行过程
-数字 #压缩效率介于1~9的数值,预设值为6,指定值越大,压缩效率越高
示例
[root@localhost ~]# mkdir 111
[root@localhost ~]# cd 111
[root@localhost 111]# du -sh 1.txt
224K 1.txt
[root@localhost 111]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \; #查找/etc/下*.conf后缀的文件内容追加到当前的1.txt文件里
[root@localhost 111]# ls
1.txt
[root@localhost 111]# du -sh 1.txt
448K 1.txt
[root@localhost 111]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost 111]# du -sh 1.txt
704K 1.txt
[root@localhost 111]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost 111]# du -sh 1.txt
1.2M 1.txt
[root@localhost 111]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost 111]# du -sh 1.txt #空隙
1.2M 1.txt
[root@localhost 111]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost 111]# du -sh 1.txt
2.2M 1.txt
这是在写数据的时候,如果数据小于4K,当再次写入数据的时候,不是从这个块里面写的,而是另起一个块,大于4K就写两个块以上。所以,就会出现容量空隙。其实就是块空间没有写满
[root@localhost 111]# gzip 1.txt
[root@localhost 111]# ls
1.txt.gz
[root@localhost 111]# du -sh 1.txt.gz
356K 1.txt.gz
[root@localhost 111]# gzip -d 1.txt.gz
[root@localhost 111]# ls
1.txt
[root@localhost 111]# gzip 1.txt
[root@localhost 111]# gunzip 1.txt.gz
[root@localhost 111]# ls
1.txt
[root@localhost 111]# du -sh 1.txt.gz #默认压缩级别为6
356K 1.txt.gz
[root@localhost 111]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Wed Nov 8 11:36:58 2017
[root@localhost 111]# gzip -d 1.txt.gz
[root@localhost 111]# gzip -1 1.txt
[root@localhost 111]# du -sh 1.txt.gz #使用压缩级别1对比默认压缩
420K 1.txt.gz
[root@localhost 111]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Wed Nov 8 11:36:58 2017, max speed
[root@localhost 111]# gzip -d 1.txt.gz
[root@localhost 111]# gzip -9 1.txt #最高压缩级别为9,当压缩限度超过上限后,级别再高也不会发生变化
[root@localhost 111]# du -sh 1.txt.gz
356K 1.txt.gz
[root@localhost 111]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Wed Nov 8 11:36:58 2017, max compression
[root@localhost 111]# gzip -d 1.txt.gz
[root@localhost 111]# zip -c 1.txt > 2.txt.gz #使用-c可以使压缩或解压的结果传送到标准输出
[root@localhost 111]# ls
1.txt 2.txt.gz
[root@localhost 111]# zip -d -c 2.txt.gz > 2.txt
[root@localhost 111]# ls
1.txt 2.txt 2.txt.gz
[root@localhost 111]# zcat 1.txt.gz #使用zcat可以在不解压情况下,查看文件内容
#内容
3. 其他
bzip2压缩工具
安装bzip2
[root@localhost 111]# yum install -y bzip2
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.btte.net
* updates: mirrors.btte.net
Resolving Dependencies
--> Running transaction check
---> Package bzip2.x86_64 0:1.0.6-13.el7 will be installed
--> Processing Dependency: bzip2-libs = 1.0.6-13.el7 for package: bzip2-1.0.6-13.el7.x86_64
--> Running transaction check
---> Package bzip2-libs.x86_64 0:1.0.6-12.el7 will be updated
---> Package bzip2-libs.x86_64 0:1.0.6-13.el7 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
===================================================================================================
Package Arch Version Repository Size
===================================================================================================
Installing:
bzip2 x86_64 1.0.6-13.el7 base 52 k
Updating for dependencies:
bzip2-libs x86_64 1.0.6-13.el7 base 40 k
Transaction Summary
===================================================================================================
Install 1 Package
Upgrade ( 1 Dependent package)
Total download size: 92 k
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/2): bzip2-libs-1.0.6-13.el7.x86_64.rpm | 40 kB 00:00:00
(2/2): bzip2-1.0.6-13.el7.x86_64.rpm | 52 kB 00:00:01
---------------------------------------------------------------------------------------------------
Total 65 kB/s | 92 kB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : bzip2-libs-1.0.6-13.el7.x86_64 1/3
Installing : bzip2-1.0.6-13.el7.x86_64 2/3
Cleanup : bzip2-libs-1.0.6-12.el7.x86_64 3/3
Verifying : bzip2-libs-1.0.6-13.el7.x86_64 1/3
Verifying : bzip2-1.0.6-13.el7.x86_64 2/3
Verifying : bzip2-libs-1.0.6-12.el7.x86_64 3/3
Installed:
bzip2.x86_64 0:1.0.6-13.el7
Dependency Updated:
bzip2-libs.x86_64 0:1.0.6-13.el7
Complete!
常用
-f/--force #强制压缩。不理会是否存在符号链接
-l/--list #列出压缩文件的相关信息
-n/--no-name #压缩文件时,不保存原来文件名称和时间
-N/--name #压缩文件时,保存原来文件名称和时间
-q/--quiet #不显示警告信息
-r/--recursive #递归
-v/--verbose #显示执行过程
-数字 #压缩效率介于1~9的数值,预设值为6,指定值越大,压缩效率越高
示例
[root@localhost 111]# bzip2 2.txt
[root@localhost 111]# ls
1.txt 1.txt.gz 2.txt.bz2
[root@localhost 111]# du -sh 2.txt.bz2
132K 2.txt.bz2
[root@localhost 111]# file 2.txt.bz2 #bzip2默认压缩级别为9
2.txt.bz2: bzip2 compressed data, block size = 900k
[root@localhost 111]# bzip2 -d -c 2.txt.bz2 > 3.txt
[root@localhost 111]# ls
1.txt 1.txt.gz 2.txt.bz2 3.txt
[root@localhost 111]# bunzip2 2.txt.bz2
[root@localhost 111]# ls
1.txt 1.txt.gz 2.txt 3.txt
[root@localhost 111]# bzip2 -1 2.txt #使用压缩级别1与默认的压缩级别对比
[root@localhost 111]# du -sh 2.txt.bz2
284K 2.txt.bz2
[root@localhost 111]# file 2.txt.bz2
2.txt.bz2: bzip2 compressed data, block size = 100k
[root@localhost 111]# bzcat 2.txt.bz2 #bzip2在不压缩的情况下查看文件内容
#内容
[root@localhost 111]# ls
1.txt 1.txt.gz 2.txt.bz2 3.txt
[root@localhost 111]# du -sh 1.txt.gz
300K 1.txt.gz
[root@localhost 111]# du -sh 2.txt.bz2
284K 2.txt.bz2
xz压缩工具
常用
-f/--force #强制压缩。不理会是否存在符号链接
-l/--list #列出压缩文件的相关信息
-n/--no-name #压缩文件时,不保存原来文件名称和时间
-N/--name #压缩文件时,保存原来文件名称和时间
-q/--quiet #不显示警告信息
-r/--recursive #递归
-v/--verbose #显示执行过程
-数字 #压缩效率介于1~9的数值,预设值为6,指定值越大,压缩效率越高
示例
[root@localhost 111]# ls
1.txt 1.txt.gz 2.txt.bz2 3.txt
[root@localhost 111]# xz 3.txt #使用xz压缩比大,但耗时也大,默认压缩级别为6
[root@localhost 111]# du 3.txt.xz
56K 3.txt.xz
[root@localhost 111]# xz -d 3.txt.xz
[root@localhost 111]# xz -c 3.txt > 3.txt.xz
[root@localhost 111]# ls
1.txt 1.txt.gz 2.txt.bz2 3.txt 3.txt.xz
[root@localhost 111]# unxz -c 3.txt.xz > 4.txt #使用unxz跟xz -d是一样的
[root@localhost 111]# ls
1.txt 1.txt.gz 2.txt.bz2 3.txt 3.txt.xz 4.txt
[root@localhost 111]# xzcat 3.txt.xz #xz的显示内容方式
#内容
[root@localhost 111]# du -sh 1.txt.gz
300K 1.txt.gz
[root@localhost 111]# du -sh 2.txt.bz2
284K 2.txt.bz2
[root@localhost 111]# du -sh 3.txt.xz
56K 3.txt.xz
zip压缩工具
使用zip支持压缩目录
安装zip压缩工具
[root@localhost ~]# yum install -y zip
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.btte.net
* updates: mirrors.btte.net
Resolving Dependencies
--> Running transaction check
---> Package zip.x86_64 0:3.0-11.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===================================================================================================
Package Arch Version Repository Size
===================================================================================================
Installing:
zip x86_64 3.0-11.el7 base 260 k
Transaction Summary
===================================================================================================
Install 1 Package
Total download size: 260 k
Installed size: 796 k
Downloading packages:
zip-3.0-11.el7.x86_64.rpm | 260 kB 00:00:04
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : zip-3.0-11.el7.x86_64 1/1
Verifying : zip-3.0-11.el7.x86_64 1/1
Installed:
zip.x86_64 0:3.0-11.el7
Complete!
[root@localhost ~]# zip 1.txt.zip 1.txt
[root@localhost ~]# zip -d 1.txt 1.txt.zip
[root@localhost ~]# zip -r 111.txt.zip 1.txt 111
安装zip解压工具
[root@localhost ~]# yum install -y unzip
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.aliyun.com
* updates: mirrors.btte.net
Resolving Dependencies
--> Running transaction check
---> Package unzip.x86_64 0:6.0-16.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===================================================================================================
Package Arch Version Repository Size
===================================================================================================
Installing:
unzip x86_64 6.0-16.el7 base 169 k
Transaction Summary
===================================================================================================
Install 1 Package
Total download size: 169 k
Installed size: 365 k
Downloading packages:
unzip-6.0-16.el7.x86_64.rpm | 169 kB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : unzip-6.0-16.el7.x86_64 1/1
Verifying : unzip-6.0-16.el7.x86_64 1/1
Installed:
unzip.x86_64 0:6.0-16.el7
Complete!
示例
- 使用zip压缩文件
[root@localhost 111]# ls
1.txt 1.txt.gz 2.txt.bz2 3.txt 3.txt.xz 4.txt
[root@localhost 111]# zip 4.txt.zip 4.txt
adding: 4.txt (deflated 73%)
[root@localhost 111]# du -sh 4.txt.zip
300K 4.txt.zip
- 解压zip文件
[root@localhost 111]# ls
1.txt 1.txt.gz 2.txt.bz2 3.txt 3.txt.xz 4.txt 4.txt.zip
[root@localhost 111]# unzip 4.txt.zip #使用zip工具压缩解压时,不会自动删除源文件
Archive: 4.txt.zip
replace 4.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
inflating: 4.txt
- 使用zip压缩目录
[root@localhost ~]# cd ..
[root@localhost ~]# du -sh 111
4.2M 111
[root@localhost ~]# zip -r 111.zip 111
adding: 111/ (stored 0%)
adding: 111/1.txt (deflated 73%)
adding: 111/1.txt.gz (deflated 0%)
adding: 111/2.txt.bz2 (deflated 0%)
adding: 111/3.txt (deflated 73%)
adding: 111/3.txt.xz (deflated 0%)
adding: 111/4.txt (deflated 73%)
adding: 111/4.txt.zip (stored 0%)
[root@localhost ~]# du -sh 111.zip
1.8M 111.zip
[root@localhost ~]# unzip 111.zip
Archive: 111.zip
replace 111/1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
inflating: 111/1.txt
inflating: 111/1.txt.gz
inflating: 111/2.txt.bz2
inflating: 111/3.txt
inflating: 111/3.txt.xz
inflating: 111/4.txt
extracting: 111/4.txt.zip
可以通过-P指定加密或解密
tar打包工具
常用
-c #建立新的备份文件
-C #指定路径
-f #指定备份文件
-v #显示执行过程
-t #列出备份文件内容
-x #解包
-r #添加文件到已压缩的文件中
-m #保留原有文件不被覆盖
-N 日期 #只将指定日期更新的文件保存到备份文件里
--exclude 文件 #过滤
-z #处理gzip备份文件
-j #处理bzip2备份文件
-J #处理xz备份文件
示例
- 打包和解包
[root@localhost 111]# tar -cvf 111.tar * #将所有为念打包备份并输出执行过程
1.txt
1.txt.gz
2.txt.bz2
3.txt
3.txt.xz
4.txt
4.txt.zip
[root@localhost 111]# ls
111.tar 1.txt 1.txt.gz 2.txt.bz2 3.txt 3.txt.xz 4.txt 4.txt.zip
[root@localhost 111]# tar -cvf 222.tar --exclude * #使用--exclude可以过滤指定的文件
1.txt
1.txt.gz
2.txt.bz2
3.txt
3.txt.xz
4.txt
4.txt.zip
[root@localhost 111]# ls
111.tar 1.txt 1.txt.gz 222.tar 2.txt.bz2 3.txt 3.txt.xz 4.txt 4.txt.zip
[root@localhost 111]# tar -cvf 333.tar *.txt #将后缀为txt的文件打包备份并输出执行过程
1.txt
3.txt
4.txt
[root@localhost 111]# ls
111.tar 1.txt 1.txt.gz 222.tar 2.txt.bz2 333.tar 3.txt 3.txt.xz 4.txt 4.txt.zip
[root@localhost 111]# tar -xvf 111.tar
1.txt
1.txt.gz
2.txt.bz2
3.txt
3.txt.xz
4.txt
4.txt.zip
[root@localhost 111]# ls
111.tar 1.txt 1.txt.gz 222.tar 2.txt.bz2 333.tar 3.txt 3.txt.xz 4.txt 4.txt.zip
[root@localhost 111]# tar -tf 111.tar #列出tar包的所有文件
1.txt
1.txt.gz
2.txt.bz2
3.txt
3.txt.xz
4.txt
4.txt.zip
[root@localhost ~]# tar -xvf 111.tar -C .
111/
111/1.txt
111/1.txt.gz
111/2.txt.bz2
111/3.txt
111/3.txt.xz
111/4.txt
111/4.txt.zip
- 打包压缩
tar的真正作用是在以上压缩工具除了zip可以压缩目录外,用tar打包目录文件后后方便压缩
[root@localhost 111]# ls -lh 111.tar
-rw-r--r--. 1 root root 4.2M Nov 8 13:41 111.tar #可以清楚的看出,tar包其实也是个文件
[root@localhost 111]# tar -czvf 444.tar.gz *.txt.* #打包并使用gzip压缩
1.txt.gz
2.txt.bz2
3.txt.xz
4.txt.zip
[root@localhost 111]# ls
111.tar 1.txt.gz 2.txt.bz2 3.txt 444.tar.gz 4.txt.zip
1.txt 222.tar 333.tar 3.txt.xz 4.txt
[root@localhost 111]# tar -cjvf 555.tar.bz2 111.tar 222.tar 333.tar #打包并使用bzip2压缩
111.tar
222.tar
333.tar
[root@localhost 111]# ls
111.tar 1.txt.gz 2.txt.bz2 3.txt 444.tar.gz 4.txt.zip
1.txt 222.tar 333.tar 3.txt.xz 4.txt 555.tar.bz2
[root@localhost 111]# tar -cJvf 666.tar.xz * #打包并使用xz压缩
111.tar
1.txt
1.txt.gz
222.tar
2.txt.bz2
333.tar
3.txt
3.txt.xz
444.tar.gz
4.txt
4.txt.zip
555.tar.bz2
[root@localhost 111]# ls
111.tar 1.txt.gz 2.txt.bz2 3.txt 444.tar.gz 4.txt.zip 666.tar.xz
1.txt 222.tar 333.tar 3.txt.xz 4.txt 555.tar.bz2
[root@localhost 111]# tar -xzvf 444.tar.gz #解包并解压gzip
1.txt.gz
2.txt.bz2
3.txt.xz
4.txt.zip
[root@localhost 111]# tar -xjvf 555.tar.bz2 #解包并解压bzip2
111.tar
222.tar
333.tar
[root@localhost 111]# tar -xJvf 666.tar.xz #解包并解压xz
111.tar
1.txt
1.txt.gz
222.tar
2.txt.bz2
333.tar
3.txt
3.txt.xz
444.tar.gz
4.txt
4.txt.zip
555.tar.bz2
[root@localhost 111]# ls
111.tar 1.txt.gz 2.txt.bz2 3.txt 444.tar.gz 4.txt.zip 666.tar.xz
1.txt 222.tar 333.tar 3.txt.xz 4.txt 555.tar.bz2
其他
- 压缩工具对比:
- 压缩耗时:xz >bzip2>7z>gzip
- 压缩比:xz>=7z>bzip2 >gzip(压缩比=压缩后文件大小/源文件大小)
- 利用tar通过网络拷贝数据
[root@localhost ~]# ls 111/
hello.txt
[root@localhost ~]# tar -cvf - 111 | ssh 192.168.9.133 "cd /home/ ; tar xvf -"
test/
test/text.txt
The authenticity of host '192.168.9.133 (192.168.9.133)' can't be established.
ECDSA key fingerprint is 52:0e:48:ee:1f:78:8e:04:eb:67:2c:48:b2:13:e7:63.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added '192.168.9.133' (ECDSA) to the list of known hosts.
root@192.168.9.133's password:
test/
test/text.txt
[root@localhost ~]# tar -cvf - 222 | ssh 192.168.9.133 "cd /home/ ; tar xvf -"
222/
222/1.txt
root@192.168.9.133's password:
222/
222/1.txt
[root@localhost2 ~] ls /home/
test
将要拷贝的目录test打包,-代表标准输出,然后再ssh到目标主机192.168.9.133,运行相应的命令,tar xvf -将前面的标准输出内容作为解包对象
修改于 171108