文件归档
在linux系统中压缩格式通常有gz,bz2,xz,zip,Z。对文本文件而言,压缩算法很简单。就是将常见字符串替换为一些其他标记。压缩算法不同,压缩比也会不一样。
compress:早期的压缩方式。 filename.Z
目前流行的是 xz、gz、bz2
gzip: .gz
gzip /PATH/FILE
-d:解压缩
-num:num是1-9 指定压缩比,默认为6,当用 -9 时压缩到最小
gunzip 解压缩
zcat 可以看压缩文件内的文件(在不解压的情况下查看)
bzip2: .bz2 (比gzip有着更大的压缩比的压缩工具)
bzip2 /PATH/FILE
-d: 解压缩
-num:num是1-9 指定压缩比,默认为6,当用 -9 时压缩到最小
-k:压缩时不删除源文件
bunzip2 解压缩
bzcat 可以查看压缩文件内的文件(在不接呀的情况下查看)
xz: .xz
xz /PATH/FILE
-d: 解压缩
-num:num是1-9 指定压缩比,默认为6,当用 -9 时压缩到最小
-k:压缩时不删除源文件
unxz
xzdec 也是一种解压缩
xzcat
这几个命令不能压缩目录。而是将目录下的所有文件都压缩一遍。
而且这几个命令会在压缩完成后删除源文件。
[root@localhost test_linux]# ls
etc etc.tar
[root@localhost test_linux]# xz -k ./etc.tar
[root@localhost test_linux]# xz -k -9 ./etc.tar
xz: ./etc.tar.xz: 文件已存在
[root@localhost test_linux]# mv ./etc.tar.xz ./etc.tar.xz.bak
[root@localhost test_linux]# xz -k -9 ./etc.tar
[root@localhost test_linux]# ll -h
总用量 46M
drwxr-sr-x. 138 root root 8.0K 10月 9 20:13 etc
-rw-r--r--. 1 root wjx 35M 10月 9 21:35 etc.tar
-rw-r--r--. 1 root wjx 5.5M 10月 9 21:35 etc.tar.xz
-rw-r--r--. 1 root wjx 5.5M 10月 9 21:35 etc.tar.xz.bak
[root@localhost test_linux]# ll
总用量 46444
drwxr-sr-x. 138 root root 8192 10月 9 20:13 etc
-rw-r--r--. 1 root wjx 36024320 10月 9 21:35 etc.tar
-rw-r--r--. 1 root wjx 5758052 10月 9 21:35 etc.tar.xz
-rw-r--r--. 1 root wjx 5761884 10月 9 21:35 etc.tar.xz.bak
其实发现使用9加密还是小了832k的。。。。。。
zip 是众多操作系统都默认的压缩方式。
但和其他压缩命令不一样的地方。他要指定压缩后的名字
zip:其归档又压缩。
zip
tar <参数> xxx.tar file.? file.? (参数f必须在最后面)-rf 继续向归档文件中添加
-cf 创建一个归档
-xf 解开一个归档
-v(verbose) 显示归档过程
--xattrs 归档时,保留文件的扩展属性
--get 取出单个文件--delete 删除单个文件
-A 合并几个tar为一个tar,原先的tar存在
-C 指定解开归档中文件至哪个目录
[root@localhost test_linux]# du -sh ./etc/
39M ./etc/
[root@localhost test_linux]# tar cf etc.tar ./etc ##默认不会将一些链接文件归档
[root@localhost test_linux]# du -sh ./etc.tar
35M ./etc.tar
[root@localhost test_linux]# tar --xattrs -cf etc.bak.tar ./etc ##用了xattrs选项,文件的大小就大了
[root@localhost test_linux]# du -sh ./etc.bak.tar
38M ./etc.bak.tar
压缩格式 zip,gz,bz2,xz (linux下)
gzip etc.tar 压缩至gz格式
gunzip etc.tar.gz 解压gz包tar zcf etc.tar.gz /etc
tar zxf etc.tar.gz
<span style="font-size:18px;">[root@localhost test_linux]# tar cf file.tar file[1-4]
[root@localhost test_linux]# gzip file.tar
[root@localhost test_linux]# ll
总用量 4
-rw-r--r--. 1 root wjx 0 10月 9 21:21 file1
-rw-r--r--. 1 root wjx 0 10月 9 21:21 file2
-rw-r--r--. 1 root wjx 0 10月 9 21:21 file3
-rw-r--r--. 1 root wjx 0 10月 9 21:21 file4
-rw-r--r--. 1 root wjx 159 10月 9 21:23 file.tar.gz
[root@localhost test_linux]# tar -tf file.tar.gz
file1
file2
file3
file4</span>
<span style="font-size:18px;">[root@localhost test_linux]# tar zcf file.tar.gz file[1..4]
[root@localhost test_linux]# ll
总用量 4
-rw-r--r--. 1 root wjx 0 10月 9 21:21 file1
-rw-r--r--. 1 root wjx 0 10月 9 21:21 file2
-rw-r--r--. 1 root wjx 0 10月 9 21:21 file3
-rw-r--r--. 1 root wjx 0 10月 9 21:21 file4
-rw-r--r--. 1 root wjx 130 10月 9 21:25 file.tar.gz</span>
bzip2 etc.tar
bunzip2 etc.tar.bz2
tar jcf etc.tar.bz2 /etc
tar jxf etc.tar.bz2
[root@localhost mnt]# tar -czf etc.tar.gz /etc
tar: 从成员名中删除开头的“/”
[root@localhost mnt]# ll
总用量 12380
drwxr-xr-x. 2 root root 6 9月 22 20:57 cdrom
-rw-r--r--. 1 root root 12674935 10月 9 22:02 etc.tar.gz
xz etc.tar
unxz etc.tar.xz
tar Jcf etc.tar.xz /etc
tar Jxf etc.tar.xz
最后来个对比
[root@localhost test_linux]# bzip2 -k etc.tar
[root@localhost test_linux]# ll
总用量 58408
drwxr-sr-x. 138 root root 8192 10月 9 20:13 etc
-rw-r--r--. 1 root wjx 36024320 10月 9 21:35 etc.tar
-rw-r--r--. 1 root wjx 11095232 10月 9 21:35 etc.tar.bz2
-rw-r--r--. 1 root wjx 12675843 10月 9 21:30 etc.tar.gz
[root@localhost test_linux]# xz -k etc.tar
[root@localhost test_linux]# ll -h
总用量 63M
drwxr-sr-x. 138 root root 8.0K 10月 9 20:13 etc
-rw-r--r--. 1 root wjx 35M 10月 9 21:35 etc.tar
-rw-r--r--. 1 root wjx 11M 10月 9 21:35 etc.tar.bz2
-rw-r--r--. 1 root wjx 13M 10月 9 21:30 etc.tar.gz
-rw-r--r--. 1 root wjx 5.5M 10月 9 21:35 etc.tar.xz
可以发现bzip2 和 xz 在指定选项 -k时就不会删除原文件,而gzip无法指定源文件
这里面,xz的压缩比最好。
unzip etc.zip
[root@localhost test_linux]# touch file{1..4}
[root@localhost test_linux]# zip file.tar.zip file[1-4]
adding: file1 (stored 0%)
adding: file2 (stored 0%)
adding: file3 (stored 0%)
adding: file4 (stored 0%)
[root@localhost test_linux]# unzip file.tar.zip
Archive: file.tar.zip
extracting: file1
extracting: file2
extracting: file3
extracting: file4
[root@localhost test_linux]# ll
总用量 4
-rw-r--r--. 1 root wjx 0 10月 9 21:18 file1
-rw-r--r--. 1 root wjx 0 10月 9 21:18 file2
-rw-r--r--. 1 root wjx 0 10月 9 21:18 file3
-rw-r--r--. 1 root wjx 0 10月 9 21:18 file4
-rw-r--r--. 1 root wjx 574 10月 9 21:19 file.tar.zip
tar -tf 照样可以查看.tar.gz .tar.br2 .tar.xz包的内容
另一个归档工具
cpio:归档工具(一个非常古老的归档工具,但在Cenos还有用)
[root@localhost boot]# ls|grep img
initramfs-0-rescue-6096c81e6fcc48c0a0fe9e5fba473a43.img
initramfs-3.10.0-327.36.1.el7.x86_64.img
initramfs-3.10.0-327.36.1.el7.x86_64kdump.img
initramfs-3.10.0-327.el7.x86_64.img
initramfs-3.10.0-327.el7.x86_64kdump.img
initrd-plymouth.img
[root@localhost boot]# file initramfs-0-rescue-6096c81e6fcc48c0a0fe9e5fba473a43.img
initramfs-0-rescue-6096c81e6fcc48c0a0fe9e5fba473a43.img: ASCII cpio archive (SVR4 with no CRC)
可以看到,这个文件是一个ASCII cpio 归档文件。
文件的传输
scp file username@ip:/dir
scp username@ip:/dir/file
rsync username@ip:/dir/file 文件同步
来几个练习题吧
1.在server主机中把/etc目录打包压缩到/mnt中,名字为etc.tar.gz
2.复制server主机中的etc.tar.gz到desktop主机的/mnt中
3.同步server主机中的/etc中的所有文件到desktop主机中/mnt中,包含链接文件
1.
<span style="font-size:18px;">[root@localhost mnt]# tar -czf etc.tar.gz /etc
tar: 从成员名中删除开头的“/”
[root@localhost mnt]# ll
总用量 12380
drwxr-xr-x. 2 root root 6 9月 22 20:57 cdrom
-rw-r--r--. 1 root root 12674935 10月 9 22:02 etc.tar.gz</span>
2.
scp ./etc.tar.gz root@196.128.1.2:/root/
3.
rsync ./etc.tar.gz root@196.128.1.2:/root/