对文件进行压缩、解压(tar)
压缩命令:tar
- 压缩命令语法:
tar zcvf 存放路径/压缩包名字 指定要压缩的文件
z 压缩的方式:zip
c 创建压缩包文件
v 显示压缩过程
f 指定压缩包文件路径信息
[root@hgg ~]# tar zcvf /hgg/hgg.tar.gz /hgg/hgg.txt
tar: Removing leading `/' from member names
/hgg/hgg.txt
[root@hgg ~]# ls /hgg
hgg01 hgg.tar.gz hgg.txt
- 如何解压数据包
[root@hgg hgg]# tar xvf /hgg/hgg.tar.gz
hgg/hgg.txt
[root@hgg hgg]# ll
total 0
drwxr-xr-x 2 root root 21 Jul 8 22:01 hgg
-rw-r--r-- 1 root root 0 Jul 8 21:53 hgg.txt
- 确认解压后的文件与源文件是否一致
查看文件的大小和时间信息
利用vimdiff或diff比较两个文件内容是否有区别
[root@hgg hgg]# vimdiff /hgg/hgg/hgg.txt /hgg/hgg.txt
- 不解压查看压缩包数据
[root@hgg ~]# tar tf /hgg/hgg.tar.gz
hgg/hgg.txt
- 在压缩目录中排除指定数据不进行压缩(--exclude)
- 单个文件排除:
tar zcvf 存放路径/压缩包名字 指定要压缩的文件 --exclude=排除的文件
[root@hgg hgg]# tar zcvf /tmp/hgg_exclude.tar.gz /hgg --exclude=/hgg/hggboy.txt
tar: Removing leading `/' from member names
/hgg/
/hgg/hggdog.txt
- 多个文件排除
tar zcvf 存放路径/压缩包名字 指定要压缩的文件 --exclude-from=排除的文件(可以一起放在一个文件中用from统一排除)
[root@hgg hgg]# vim /tmp/hgg.test.txt
/hgg/hgg
/hgg/hggboy.txt
root@hgg hgg]# tar zcvf /tmp/hgg_exclude04.tar.gz /hgg --exclude-from=/tmp/hgg.test.txt
tar: Removing leading `/' from member names
/hgg/
/hgg/hggdog.txt
- 批量压缩(xargs)
[root@hgg ~]# find /hgg -maxdepth 1 -type f -name "*txt"|xargs tar zcvf /tmp/hgg100.tar.gz --查找到.txt的所有文件,然后通过管道跟xargs列为一行进行统一压缩
tar: Removing leading `/' from member names
/hgg/hgg01.txt
/hgg/hgg02.txt
/hgg/hgg03.txt
/hgg/hgg04.txt
/hgg/hgg05.txt
/hgg/hgg06.txt
/hgg/hgg07.txt
/hgg/hgg08.txt
/hgg/hgg09.txt
/hgg/hgg10.txt
[root@hgg ~]# tar tf /tmp/hgg100.tar.gz ---查看压缩是否成功
hgg/hgg01.txt
hgg/hgg02.txt
hgg/hgg03.txt
hgg/hgg04.txt
hgg/hgg05.txt
hgg/hgg06.txt
hgg/hgg07.txt
hgg/hgg08.txt
hgg/hgg09.txt
hgg/hgg10.txt