Brief: This article guide demonstrates the usage of the Linux ar command to create, modify, and extract from archives.
Linux provides several useful command-line utilities for managing archives. The ar command-line tool is among these utilities, which create, modify, and extract from archives.
An archive is a single file format that holds a collection of other files within the archive that are referred to as members. The structure of the archive file makes it possible to retrieve the original files prior to their compression into the archived state.
The archive preserves the original file attributes such as owner, group, timestamp, and mode (permissions).
ar Command Features
The ar command archive management utility is associated with the following popular features:
- Can accommodate archived (members’ names) of considerable length with the option of imposing a member-name length limit.
- Since the ar-type archives can be used as libraries, the ar command-line utility is considered binary.
- Can record a library’s dependencies since libraries tend to depend on each other.
- Allows the optional creation of a thin archive. This type of archive uses a symbol index to reference the archived member files’ original copies.
- Supports archives that are either thin or normal, but not a combination of both.
- An existing archive’s format can only be changed via its deletion and recreation of a new one.
How to Use ar Command in Linux
Archives associated with the ar utility have a
'.a' file extension e.g
my_archive.a. Here, the ar utility handles tasks related to creating, modifying, and extracting member content from archives are discussed below:
The standard syntax for the usage of ar command is as follows:
$ ar [OPTIONS] archive_name.a member_file(s)
Here is a summary of common [OPTIONS] associated with ar command:
-d– Deletes archived member files.
-t– Prints the archive’s table of contents.
-x– Extracts files from an existing archive.
-r– Creates and/or adds member files to the end of the archive.
-v– Verbose mode, prints the names of the files being processed.
Creating an Archive File
r command option is used to create an ar archive with a
'.a' file extension by specifying the member files to be archived. If the targeted member file does not exist, an error is thrown.
The following command creates the ar archive
files.a with member files marketed.png and Invoice.pdf.
$ ar r files.a marketed.png Invoice.pdf
We can implement the verbose mode
(-v) in the following command for an interactive ar archiving process.
$ ar -vr backup.a text3.txt text4.txt
Listing Contents of an Archive File
t command option is used to list an archive’s content (member files) by reading the entire archive.
$ ar t files.a
For more details on the listed member files like an owner, group, timestamp, and permission, we can combine the above command with the verbose option.
$ ar tv backup.a
Displaying Contents of an Archive
Here, we use the
p command option to print the content of each or all member files as standard output. We have to specify the targeted member file(s) as demonstrated below:
$ ar p backup.a text3.txt $ ar p backup.a text3.txt text4.txt
We can also include the verbose option to display the member’s name before its content is printed.
$ ar pv backup.a text3.txt $ ar pv backup.a text3.txt text4.txt $ ar pv backup.a
Add New Member to Archive
Here, we reuse the
r command option and specify the filename(s) to be added to the existing ar archive.
$ ar -r backup.a text5.txt text6.txt
We can also combine ar command with the verbose option to view the underlying archiving process.
$ ar -rv backup.a text7.txt text8.txt
Extracting Archive Files
Use the command option
x. Here, we can specify the name of the member file to extract. If we do not specify a targeted member file, the whole archive will be extracted. It is also ideal to combine this command with the verbose option for an interactive output.
$ ar xv backup.a text3.txt text4.txt $ ar xv backup.a
Deleting an Archive Member/File
Use command option
d to delete member files of an existing archive. You need to specify the targeted file(s) for deletion to not compromise the entire archive. We can combine this command with the verbose option to confirm the deletion of the specified member file.
$ ar dv backup.a text3.txt text4.txt
Creating Archive Index (Symbol Table)
Use the command option
s to create an archive index.
$ ar s files.a
In the upcoming tutorial segment, we will cover static vs dynamic libraries and how to practically create a static library via the Linux ar Command.