search
HomeSystem TutorialLINUXEasily learn file packaging and compression under Linux - comprehensive understanding of the tar command
Easily learn file packaging and compression under Linux - comprehensive understanding of the tar commandFeb 12, 2024 pm 09:12 PM
linuxlinux tutoriallinux systemlinux commandshell scriptembeddedlinuxGetting started with linuxlinux learning

In Linux systems, file packaging and compression are extremely common and necessary operations, and the tar command is a powerful tool. Not only can it pack multiple files into one, but it can also compress and decompress files. For Linux newbies, it is very important to be familiar with the tar command, because it will greatly improve our work efficiency. In this article, we will fully understand how to use the tar command, so that you can easily learn file packaging and compression techniques under Linux.

Easily learn file packaging and compression under Linux - comprehensive understanding of the tar command

In short, an archive is a single file that contains a series of files and/or directories. Archive files are typically used for transfer locally or over the Internet, or as a backup copy of a series of files and directories, allowing you to work with a single file (if compressed, its size will be smaller than the sum of all files). Likewise, archives are used for software application packaging. This single file can be easily compressed for transport, while the files in the archive retain their original structure and permissions.

We can use the tar tool to create, list and extract files in the archive. Archives produced with tar are often called "tar files", "tar archives" or "tar packages" (because all archived files are combined into one file).

This tutorial shows how to use tar to create, list, and extract the contents of an archive. All three operations use two common options -f and -v: use -f to specify the name of the archive file, and use the -v ("redundant") option to cause tar to output the file name when processing the file. Although the -v option is not required, it allows you to observe the progress of the tar operation.

In the following parts of this tutorial, 3 topics will be covered: 1. Creating an archive file; 2. Listing the contents of the archive file; 3. Extracting the contents of the archive file. Additionally we will conclude this tutorial by answering 6 practical questions about archive management. What you learn from this tutorial is critical to performing tasks related to cybersecurity and cloud technologies.

1. Create an archive file

To create an archive using tar, use the -c ("create") option, followed by the -f option to specify the name of the archive to be created. Common practice is to use a name with a .tar extension, such as my-backup.tar. Note that all commands and parameters used in the remainder of this article are in lowercase unless otherwise specifically stated. Remember, when entering the commands for this article into your terminal, you do not need to enter the $ prompt at the beginning of each command line.

Enter the name of the file to be archived as a parameter; if you want to create an archive file containing all files and their subdirectories, provide the directory name as a parameter.

To archive the contents of the project directory, enter:

$ tar -cvf project.tar project

This command will create an archive file named project.tar, containing all the contents of the project directory, while the original directory project will remain unchanged.

Use the -z option to compress the archive, which produces the same output as creating an uncompressed archive and then compressing it with gzip, but it eliminates the extra step.

To create a project.tar.gz compressed package from the project directory, enter:

$ tar -zcvf project.tar.gz project

This command will create a project.tar.gz compressed package containing all the contents of the project directory, while the original project directory will remain unchanged.

Note: When using the -z option, you should use the .tar.gz extension instead of the .tar extension to indicate compression. Although not required, it is a good practice.

gzip is not the only form of compression, there are also bzip2 and xz. When we see a file with an extension of .xz, we know that the file is compressed using xz, and a file with an extension of .bz2 is compressed using bzip2. As bzip2 is no longer maintained, we will move away from it and focus on xz. When using xz compression, it takes longer. However, the wait is usually worth it, as the compression is much better, meaning the archive is usually smaller than with other forms of compression. Even better, there's not much difference in decompressing or extracting files between different compression forms. Below we will see an example of how to use xz when compressing a file using tar:

$ tar -Jcvf project.tar.xz project

We just need to convert the -z option of gzip to the uppercase -J of xz. Here is some output showing the differences between the compressed forms:
Easily learn file packaging and compression under Linux - comprehensive understanding of the tar command

Easily learn file packaging and compression under Linux - comprehensive understanding of the tar command

As you can see, xz takes the longest to compress. However, it does the best job of reducing file size, so it's worth the wait. The larger the file, the better the compression.

2. List the contents of archive files

To list the contents of a tar archive without extracting it, use the -t option.

To list the contents of project.tar, enter:

$ tar -tvf project.tar

这个命令列出了 project.tar 归档的内容。-v 和 -t 选项一起使用会输出每个文件的权限和修改时间,以及文件名。这与 ls 命令使用 -l 选项时使用的格式相同。

要列出 project.tar.gz 压缩包的内容,输入:

$ tar -tzvf project.tar.gz

3、从归档中提取内容

要提取(解压)tar 归档文件中的内容,使用 -x(“提取”)选项。

要提取 project.tar 归档的内容,输入:

$ tar -xvf project.tar

这个命令会将 project.tar 归档的内容提取到当前目录。

如果一个归档文件被压缩,通常来说它的扩展名为 .tar.gz 或 .tgz,请包括 “-z” 选项。

要提取 project.tar.gz 压缩包的内容,输入:

$ tar -zxvf project.tar.gz

注意: 如果当前目录中有文件或子目录与归档文件中的内容同名,那么在提取归档文件时,这些文件或子目录将被覆盖。如果你不知道归档中包含哪些文件,请考虑先查看归档文件的内容。

在提取归档内容之前列出其内容的另一个原因是,确定归档中的内容是否包含在目录中。如果没有,而当前目录中包含许多不相关的文件,那么你可能将它们与归档中提取的文件混淆。

要将文件提取到它们自己的目录中,新建一个目录,将归档文件移到该目录,然后你就可以在新目录中提取文件。

FAQ

现在我们已经学习了如何创建归档文件并列出和提取其内容,接下来我们可以继续讨论 Linux 专业人员经常被问到的 9 个实用问题。

可以在不解压缩的情况下添加内容到压缩包中吗?

很不幸,一旦文件将被压缩,就无法向其添加内容。你需要解压缩或提取其内容,然后编辑或添加内容,最后再次压缩文件。如果文件很小,这个过程不会花费很长时间,否则请等待一会。

可以在不解压缩的情况下删除归档文件中的内容吗?

这取决压缩时使用的 tar 版本。较新版本的 tar 支持 -delete 选项。

例如,假设归档文件中有 file1 和 file2,可以使用以下命令将它们从 file.tar 中删除:

$ tar -vf file.tar –delete file1 file2

删除目录 dir1:

$ tar -f file.tar –delete dir1/*

压缩和归档之间有什么区别?

查看归档和压缩之间差异最简单的方法是查看其解压大小。归档文件时,会将多个文件合并为一个。所以,如果我们归档 10 个 100kb 文件,则最终会得到一个 100kb 大小的文件。而如果压缩这些文件,则最终可能得到一个只有几 kb 或接近 100kb 的文件。

如何压缩归档文件?

如上所说,你可以使用带有 cvf 选项的 tar 命令来创建和归档文件。要压缩归档文件,有两个选择:通过压缩程序(例如 gzip)运行归档文件,或在使用 tar 命令时使用压缩选项。最常见的压缩标志 -z 表示 gzip,-j 表示 bzip,-J 表示 xz。例如:

$ gzip file.tar

或者,我们可以在使用 tar 命令时使用压缩标志,以下命令使用 gzip 标志 z:

$ tar -cvzf file.tar /some/directory

如何一次创建多个目录和/或文件的归档?

一次要归档多个文件,这种情况并不少见。一次归档多个文件和目录并不像你想的那么难,你只需要提供多个文件或目录作为 tar 的参数即可:

tar -cvzf file.tar file1 file2 file3

创建归档时如何跳过目录和/或文件?

你可能会遇到这样的情况:要归档一个目录或文件,但不是所有文件,这种情况下可以使用 –exclude 选项:

$ tar –exclude ‘/some/directory’ -cvf file.tar /home/user

在示例中,/home/user 目录中除了 /some/directory 之外都将被归档。将 –exclude 选项放在源和目标之前,并用单引号将要排除的文件或目录引起来,这一点很重要。

本文详细介绍了tar命令的使用方法和参数。通过学习,我们已经掌握了如何使用tar命令来实现文件的打包、压缩和解压缩操作。在日常工作中,熟练掌握这些技巧能够使我们更加高效地管理文件和数据,节约时间和资源。希望本文能够对您有所帮助,也希望大家能够通过实践不断提高自己的技能水平。

The above is the detailed content of Easily learn file packaging and compression under Linux - comprehensive understanding of the tar command. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

什么是linux交叉编译什么是linux交叉编译Apr 29, 2022 pm 06:47 PM

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

linux中rpc是什么意思linux中rpc是什么意思May 07, 2022 pm 04:48 PM

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft