Introduction

GNU ar can create, modify, extract archive. Usually .a extension is used for denoting ar archives. This sort of archive is used for creating library for holding commonly used subroutines. ar can also create an index for symbol defined in library and stores in archive, which speeds up linking to the library.

Usage

ar has the following signature:

ar [-]OPTION[MODIFIER [MEMBER]] ARCHIVE [MEMBER...]

- is optional for ar, but I prefer to add - in the command for consistency.

Options:

Option Description
r Insert or replace the members in the archive
q Quickly inserts members at the end of archive, doesn’t update symbol table
s Create or update symbol table, can also be used as modifier
t Lists members in the archive
p Prints content of file in archive on standard output
m Moves member order in archive
d Delete members in archive
x Extracts files from archive

Modifier:

Modifier Description
a <MEMBER> Inserts after MEMBER in archive, by default inserts at end
b <MEMBER> Inserts before MEMBER in archive
c Create archive if not exists
o Preserve original date while extraction, else file stamped with extraction time
s Create symbol table of binary object, can be seen using nm -s <archive>
T Creates thin archive. Thin archive uses original file reference instead on copy
u Inserts only if MEMBER is newer then the same name MEMBER in archive
v Verbose mode, prints detailed information
V Prints version

For complete list see man ar.

Example:

For demonstration purpose lets create sample files to work with using following script. This creates 3 files each with one line ‘Line NUM’, where NUM is file number.

for NUM in 1 2 3; do
    echo "Line $NUM" > "file$NUM";
done

Now for creating an archive named pack.a with file1 and file3 in it,

$ ar -r pack.a file1 file3
ar: creating pack.a

This creates pack.a and prints a message in standard error. We can omit this message by adding c modifier.

$ rm pack.a
$ ar -rc pack.a file1 file3

We can view the file names using t option, v modifies can be used for detailed information.

$ ar -t pack.a
file1
file3

$ ar -tv pack.a
rw-r--r-- 0/0     14 Jan  1 06:00 1970 file1
rw-r--r-- 0/0      7 Jan  1 06:00 1970 file3

p option dumps the archive content in standard output, v modifier prints with filename

$ ar -p pack.a
Line 1
Line 3

$ ar -pv pack.a
<file1>

Line 1

<file3>

Line 3

Now if we try to add file3 again in pack.a it will be replaced. But q options doesn’t check for duplication and quickly append at bottom. In some system it also doesn’t update symbol table.

$ ar -r pack.a file3
$ ar -t pack.a
file1
file3

$ ar -q pack.a file3
$ ar -t pack.a
file1
file3
flle3

We can delete using d option

$ ar -d pack.a file3
$ ar -t pack.a
file1
file3

By default r appends new member at bottom. But m option can be used for moving position

$ ar -r pack.a file2
$ ar -t pack.a
file1
file3
file2

# Move after file1
$ ar -ma file1 pack.a file2
$ ar -t pack.a
file1
file2
file3

# Insert file2 before file3
$ ar -d pack.a file2
$ ar -rb file3 pack.a file2

For updating index of symbol table we can use either s option directly or with modifier s while inserting or appending. We can see symbol table with nm -s <archive>. Symbol table is meaningful only for compiled objects. In this article only text files is used for archiving.

$ ar -s pack.a

# Or update table with 'r' or 'q'
$ ar -qs pack.a

For extracting from archive x option is used. o modifier can be used for exacting with original timestamps.

$ rm file1 file2 file3
$ ar -xo pack.a
$ ls
file1 file2 file3

# For extracting specific file(s)
$ rm file2
$ ar -x pack.a file2
$ ls
file1 file2 file3

For creating thin archive T option is used, in this case file reference is used. An archive can either be thin or normal. But can’t be both. If member location or content changes ar produces error.

$ ar -rcT thin.a file1 file2 file3
$ ls
file1 file2 file3 pack.a thin.a

$ rm file1
$ ar -t thin.a
ar: thin.a: Malformed archive

Tools Version

  • GNU ar (GNU Binutils for Ubuntu) 2.30

Bookmarks