
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
命令用于创建,修改和提取档案。 ar
是archive
的缩写和前两个字母。 大多数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
是用于压缩文件的另一种压缩格式和命令,但是gz
, bz
, zip
和rar
都是压缩格式,算法和工具。
在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.java
, example.txt
, fgets.c
文件添加到名为myfiles.a
的新存档中。
$ ar r myfiles.a HelloWorld.java example.txt fgets.c

档案可以保存多个同名文件(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
。
提取存档的所有内容 (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 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.txt
, fgets.c
文件,也可以根据需要添加更多文件。
$ ar xv myfiles.a example.txt fgets.c

列出档案内容(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 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

读取存档文件的内容(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

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.txt
and its content is printed after that.
从输出中我们可以看到第一个文件是HelloWorld.java
文件,该文件在其内容之前被打印为HelloWorld.java
。 第二个文件是example.txt
然后打印其内容。
将新文件添加到现有存档中 (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.txt
和fgets.c
$ ar qv myfiles.a example.txt fgets.c

从存档中删除/删除文件(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

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