一、tar指令的基本选项
操作模式选项:
选项 | 选项说明 | 翻译 |
---|---|---|
-A, --catenate, --concatenate | append tar files to an archive | 将文件附加到存档 |
-c, --create | create a new archive | 创建新存档 |
-d, --diff, --compare | find differences between archive and file system | 查找存档和文件系统之间的差异 |
–delete | delete from the archive (not on mag tapes!) | 从存档中删除 |
-r, --append | append files to the end of an archive | 将文件附加到存档末尾 |
-t, --list | list the contents of an archive | 列出存档的内容 |
–test-label | test the archive volume label and exit | 测试存档卷标签并退出 |
-u, --update | only append files newer than copy in archive | 仅在存档中附加比副本更新的文件 |
-x, --extract, --get | extract files from an archive | 从存档中提取文件 |
压缩选项:
选项 | 选项说明 | 翻译 |
---|---|---|
-z, --gzip | filter the archive through gzip | 使用gzip压缩方式(.tar.gz) |
-j, --bzip2 | filter the archive through bzip2 | 使用bzip2压缩方式(.tar.bz2) |
-J, --xz | filter 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