跳转至

tar 文件归档工具

概要: Linux归档工具tar基本使用方法

创建时间: 2022.04.25 21:50:37

更新时间: 2023.07.28 23:08:53

tar 是tape archive的缩写,即磁带归档。常见的 .tar.tgz 结尾文件分别由普通的 tar 工具归档的文件和用 gzip 归档的文件。

命令范式

Bash
tar mode[options] path ...

命令参数

常用参数

Bash
-f  # 指定归档文件

-c  # 建立新的归档文件
-z  # 通过gzip命令来压缩/解压缩文件,文件名一般为 xx.tar.gz
-j  # 通过bzip2命令来压缩/解压缩文件,文件名一般为xx.tar.bz2
-J  # 通过xz命令来压缩/解压缩文件,文件名一般为xx.tar.xz

-r  # 添加新文件到已经压缩的文件中
--exclude=[范本样式]  # 排除符合范本样式的文件(macOS不适用)
--remove-files       # 归档/压缩之后删除源文件(macOS不适用)

-x  # 解压或提取归档文件内容 
-C  # 解压到指定的目录下
-p  # 解压时保留原来的文件权限与属性

-t  # 列出归档文件的内容

-v  # 显示命令执行过程
-w  # 交互式确认归档文件
--help  # 查看命令帮助

全部参数macOS

Bash
tar(bsdtar): manipulate archive files
First option must be a mode specifier:
  -c Create  -r Add/Replace  -t List  -u Update  -x Extract
Common Options:
  -b #  Use # 512-byte records per I/O block
  -f <filename>  Location of archive
  -v    Verbose
  -w    Interactive
Create: tar -c [options] [<file> | <dir> | @<archive> | -C <dir> ]
  <file>, <dir>  add these items to archive
  -z, -j, -J, --lzma  Compress archive with gzip/bzip2/xz/lzma
  --format {ustar|pax|cpio|shar}  Select archive format
  --exclude <pattern>  Skip files that match pattern
  -C <dir>  Change to <dir> before processing remaining files
  @<archive>  Add entries from <archive> to output
List: tar -t [options] [<patterns>]
  <patterns>  If specified, list only entries that match
Extract: tar -x [options] [<patterns>]
  <patterns>  If specified, extract only entries that match
  -k    Keep (don't overwrite) existing files
  -m    Don't restore modification times
  -O    Write entries to stdout, don't restore to disk
  -p    Restore permissions (including ACLs, owner, file flags)
bsdtar 3.5.1 - libarchive 3.5.1 zlib/1.2.11 liblzma/5.0.5 bz2lib/1.0.8

命令示例

准备工作

假设当前目录下有如下结构的一些文件/文件夹

Bash
1
2
3
4
5
6
7
8
.
└── test_tar
    ├── a.txt
    ├── b.txt
    ├── c.txt
    ├── dir2
       └── c.txt
    └── hello.py

归档到指定tar文件

归档指定的多个文件

Bash
tar -cf txt.tar test_tar/a.txt test_tar/b.txt
归档整个文件夹的所有文件(所有文件结构不变)
Bash
tar -cf tt.tar test_tar
归档文件后删除源文件/文件夹(macOS不适用,Linux适用
Bash
tar -cf tt.tar test_tar --remove-files
归档除 dir2 目录之外整个文件夹的所有文件(macOS不适用,Linux适用
Bash
tar -cf tt.tar --exclude test_tar/hello.py test_tar
归档文件夹和子文件夹的特定类型文件(解压后所有的txt文件在同一个文件夹内)
Bash
tar -cf tt2.tar test_tar/*.txt
添加新文件到归档后的文件(将 hello.py 文件添加到上文的 tt2.tar 归档文件中)
Bash
tar -rf tt2.tar test_tar/hello.py
更新已有文件到归档后的文件(将修改后的 hello.py 文件更新到上文的 tt2.tar 归档文件中)(macOS不适用,Linux适用)
Bash
echo "pass" >> test_tar/hello.py
tar -uf tt2.tar test_tar/hello.py
列出指定归档文件中的内容的简要信息(不解压,仅查看)
Bash
tar -tf tt2.tar
image.png列出指定归档文件中的内容的详细信息(不解压,仅查看)
Bash
tar -tvf tt2.tar
image.png

归档并压缩到指定文件

归档并压缩整个文件夹为 .gz 文件

Bash
tar -czf tt.tar.gz test_tar
归档并压缩整个文件夹为 .bz2 文件
Bash
tar -cjf tt.tar.bz2 test_tar
归档并压缩整个文件夹为 .xz 文件
Bash
tar -cJf tt.tar.xz test_tar

解压文件

提取 .tar 文件(不保留源文件权限)

Bash
tar -xf tt.tar
提取 .tar 文件(保留源文件权限)
Bash
tar -xpf tt.tar
提取 .tar 文件到指定目录
Bash
mkdir my_dir
tar -xf tt.tar -C my_dir
解压 .tar.gz 文件
Bash
tar -xzf tt.tar.gz
解压 .tar.bz2 文件
Bash
tar -xjf tt.tar.bz2
解压 .tar.xz 文件
Bash
tar -xJf tt.tar.xz

参考