Linux命令 - tar

tar命令用于创建归档文件(压缩文件)。

Reference: Tar Command in Linux (Create and Extract Archives) | Linuxize.

What is tar?

The tar command creates tar files by converting a group of files into an archive. It also can extract tar archives, display a list of the files included in the archive, add additional files to an existing archive, and various other kinds of operations.

Tar was originally designed for creating archives to store files on magnetic tape, which is why it has its name “Tape ARchive”.

Syntax:

1
tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME(s)]
  • OPERATION - Only one operation argument is allowed and required. The most frequently used operations are:

    • --create (-c) - Create a new tar archive.
    • --extract (-x) - Extract the entire archive or one or more files from an archive.
    • --list (-t) - Display a list of the files included in the archive
  • OPTIONS

    - The most frequently used operations are:

    • --verbose (-v) - Show the files being processed by the tar command.
    • --file=archive=name (-f archive-name) - Specifies the archive file name.
  • ARCHIVE_NAME - The name of the archive.

  • FILE_NAME(s) - A space-separated list of filenames to be extracted from the archive. If not provided, the entire archive is extracted.

1. 创建压缩文件

创建tar压缩文件

To create a tar archive, use the -c option followed by -f and the name of the archive.

-f (--file=ARCHIVE) - use archive file or device ARCHIVE

‘-c’为创建tar压缩文件的选项;’-f’选项后紧跟着压缩文件的名称,该选项表明会使用指定名称的压缩文件。因此’-cf’合起来就是创建指定名称的压缩文件。

1
$ tar -cf archive.tar file1 file2 file3

创建tar.gz(tgz)压缩文件

Gzip is the most popular algorithm for compressing tar files. When compressing tar archives with gzip, the archive name should end with either tar.gz or tgz.

-z - tells tar to compress the archive using the gzip algorithm as it is created.

1
$ tar -czf archive.tar.gz file1 file2

保留链接文件数据

-h (--dereference) - follow symlinks; archive and dump the files they point to

跟随链接文件,并且归档和转存这些链接文件指向的内容。

2. 列出压缩文件内容

-t (--list) - the tar command lists the content of a tar archive without extracting it

1
$ tar -tf archive.tar

解压

解压到当前目录

1
2
3
4
# To extract a tar archive, use the --extract (-x) option followed by the archive name:
$ tar -xf archive.tar
# It is also common to add the -v option to print the names of the files being extracted:
$ tar -xvf archive.tar

解压到指定目录

-c (--directory) - to extract archive files in a specific directory

1
$ tar -xf archive.tar -C /opt/files

解压指定文件

When extracting files, you must provide their exact names, including the path, as printed by --list (-t).

1
2
3
4
# To extract a specific file(s) from a tar archive, append a space-separated list of file names to be extracted after the archive name:
$ tar -xf archive.tar file1 file2
# Extracting one or more directories from an archive is the same as extracting files:
tar -xf archive.tar dir1 dir2

添加文件到压缩文件

-r (--append) - add files or directories to an existing tar archive

1
$ tar -rvf archive.tar newfile

无法在不解压的情况下添加或更新文件到tar.gz(tgz)文件中:linux - Is there a way to add or update file in tar.gz(tgz) without decompress? - Stack Overflow

从压缩文件中删除文件

--delete - to remove files from an archive.

1
$ tar --delete -f archive.tar file1

压缩时排除指定文件

在压缩过程中排除指定数据不要被压缩

1
tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME(s)] --exclude-from=[FILE]

方法一

1
2
3
4
5
6
7
8
9
# [FILE]: 含有排除列表的文本路径(Exclude pattern lists in FILE)
$ tar zcvf [ARCHIVE_NAME] [FILE_NAME(s)] --exclude-from=[FILE]
# [FILE_NAME(s)]和[PATTERN]必须均为绝对路径或相对路径(这是为了防止"--exclude-from"失效).而[ARCHIVE_NAME]为绝对路径或相对路径均可
$ tar zcvf /tmp/test.tar.gz /test --exclude-from=~/exclude.txt
$ tar zcvf /tmp/test.tar.gz ./test --exclude-from=./exclude.txt
# 而exclude.txt内容如下
/tmp/file1.txt
/tmp/file2.txt
# 则压缩时会排除这两个文件

方法二

1
tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME(s)] --exclude=[PATTERN]
1
2
3
4
5
6
7
# [PATTERN]: 排除列表
$ tar zcvf [ARCHIVE_NAME] [FILE_NAME(s)] --exclude=[PATTERN]
# [FILE_NAME(s)]和[PATTERN]必须均为绝对路径或相对路径(这是为了防止"--exclude"失效).而[ARCHIVE_NAME]为绝对路径或相对路径均可
$ tar zcvf /tmp/test.tar.gz /test --exclude=/test/t.txt
$ tar zcvf /tmp/test.txt.tar.gz ./test --exclude=./test/t.txt
# [PATTERN]可以是多个
$ tar zcvf /tmp/test.txt.tar.gz ./test --exclude=./test/t.txt ./test/t2.txt ./test/t3.txt