![cover](https://img-blog.csdnimg.cn/9f075f241122481e955cbd064884a220.png)
命令行基础-tar命令详解
一、归档与压缩二、备份与恢复三、总结
·
目录
一、归档与压缩
1、定义
1)归档的定义
将许多零散的文件整理为一个文件且文件总的大小基本不变
2)压缩的定义
按某种算法减小文件所占用空间的大小,恢复时按对应的逆向算法进行解压
2、常见的压缩格式及命令工具
.gz →gzip、gunzip
.bz2 → bzip2、bunzip2
.xz → xz、unxz
3、tar命令的选项
-c:创建归档
-x:释放归档
-f:指定归档文件名称
-z:调用.gz格式的工具进行处理
-j:调用.bz2格式的工具进行处理
-J:调用.xz格式的工具进行处理
-t:显示归档中的文件清单
-C:指定释放路径
二、备份与恢复
1、制作tar包
1)使用tar -c 命令
-tar -zcf 备份文件tar.gz被备份的文档(可压缩多个文档)
-tar -jcf 备份文件tarbz2被备份的文档(可压缩多个文档)
-tar -Jcf 备份文件tar.xz被备份的文档(可压缩多个文档)
2)实例
[root@localhost wangwu]# ll -h /root/
总用量 4.8M
[root@localhost wangwu]# tar -zcf /home/wangwu/root.tar.gz /root/
[root@localhost wangwu]# ll -h root.tar.gz
-rw-r--r--. 1 root root 1.5M 2月 6 16:51 root.tar.gz
[root@localhost wangwu]# ll -h /root/
总用量 4.8M
[root@localhost wangwu]# tar -jcf /home/wangwu/root.tar.bz2 /root/
[root@localhost wangwu]# ll -h root.tar.bz2
-rw-r--r--. 1 root root 1.4M 2月 6 16:58 root.tar.bz2
[root@localhost wangwu]# ll -h /root/
总用量 4.8M
[root@localhost wangwu]# tar -Jcf /home/wangwu/root.tar.xz /root/
[root@localhost wangwu]# ll -h root.tar.xz
-rw-r--r--. 1 root root 950K 2月 6 17:02 root.tar.xz
2、查看tar包内容
1)使用tar -t 命令
tar -tf 备份文件 文件名称
2)实例
[root@localhost wangwu]# tar -tf root.tar.gz
root/
root/.bash_logout
root/.bash_profile
... ... ...
[root@localhost wangwu]# tar -tf root.tar.bz2
root/
root/.bash_logout
root/.bash_profile
... ... ...
[root@localhost wangwu]# tar -tf root.tar.xz
root/
root/.bash_logout
root/.bash_profile
... ... ...
3、解压tar包
1)使用 tar -x 命令
tar -xf 备份文件 -C 目标位置
2)实例
[root@localhost wangwu]# ll -d /root/
dr-xr-x---. 14 root root 4096 2月 6 16:45 /root/
[root@localhost wangwu]# tar -xf root.tar.bz2 -C /home/wangwu/
[root@localhost wangwu]# ll -d /home/wangwu/root
dr-xr-x---. 14 root root 4096 2月 6 16:45 /home/wangwu/root
[root@localhost wangwu]# tar -xf root.tar.gz -C /opt/
[root@localhost wangwu]# ll -d /opt/root/
dr-xr-x---. 14 root root 4096 2月 6 16:45 /opt/root/
[root@localhost wangwu]# tar -xf root.tar.xz -C /tmp/
[root@localhost wangwu]# ll -d /tmp/root/
dr-xr-x---. 14 root root 4096 2月 6 16:45 /tmp/root/
三、总结
gz压缩速度最快,压缩率最低,解压速度很快;
bz2压缩速度一般,压缩率一般,体积一般,解压速度最慢;
xz压缩速度一般,压缩率最高,解压速度最快;
更多推荐
所有评论(0)