【Linux】管理压缩文件( tar )

一、tar指令的基本选项

操作模式选项:

选项选项说明翻译
-A, --catenate, --concatenateappend tar files to an archive将文件附加到存档
-c, --createcreate a new archive创建新存档
-d, --diff, --comparefind differences between archive and file system查找存档和文件系统之间的差异
–deletedelete from the archive (not on mag tapes!)从存档中删除
-r, --appendappend files to the end of an archive将文件附加到存档末尾
-t, --listlist the contents of an archive列出存档的内容
–test-labeltest the archive volume label and exit测试存档卷标签并退出
-u, --updateonly append files newer than copy in archive仅在存档中附加比副本更新的文件
-x, --extract, --getextract files from an archive从存档中提取文件

压缩选项:

选项选项说明翻译
-z, --gzipfilter the archive through gzip使用gzip压缩方式(.tar.gz)
-j, --bzip2filter the archive through bzip2使用bzip2压缩方式(.tar.bz2)
-J, --xzfilter the archive through xz使用xz压缩方式(.tar.xz)

常用的一般选项:

选项选项说明
-v, --verbose详细信息,显示存档或提取的文件有哪些
-f, --file=文件名,此选项必须后接要使用或创建的存档的文件名
-p, --preserve-permissions在提取存档时保留文件和目录的权限,而不去除umask

执行一条tar指令必须要有一个操作模式选项,携带压缩选项即为压缩指令,否则为存档指令。

二、存档

1、存档文件和目录

使用参数-c :

[root@host ~]# tar -cf file.tar file1 file2 file3
[root@host ~]# tar --file=file.tar --creat file1 file2 file3'

2、列出存档内容

使用参数-t :

[root@host ~]# tar -tf file.tar
file1
file2
file3

3、从存档中提取文件

使用参数-x :

[root@host ~]# tar -xf file.tar

当root用户进行提取存档时,他人命令会保留文件的原始用户和组的所有权,如果普通用户使用tar提取文件,文件所有权将属于从存档中提取文件的用户。默认情况下从存档中提取文件时,将从存档内容的权限中去除umask,要保留存档文件的权限可在提取存档时使用-p选项。

[root@host ~]# tar -xpf file.tar

三、压缩

1、三种压缩格式

tar命令支持以下三种不同的压缩格式:

  • gzip:压缩速度最快,历史最久,使用最为广泛,能够跨发行版本甚至跨平台使用。
  • bzip2:通常比gzip创建的压缩文档小,但是可用性不如gzip广泛。
  • xz:xz压缩方式相对较新,但通常可以提供可用方式中最佳的压缩率。

2、创建压缩存档

[root@host ~]# tar -zcvf file.tar.gz file1 file2 file3
file1
file2
file3

[root@host ~]# tar -jcvf file.tar.bz2 file1 file2 file3
file1
file2
file3

[root@host ~]# tar -Jcvf file.tar.xz file1 file2 file3
file1
file2
file3

3、提取压缩的存档

[root@host ~]# tar -zxvf file.tar.gz
file1
file2
file3

[root@host ~]# tar -jxvf file.tar.bz2
file1
file2
file3

[root@host ~]# tar -Jxvf file.tar.xz
file1
file2
file3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值