Linux tar命令以及如何对目录进行压缩

本文详细介绍了Linux中的tar命令,包括如何创建归档、压缩文件、列出存档内容以及从存档中提取单个或多个文件。tar命令支持gzip和bzip2压缩格式,例如`tar cvfz`用于创建gzip压缩的归档,而`tar xvfz`用于解压。此外,使用通配符`--wildcards`可以批量提取符合特定模式的文件。
摘要由CSDN通过智能技术生成

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
  • for compress but actually it is not compressed

    c表示压缩,但实际上未压缩

  • for verbosity to see what happens

    v表示冗长,以了解发生了什么

  • 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 parameter. is used to gzip format

我们可以使用z参数压缩存档文件。 z用于gzip格式

$ tar cvfz dymerge.tar.gz dymerge
  • is for gzip compression other options are default for our usage

    z用于gzip压缩,我们默认使用其他选项

To compress with bzip2 parameter should be provided.

要使用bzip2 j压缩,应提供参数。

列出Tar存档中的文件 (List Files in Tar Archive)

We can list files without opening the tar. parameter is used to list. But if the archive is gzip we should provide too. As we see tar.gz is extension for tarred and gzipped files.

我们可以在不打开tar的情况下列出文件。 t参数用于列出。 但是,如果归档文件是gzip,我们也应该提供z 。 如我们所见, tar.gz是压缩文件和压缩文件的扩展名。

$ tar tvfz dymerge.tar.gz
  • list files

    t清单文件

  • archive is gzip format

    z档案为gzip格式

List Files in Tar Archive
List Files in Tar Archive
列出Tar存档中的文件

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. 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是我们要提取的文件

LEARN MORE  Linux gtar Command Tutorial with Examples and Compare with Tar
通过示例了解更多Linux gtar命令教程,并与Tar进行比较

使用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"
Untar Command
Untar Command
解散命令

将文件添加到存档(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.txtinto 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 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)

Linux tar Command and How To Tar A Directory Infografic

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值