linux ar命令_Linux ar命令教程以及归档示例

Linux ar命令用于创建、修改和提取档案,但不进行压缩。它可以将多个文件打包到一个文件中,同时允许档案中存在同名文件。在Ubuntu等Linux发行版上,ar作为开发工具包的一部分预装。使用ar的基本语法包括提供选项和文件参数。ar命令可创建档案、提取所有内容、列出档案内容及其属性,添加新文件到存档,以及从存档中删除文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux ar命令

linux ar命令

Linux ar command is used to create, modify, and extract archives. ar is the short form and first two letters of the archive. ar is provided by most of the Linux distributions like Ubuntu, Debian, Kali, Mint, CentOS, Fedora, Red Hat, and BSD variants.

Linux ar命令用于创建,修改和提取档案。 ararchive的缩写和前两个字母。 大多数Linux发行版(例如Ubuntu,Debian,Kali,Mint,CentOS,Fedora,Red Hat和BSD变体)都提供ar

Ar存档不压缩 (Ar Is Archiving Not Compression)

Archiving is just putting multiple files into a single file without any extra operation. This means archiving do not compress files put into an archive. Archived files sized do not change and stays the same but in compression, the compressed file sizes are decreased. tar is another compression format and command used to archive files but gz, bz , zip , rar are all of them are compression formats, algorithms, and tools.

归档只是将多个文件放入单个文件中,而无需任何额外的操作。 这意味着归档不会压缩放入存档的文件。 归档文件的大小不会更改并且保持不变,但是在压缩状态下,压缩文件的大小会减小。 tar是用于压缩文件的另一种压缩格式和命令,但是gzbzziprar都是压缩格式,算法和工具。

在Ubuntu,Mint,Kali,Debian上安装ar (Install ar On Ubuntu, Mint, Kali, Debian)

ar command is an auxiliary tool for the development and building packages. So it comes with the package named build-essential where we have to install this package to use ar command.

ar命令是用于开发和构建软件包的辅助工具。 因此,它带有一个名为build-essential的软件包,在这里我们必须安装此软件包才能使用ar命令。

$ sudo apt-get install build-essential

ar命令语法 (ar Command Syntax)

ar command has very simple syntax where we provide the option and then file and directory parameters.

ar命令具有非常简单的语法,我们在其中提供选项,然后提供文件和目录参数。

ar OPTION ARCHIVE_NAME FILES
  • OPTION is the ar command option which specifies the action and behavior of the ar command.

    OPTION是ar命令选项,用于指定ar命令的操作和行为。

  • ARCHIVE_NAME is the name of the archive to be created, edited, or listed.

    ARCHIVE_NAME是要创建,编辑或列出的档案的名称。
  • FILES part provides the files which will be used during operation like compress, extract or list.

    FILES部分提供了将在压缩,提取或列表等操作期间使用的文件。

创建档案 (Create An Archive)

We will start with an archive created with the ar command. r option is used to create an archive and we need to also provide the archive name and the files we want to put into an archive. In this example, we will add files named HelloWorld.java, example.txt, fgets.c into a new archive named myfiles.a.

我们将从使用ar命令创建的存档开始。 r选项用于创建档案,我们还需要提供档案名称和我们要放入档案中的文件。 在此示例中,我们将名为HelloWorld.javaexample.txtfgets.c文件添加到名为myfiles.a的新存档中。

$ ar r myfiles.a HelloWorld.java example.txt fgets.c
Create An Archive
Create An Archive
创建档案

档案可以保存多个同名文件(Archives Can Hold Multiple Files with The Same Name)

We have to say that before starting to work on ar command. The archives can store files with the same name in a single archive. As an example, example.txt can be seen multiple times in a single archive file.

我们必须说,在开始使用ar命令之前。 存档可以将具有相同名称的文件存储在单个存档中。 例如,可以在单个存档文件中多次查看example.txt

LEARN MORE  How To Compress Files and Folders In File System Level With Compact Command In Windows NTFS?
了解更多信息如何在Windows NTFS中使用压缩命令在文件系统级别压缩文件和文件夹?

提取存档的所有内容 (Extract All Contents Of Archive)

We can extract all contents of an archive with the x option. We will also use v option in order to print the file names extracted in verbose mode.

我们可以使用x选项提取档案的所有内容。 我们还将使用v选项以打印以详细模式提取的文件名。

$ ar xv myfiles.a
Extract All Contents Of Archive
Extract All Contents Of Archive
提取存档的所有内容

从存档中提取指定的文件(Extract Specified Files From The Archive)

We can also extract single or multiple files from the archive. We will again use the x option but we will also provide the file we want to extract. In this example, we will extract the file named example.txt.

我们还可以从存档中提取单个或多个文件。 我们将再次使用x选项,但我们还将提供要提取的文件。 在此示例中,我们将提取名为example.txt的文件。

$ ar xv myfiles.a example.txt

AND we can also extract multiple files with a single command by adding the file names at the end of the command. In this example, we will extract files named example.txt , fgets.c we can also add more according to our wish.

并且我们还可以通过在命令末尾添加文件名来使用单个命令提取多个文件。 在此示例中,我们将提取名为example.txtfgets.c文件,也可以根据需要添加更多文件。

$ ar xv myfiles.a example.txt fgets.c
Extract Specified Files From The Archive
Extract Specified Files From The Archive
从存档中提取指定的文件

列出档案内容(List Contents Of Archive)

We can list currently existing archive files and their names with the t option. This will only list the names and do not provide extra attributes like timestamps and ownership and access rights.

我们可以使用t选项列出当前存在的存档文件及其名称。 这只会列出名称,不提供额外的属性,例如时间戳,所有权和访问权限。

$ ar t myfiles.a
List Contents Of Archive
List Contents Of Archive
列出档案内容

列出存档内容及其时间戳,大小,所有权和访问权限(List Contents Of Archive with Their Time Stamp, Size, Ownership and Access Rights)

By using t option we only list the archives file names but we can also list the attributes of the archived files with an extra v option. This will list time stamp, size, ownership, access rights.

通过使用t选项,我们仅列出归档文件的名称,但是我们也可以使用额外的v选项列出归档文件的属性。 这将列出时间戳,大小,所有权,访问权限。

$ ar tv myfiles.a
List Contents Of Archive with Their Time Stamp, Size, Ownership and Access Rights
List Contents Of Archive with Their Time Stamp, Size, Ownership and Access Rights
列出存档内容及其时间戳,大小,所有权和访问权限

读取存档文件的内容(Read Contents Of Archived Files)

Generally, ASCII files are archived and used software development. So these source code, configuration files are in ASCII form and can be printed to the screen without extracting/de-archiving them. We can use pv option which will print and display all files contents to the screen and prefix contents with its file name.

通常,ASCII文件被存档并用于软件开发。 因此,这些源代码,配置文件为ASCII格式,可以在不提取/取消存档的情况下将其打印到屏幕上。 我们可以使用pv选项,该选项将在屏幕上显示并显示所有文件内容,并以文件名作为前缀。

$ ar pv myfiles.a
Read Contents Of Archived Files
Read Contents Of Archived Files
读取存档文件的内容

We can see from the output that the first file is HelloWorld.java file which is printed as HelloWorld.java before its content. The second file is example.txtand its content is printed after that.

从输出中我们可以看到第一个文件是HelloWorld.java文件,该文件在其内容之前被打印为HelloWorld.java 。 第二个文件是example.txt然后打印其内容。

LEARN MORE  How To Unzip tar.gz File?
了解更多如何解压缩tar.gz文件?

将新文件添加到现有存档中 (Add New Files Into Existing Archive)

We can also add new files to the existing archive file. In order to add new files, we will use q option. In this example, we will append a file named example.txt.

我们还可以将新文件添加到现有存档文件中。 为了添加新文件,我们将使用q选项。 在此示例中,我们将附加一个名为example.txt的文件。

$ ar qv myfiles.a example.txt

AND we can also add multiple files just by putting them at the end of the command and separating the file names with space. In this example, we will add multiple files names example.txt and fgets.c

并且我们还可以通过将多个文件放在命令末尾并用空格分隔文件名来添加多个文件。 在此示例中,我们将添加多个文件名称example.txtfgets.c

$ ar qv myfiles.a example.txt fgets.c
Add New Files Into Existing Archive
Add New Files Into Existing Archive
将新文件添加到现有存档中

从存档中删除/删除文件(Delete/Remove Files From An Archive)

We can delete or remove files from an existing archive with the d option. In this example, we will delete the file named example.txt from the archive named myfiles.a.

我们可以使用d选项从现有存档中删除文件或从中删除文件。 在此示例中,我们将从名为myfiles.a的存档中删除名为example.txt的文件。

$ ar dv myfiles.a example.txt

AND we can also remove and delete multiple files in a single command by adding them to the end of the command and separating it with spaces.

并且我们还可以通过在单个命令中删除和删除多个文件,方法是将它们添加到命令末尾并用空格分隔。

$ ar dv myfiles.a fgets.c sqlscript.sql
Delete/Remove Files From An Archive
从存档中删除/删除文件

翻译自: https://www.poftut.com/linux-ar-command-tutorial-with-examples-for-archiving/

linux ar命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值