Old days of Unix to make backups into tape archives tar utility was used. The days past and Linux become popular in UNIX world. But the popularity of tar is never ended. tar is used compress and extract files. We will look usage examples of tar in this tutorial. Tar command is similar to the Windows zip. We can call this for Linux zip folder. There are alternative ways to compress files but we will look tar Linux.
使用Unix的较早时期来将备份备份到磁带存档中,使用tar实用程序。 过去的日子和Linux在UNIX世界中变得流行。 但是tar的普及是永无止境的。 tar用于压缩和提取文件。 我们将在本教程中查看tar的用法示例。 Tar命令类似于Windows zip。 我们可以将其称为Linux zip文件夹。 还有其他压缩文件的方法,但我们将使用tar Linux。
使用Linux tar创建档案 (Create Archive with Linux tar)
Generally used to make multiple files and directories one file. We will tar a directory
通常用于制作多个文件和目录一个文件。 我们将tar目录
$ tar cvf dymerge.tar dymerge
dymerge/
dymerge/txt/
dymerge/txt/archive_formats.txt
dymerge/txt/logo.txt
c for compress but actually it is not compressed
c表示压缩,但实际上未压缩
v for verbosity to see what happens
v表示冗长,以了解发生了什么
f for archive file name
f表示存档文件名
dymerge.tar is new tar file name
dymerge.tar是新的tar文件名
dymerge is the source directory name
dymerge是源目录名称
存档时压缩(Compress While Archiving)
We can compress archived with with z parameter. z is used to gzip format
我们可以使用z参数压缩存档文件。 z用于gzip格式
$ tar cvfz dymerge.tar.gz dymerge
z is for gzip compression other options are default for our usage
z用于gzip压缩,我们默认使用其他选项
To compress with bzip2 j parameter should be provided.
要使用bzip2 j压缩,应提供参数。
列出Tar存档中的文件 (List Files in Tar Archive)
We can list files without opening the tar. t parameter is used to list. But if the archive is gzip we should provide z too. As we see tar.gz is extension for tarred and gzipped files.
我们可以在不打开tar的情况下列出文件。 t参数用于列出。 但是,如果归档文件是gzip,我们也应该提供z 。 如我们所见, tar.gz是压缩文件和压缩文件的扩展名。
$ tar tvfz dymerge.tar.gz
t list files
t清单文件
z archive is gzip format
z档案为gzip格式

If the archive is bzip2 we can use following command
如果存档是bzip2,我们可以使用以下命令
$ tar tvfj dymerge.tar.gz
从存档中提取单个文件 (Extract Single File From Archive)
With tar a single file can be extracted from archive. x is the parameter to be used for this operation
使用tar可以从存档中提取单个文件。 x是用于此操作的参数
$ tar xvfz dymerge.tar.gz dymerge/surnames.txt
dymerge/surnames.txt
dymerge.tar.gz is our archive that contains our single file
dymerge.tar.gz是包含单个文件的存档
dymerge/surnames.txt is the file we want to extract
dymerge / surnames.txt是我们要提取的文件
使用tar从存档中提取多个文件(Extract Multiple Files From Archive with tar)
We can extract multiple files with tar. We need too provide –wildcards parameter and related files for this. Here * is used for globing
我们可以用tar提取多个文件。 我们也需要为此提供–wildcards参数和相关文件。 这里*用于显示
$ tar xvfz dymerge.tar.gz --wildcards *.txt
dymerge/txt/archive_formats.txt
dymerge/txt/logo.txt
dymerge/surnames.txt
dymerge/vbscan/reports/forum.doom9.org/forum.doom9.org_report_2016-10-16_at_4.56.1.txt
dymerge/vbscan/reports/hello.txt
dymerge/vbscan/love.txt
dymerge/vbscan/exploit/vbscandb.txt
dymerge/dymerged.txt
dymerge/names.txt
–wildcards is the parameter
–wildcards是参数
*.txt is the file names we want to exract
* .txt是我们要提取的文件名
解散命令(Untar Command)
We can create some alias to create new untar command like below.
我们可以创建一些别名来创建新的untar命令,如下所示。
$ export alias untar="tar dvf $1"

将文件添加到存档(Adding File into Archive)
After creating an archive we may need to update or add new files and folders to the existing archive. We can use r
option to add new file or folder by specifying file and folder names at the end of the command like below. In this example we will add file named test.txt
into the existing archive named dymerge.tar.gz
创建档案后,我们可能需要更新或向现有档案添加新文件和文件夹。 我们可以使用r
选项通过在命令末尾指定文件和文件夹名称来添加新文件或文件夹,如下所示。 在此示例中,我们将名为test.txt
文件添加到名为dymerge.tar.gz
的现有存档中
$ tar rvfz dymerge.tar.gz test.txt
We append test.txt file with r option
我们用r选项附加test.txt文件
- We can add directories too我们也可以添加目录
估算档案大小(Estimate Archive Size)
Before archiving a directory with tar
command we can learn the size of the archive with the help of wc
command. We will tar the directory in to the starndard output which will be redirected to the wc
command. wc
command will count the bytes and provide estimated size of the given archived file.
在使用tar
命令归档目录之前,我们可以借助wc
命令了解档案的大小。 我们会将目录tar压缩到starndard输出中,该输出将重定向到wc
命令。 wc
命令将计算字节数并提供给定存档文件的估计大小。
$ tar -cf - dymerge | wc -c
573440
Linux tar命令以及如何对目录信息进行Tar (Linux tar Command and How To Tar A Directory Infografic)

翻译自: https://www.poftut.com/linux-tar-command-tutorial-how-to-tar-a-directory/