linux中的tar命令

tar归档

创建归档文件:

命令格式:tar 选项 打包后的文件名 要打包的文件

-c 创建

-v 详细信息

-f 文件

制作压缩文件
[root@localhost ~]# dd if=/dev/zero of=2M bs=2M count=1
[root@localhost ~]# dd if=/dev/zero of=4M bs=4M count=1
[root@localhost ~]# dd if=/dev/zero of=6M bs=6M count=1
[root@localhost ~]# tar -cvf a.tar 2M 4M 6M
2M
4M
6M
[root@localhost ~]# ls
1.txt  2M  4M  6M  anaconda-ks.cfg  a.tar  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
​
打包和压缩
-z  gzip
[root@localhost ~]# ls
1.txt  2M  4M  6M  anaconda-ks.cfg  a.tar  b.tar.gz  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@localhost ~]# file b.tar.gz 
b.tar.gz: gzip compressed data, from Unix, last modified: Thu Dec  7 11:13:10 2023
​
bzip2
[root@localhost ~]# tar -cjvf c.tar.bz2 2M 4M 6M
2M
4M
6M
[root@localhost ~]# file c.tar.bz2 
c.tar.bz2: bzip2 compressed data, block size = 900k
​
xz文件
[root@localhost ~]# tar -cJvf c.tar.xz 2M 4M 6M
2M
4M
6M
[root@localhost ~]# file c.tar.xz 
c.tar.xz: XZ compressed data
查看压缩包中的文件
[root@localhost ~]# tar -tvf a.tar 
-rw-r--r-- root/root   2097152 2023-12-07 11:08 2M
-rw-r--r-- root/root   4194304 2023-12-07 11:10 4M
-rw-r--r-- root/root   6291456 2023-12-07 11:10 6M
解压缩包
[root@localhost ~]# ls
anaconda-ks.cfg  backup.tar.gz  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
创建文件1-4.txt
[root@localhost ~]# touch {1,2,3,4}.txt
压缩
[root@localhost ~]# tar -czvf 123.tar.gz {1,2,3,4}.txt
1.txt
2.txt
3.txt
4.txt
[root@localhost ~]# ls
123.tar.gz  2.txt  4.txt            backup.tar.gz         公共  视频  文档  音乐
1.txt       3.txt  anaconda-ks.cfg  initial-setup-ks.cfg  模板  图片  下载  桌面
此时删除文件
[root@localhost ~]# rm -rf {1,2,3,4}.txt
[root@localhost ~]# ls
123.tar.gz  anaconda-ks.cfg  backup.tar.gz  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
可以看见txt文件没有了
使用tar -xvf进行解压可以看见文件被解压出来了
[root@localhost ~]# tar -xvf 123.tar.gz 
1.txt
2.txt
3.txt
4.txt
[root@localhost ~]# ls
123.tar.gz  2.txt  4.txt            backup.tar.gz         公共  视频  文档  音乐
1.txt       3.txt  anaconda-ks.cfg  initial-setup-ks.cfg  模板  图片  下载  桌面
​
  • 23
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值