本文共 4866 字,大约阅读时间需要 16 分钟。
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命令压缩和提取文件。
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 filev - Verbosely outputs the creation of the .tar filef - Defines the file name of the 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
样品输出
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
样品输出
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
样品输出
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
标志,如上例所示
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
样品输出
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
样品输出
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
样品输出
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
样品输出
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.
这总结了本教程。 尝试一下,随时留下您的反馈。
翻译自:
linux tar命令压缩
转载地址:http://pnlzd.baihongyu.com/