Linux ar初学者命令教程(5个示例)

Linux ar初学者命令教程(5个示例)

在Linux中,有几个命令行实用程序可用于创建存档。 一个这样的效用是ar。 在本教程中,我们将使用一些易于理解的示例讨论此命令行工具的基础知识。 但在我们这样做之前,值得一提的是文章中包含的所有示例都已经在Ubuntu 18.04 LTS机器上进行了测试。

Linux ar命令

ar命令允许您创建,修改或提取存档。 以下是它的语法:

ar [OPTIONS] archive_name member_files

以下是该手册页中有关此工具的说明:

The GNU ar program creates, modifies, and extracts from archives. An archive is a single file 
holding a collection of other files in a structure that makes it possible to retrieve the original
individual files (called members of the archive).

The original files' contents, mode (permissions), timestamp, owner, and group are preserved in the
archive, and can be restored on extraction.

GNU ar can maintain archives whose members have names of any length; however, depending on how ar is
configured on your system, a limit on member-name length may be imposed for compatibility with
archive formats maintained with other tools.  If it exists, the limit is often 15 characters
(typical of formats related to a.out) or 16 characters (typical of formats related to coff).

ar is considered a binary utility because archives of this sort are most often used as libraries
holding commonly needed subroutines.

ar creates an index to the symbols defined in relocatable object modules in the archive when you
specify the modifiers. Once created, this index is updated in the archive whenever ar makes a change
to its contents (save for the q update operation).  An archive with such an index speeds up linking
to the library, and allows routines in the library to call each other without regard to their
placement in the archive.

以下是一些问答风格的例子,可以让你对ar的工作原理有所了解。

Q1。 如何使用ar创建存档?

这可以使用r命令选项,根据手册页,您可以“替换现有文件或将新文件插入到存档中”。

例如:

ar r test.a *.txt

上面的命令创建一个存档'test.a',其中包含当前目录中的所有txt文件。

Q2。 如何使用ar列出存档的内容?

这可以使用't'命令行选项来完成。 例如,运行以下命令:

ar t test.a

显示存档中包含的所有文件的列表。

Q3。 如何直接显示归档中包含的文件内容?

这可以使用'p'命令选项完成。 这是一个例子:

ar p test.a

这是此命令产生的输出:

因此,您可以看到所有三个文本文件的内容都显示在输出中(因为这些文件是彼此的副本,因此在所有三种情况下内容都相同)。

Q4。 如何添加新成员进行归档?

'r'命令选项也允许您执行此操作。 例如,要将新文本文件 - tes3.txt - 添加到现有存档test.a,我使用以下命令:

ar r test.a test3.txt

Q5。 如何从档案中删除成员?

这也很容易。 只需使用'd'命令选项并指定要删除的成员的名称。

例如,要删除test3.txt,我按以下方式使用ar命令:

ar d test.a test3.txt

以下屏幕截图显示文件已成功删除:

结论

当您想要创建或编辑档案时,ar命令是一个方便的小工具。 它还用于编程以创建程序链接到的静态库。 我们刚刚在这里摸索了一下。 有关该工具的更多信息,请访问其手册页

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏