search
How to use linux tar commandFeb 01, 2023 pm 06:21 PM
linux

In Linux, the tar command can save many files together to a separate tape or disk for archiving. The syntax is "tar [option] source file or directory"; it can also restore the required files from the archive file. File, which is the reverse process of packaging, is called unpacking, the syntax is "tar [option] compressed package"; it can also be packaged and compressed at the same time, the syntax is "tar [option] compressed package source file or directory".

How to use linux tar command

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

Linux tar packaging command detailed explanation

In the Linux system, the most commonly used archiving (packaging) command is tar, which can save many files together into a single Archive to tape or disk. Not only that, this command can also restore the required files from the archive, which is the reverse process of packaging, called unpacking.

Packages archived using the tar command are usually called tar packages (tar package files all end with ".tar").

The tar command performs packaging operations

When the tar command is used for packaging operations, the basic format of the command is:

[root@localhost ~]#tar [选项] 源文件或目录

The commonly used options of this command and their respective meanings are shown in Table 1.

## -cPackage multiple files or directories. -AAppend the tar file to the archive. -f package nameSpecifies the file name of the package. The extension of the package is used to identify the format for the administrator, so the extension must be specified correctly; -vDisplays the process of packaging files;
Table 1 tar packaging command common options and their meaning
Options Meaning
It should be noted that when using the tar command to specify options, you do not need to enter "-" in front of the options. For example, using the "cvf" option has the same effect as "-cvf".

Here are a few examples to show you how to use the tar command to package files and directories.

Example 1: Packing files and directories

[root@localhost ~]# tar -cvf anaconda-ks.cfg.tar anaconda-ks.cfg

How to use linux tar command

The option "-cvf" is generally used, remember to use it when packaging Specify the file name after packaging, and use ".tar" as the extension. The same is true for packaging directories:

How to use linux tar command

Example 2: Pack and compress directories

First of all, let’s make it clear that the compression command cannot directly compress directories. You must first use the tar command to package the directory, and then use the gzip command or the bzip2 command to compress the packaged file. For example:

[root@localhost ~]#ll -d test test.tar
drwxr-xr-x 2 root root 4096 6月 17 21:09 test
-rw-r--r-- 1 root root 10240 6月 18 01:06 test.tar
#我们之前已经把test目录打包成test.tar文件
[root@localhost ~]# gzip test.tar
[root@localhost ~]# ll test.tar.gz
-rw-r--r-- 1 root root 176 6月 18 01:06 test.tar.gz
#gzip命令会把test.tar压缩成test.tar.gz

The tar command is used to unpack the tar package

When the tar command is used to unpack the tar package, the The basic format is as follows:

[root@localhost ~]#tar [选项] 压缩包

When used to unpack, the commonly used options and meanings are shown in Table 2.


Table 2 tar unpacking common options and their meaningsOptionsMeaning-xUnpack the tar package. -fSpecify the package name of the tar package to be decompressed. -tOnly checks which files or directories are in the tar package and does not unpack the tar package. -C DirectorySpecifies the unpacking location. -vDisplay the specific process of unpacking.

其实解打包和打包相比,只是把打包选项 "-cvf" 更换为 "-xvf"。我们来试试:

[root@localhost ~]# tar -xvf anaconda-ks.cfg. tar
#解打包到当前目录下

如果使用 "-xvf" 选项,则会把包中的文件解压到当前目录下。如果想要指定解压位置,则需要使用 "-C(大写)" 选项。例如:

[root@localhost ~]# tar -xvf test.tar -C /tmp
#把文件包test.tar解打包到/tmp/目录下

如果只想查看文件包中有哪些文件,则可以把解打包选项 "-x" 更换为测试选项 "-t"。例如:

[root@localhost ~]# tar -tvf test.tar

How to use linux tar command

tar命令做打包压缩(解压缩解打包)操作

你可能会觉得 Linux 实在太不智能了,一个打包压缩,居然还要先打包成 ".tar" 格式,再压缩成 ".tar.gz" 或 ".tar.bz2" 格式。其实 tar 命令是可以同时打包压缩的,前面的讲解之所打包和压缩分开,是为了让大家了解在 Linux 中打包和压缩的不同。

当 tar 命令同时做打包压缩的操作时,其基本格式如下:

[root@localhost ~]#tar [选项] 压缩包 源文件或目录

此处常用的选项有以下 2 个,分别是:

  • -z:压缩和解压缩 ".tar.gz" 格式;

  • -j:压缩和解压缩 ".tar.bz2"格式。

示例1:压缩与解压缩 ".tar.gz"格式

[root@localhost ~]# tar -zcvf tmp.tar.gz /tmp/
#把/temp/目录直接打包压缩为".tar.gz"格式,通过"-z"来识别格式,"-cvf"和打包选项一致

解压缩也只是在解打包选项 "-xvf" 前面加了一个 "-z" 选项。

[root@localhost ~]# tar -zxvf tmp.tar.gz
#解压缩与解打包".tar.gz"格式

前面讲的选项 "-C" 用于指定解压位置、"-t" 用于查看压缩包内容,在这里同样适用。

示例2:压缩与解压缩 ".tar.bz2" 格式

和".tar.gz"格式唯一的不同就是"-zcvf"选项换成了 "-jcvf",如下所示:

[root@localhost ~]# tar -jcvf tmp.tar.bz2 /tmp/
#打包压缩为".tar.bz2"格式,注意压缩包文件名
[root@localhost ~]# tar -jxvf tmp.tar.bz2
#解压缩与解打包".tar.bz2"格式

把文件直接压缩成".tar.gz"和".tar.bz2"格式,才是 Linux 中最常用的压缩方式,这是大家一定要掌握的压缩和解压缩方法。

tar 命令最初被用来在磁带上创建备份,现在可以在任何设备上创建备份。利用 tar 命令可以把一大堆的文件和目录打包成一个文件,这对于备份文件或是将几个文件组合成为一个文件进行网络传输是非常有用的。

相关推荐:《Linux视频教程

The above is the detailed content of How to use linux tar command. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
什么是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交叉编译什么是linux交叉编译Apr 29, 2022 pm 06:47 PM

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

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

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

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
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.