单个文件压缩/解压缩 ------- gzip / bzip2 / xz
1) gzip压缩: 后缀名*.gz
[root@tan /]# gzip /file/1.txt
[root@tan /]# file /file/1.txt.gz (查看文件类型)
/file/1.txt.gz: gzip compressed data, was “1.txt”, from Unix, last modified: Thu Feb 27 03:35:55 2020
gzip解压缩:
[root@tan /]# gzip -d /file/1.txt.gz
2) bzip2压缩: 后缀名*.bz2
[root@tan /]# bzip2 /file/2.txt
bzip2解压缩:
[root@tan /]# bzip2 -d /file/2.txt.bz2
3) xz压缩: 后缀名*.xz
[root@tan /]# xz /file/3.txt
1.txt 2.txt 3.txt.xz
[root@tan /]# file /file/3.txt.xz (查看文件类型)
/file/3.txt.xz: XZ compressed data
xz解压缩:
[root@tan /]# xz -d /file/3.txt.xz
创建打包文件 ---- tar
1) 创建打包文件 *.tar (后缀名要手动输入)
#tar cf 打包文件名称 源文件
c: create创建
f:file文件
[root@tan ~]# tar cf /bak/file01.tar /file/
2) tar解包:
tar xf 打包文件名称 [-C 目录名称] -C 解压到指定路径,不指路径就解压在你当前路径;
[root@tan ~]# tar xf /bak/file01.tar
[root@tan ~]# ls
anaconda-ks.cfg file sh
[root@tan ~]# tar xf /bak/file01.tar -C /tmp/
[root@tan ~]# ls /tmp/
1.txt 2.txt file
3) tar查看包中的文件:
[root@tan ~]# tar tvf /bak/file01.tar
drwxr-xr-x root/root 0 2020-02-27 03:46 file/
-rw-r–r-- root/root 47 2020-02-27 03:36 file/2.txt
-rw-r–r-- root/root 47 2020-02-27 03:35 file/1.txt
调用 gzip 实现压缩/解压缩:
#tar czf 打包文件名称 源文件
[root@tan ~]# tar czf /bak/etc02.tar.gz /etc/
[root@tan ~]# ls /bak/
etc01.tar etc02.tar.gz file01.tar
[root@tan ~]# du -sh /bak/etc01.tar (用 du -sh 查看文件大小)
28M /bak/etc01.tar
[root@tan ~]# du -sh /bak/etc02.tar.gz
9.7M /bak/etc02.tar.gz
[root@tan ~]# tar czf /bak/etc-$(date +%F).tar.gz /etc/ 打包并以时间为名称
[root@tan ~]# ls /bak/
etc-2020-02-27.tar.gz
解压调用的解压:
#tar zxf 打包文件名称 [-C 目录名称] 不加-C 就是当前目录
[root@tan ~]# tar zxf /bak/etc02.tar.gz -C /tmp/
调用bzip2实现压缩/解压缩:
#tar cjf 打包文件名称 目录名称
j: 调用bzip2
[root@tan ~]# tar cjf /bak/etc03.tar.bz2 /etc/
解压缩:
#tar xjf 打包文件名称 [-C 目录名称] 不加-C 就是当前目录
[root@tan ~]# tar xjf /bak/etc03.tar.bz2 -C /tmp/
调用xz实现压缩/解压缩:
#tar cJf 打包文件名称 目录名称
J: 调用xz
[root@tan ~]# tar cJf /bak/etc04.tar.xz/etc/
[root@tan ~]# du -sh /bak/etc04.tar.xz
6.9M /bak/etc04.tar.xz
解压缩:
#tar xJf 打包文件名称 [-C 目录名称] 不加-C 就是当前目录
[root@tan ~]# tar xJf /bak/etc04.tar.xz -C /tmp/
tar例子压缩文件用时间格式显示名称:
压缩后 名称格式是etc-YYYY-MM-DD.tar.gz
#tar czf /bak/etc-$(date +%F).tar.gz /etc/
[root@tan ~]# date +%F
2020-02-27
[root@tan ~]# tar czf /bak/etc-$(date +%F).tar.gz /etc/
[root@tan ~]# ls /bak/
etc-2020-02-27.tar.gz