linux tar命令压缩_Linux tar命令来压缩和提取文件

linux tar命令压缩

In this guide, we look at the Linux Tar command, Tar, short for Tape Archive, is a commonly used command that is used for created compressed files, known as tarball files which are easily portable from one disk to another. Furthermore, the command can be used to uncompress these archived files and further make some modifications. In this guide, we will dive and see tar command examples and see how to compress and extract files using the tar command.

在本指南中,我们介绍Linux Tar命令,Tar是Tape Archive的缩写,它是用于创建压缩文件(称为tarball文件)的常用命令,该文件可以轻松地从一个磁盘移植到另一个磁盘。 此外,该命令可用于解压缩这些归档文件并进一步进行一些修改。 在本指南中,我们将深入探讨tar命令示例,并了解如何使用tar命令压缩和提取文件。

创建一个Tar存档文件 (Creating a Tar Archive file)

To create a tar archive of a directory , use the syntax below

要创建目录的tar归档文件,请使用以下语法

# tar -cvf tarball_name.tar  /path/to/directory

For example, the command below creates a tarball called james.tar from the home directory /home/james

例如,以下命令从主目录/home/james创建一个名为james.tar的tarball。

# tar -cvf james.tar  /home/james/

Sample output

样品输出

Let’s take a look at the options

让我们看一下选项

c - Used for creating a new .tar file
v - Verbosely outputs the creation of the .tar file
f - Defines the file name of the archive file

创建一个tar.gz存档文件 (Creating a tar.gz archive file)

To create a tar.gz file , use the -z option. the command below creates a tar.gz tarball from the /home/james/ directory as shown.

要创建tar.gz文件,请使用-z选项。 下面的命令从/home/james/目录创建一个tar.gz tarball,如下所示。

# tar -cvzf james.tar.gz  /home/james/

Sample output

样品输出

创建一个tar.bz2存档文件 (Creating a tar.bz2 archive file)

The bz2 option is used to compress a more highly compressed tar file which is of lesser file size compared to gzip compression. However, it takes a longer time to achieve compression. To create a bz2 archive , use the -j option as shown in the example below

bz2选项用于压缩更高压缩的tar文件,与gzip压缩相比,该文件的大小较小。 但是,需要更长的时间才能实现压缩。 要创建bz2存档,请使用-j选项,如下例所示

# tar -cvjf james.tar.bz2  /home/james/

OR

要么

# tar -cvjf james.tar.tbz  /home/james/

OR

要么

# tar -cvjf james.tar.tb2  /home/james/

Sample output

样品输出

Untar tar存档文件 (Untar tar archive file)

To uncompress or untar a .tar file , use the x option for extracting as shown

要解压缩或解压缩.tar文件,请使用x选项进行解压缩,如下所示

# tar -xvf james.tar

Sample output

样品输出

To extract the file to a different directory, use the -C to specify the path to the directory

要将文件提取到其他目录,请使用-C指定目录的路径

# tar -xvf james.tar  -C /opt

Sample output

样品输出

解压缩tar.gz文件 (Uncompressing a tar.gz file)

To extract or uncompress a tar.gz file, use the -x option as shown

要解压缩或解压缩tar.gz文件,请使用-x选项,如图所示

# tar -xvf james.tar.gz

Sample output

样品输出

To extract it to a different directory, use the -C flag as shown in the previous example

要将其解压缩到其他目录,请使用-C标志,如上例所示

解压缩tar.bz2文件 (Uncompressing a tar.bz2 file)

Again the command for extracting the tar.bz2 takes the same approach as the 2 previous examples.

同样,提取tar.bz2的命令采用与前面两个示例相同的方法。

# tar -xvf james.tar.bz2

Sample output

样品输出

列出存档文件的内容 (Listing contents of an archive file)

if you want to list contents of a file before extracting the archive , use the -t option

如果要在提取归档文件之前列出文件的内容,请使用-t选项

To list contents of a tar file, run the command

要列出tar文件的内容,请运行以下命令

# tar -tvf james.tar

Sample output

样品输出

To list contents of a tar.bz2 file execute

要列出tar.bz2文件的内容,请执行

# tar -tvf james.tar.bz2

Sample output

样品输出

Likewise, for a tar.gz file, the command will be

同样,对于tar.gz文件,该命令将为

# tar -tvf james.tar.gz

Sample output

样品输出

将文件或目录添加到Tar归档文件 (Adding a file or directory to a Tar archive)

To add a file or a directory to a pre-existing archive, use the -r option as shown

要将文件或目录添加到预先存在的存档中,请使用-r选项,如下所示

# tar -rvf root.tar install.sh

In the above example, we have added the file install.sh to the tarball root.tar

在上面的示例中,我们将文件install.sh添加到tarball root.tar

Sample output

样品输出

To add a directory, use the same syntax. in the example below, we are adding the directory data to the tarball root.tar

要添加目录,请使用相同的语法。 在下面的示例中,我们将目录data添加到tarball root.tar

# tar -rvf root.tar data

Sample output

样品输出

将文件或目录添加到tar.gz或tar.bz2存档 (Adding files or directories to tar.gz or tar.bz2 archive)

Similarly, to add a file or directory in a tar.gz or tar.bz2 file, use the -r option as shown

同样,要在tar.gz或tar.bz2文件中添加文件或目录,请使用-r选项,如下所示

# tar -rvf root.tar.gz install.sh

Sample output

样品输出

Likewise, for a tar.bz2 file, you will execute

同样,对于tar.bz2文件,您将执行

# tar -rvf root.tar.bz2 install.sh

Sample output

样品输出

检查tar,tar.gz和tar.bz2文件的大小 (Check the size of a tar , tar.gz and tar.bz2 files)

To check the size of your tarball file, use the syntax as shown below

要检查tarball文件的大小,请使用如下所示的语法

# tar -czf root.tar | wc -c

For a tar.gz file, this will be

对于tar.gz文件,这将是

# tar -czf root.tar.gz | wc -c

And finally, for a tar.bz2 file, run

最后,对于tar.bz2文件,运行

# tar -czf root.tar.bz2 | wc -c

Sample output

样品输出

This wraps up this tutorial. Give it a try and feel free to leave your feedback.

这总结了本教程。 尝试一下,随时留下您的反馈。

翻译自: https://www.journaldev.com/28442/linux-tar-command

linux tar命令压缩

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值