Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Archives and Compression

Linux ar command

Reference for the Linux ar command: create and modify archives, list and extract members, build static libraries (.a files), and make deterministic archives.

ar creates and edits archives: single files that hold other files, called members. It is the tool behind static libraries (libfoo.a) and .deb packages (which are ar archives). Unlike tar, it does not compress and does not store directories, only a flat list of files, but it can keep a symbol index that the linker uses.

The command letter comes first, then modifier letters, then the archive name and members, often written without a leading dash: ar rcs libx.a a.o b.o.

Terminal
ar rcs libutil.a util.o math.o     # create or update, with an index
ar t libutil.a                     # list members
ar x libutil.a                     # extract all

Try it

We use text files as members so the example does not need a compiler; the commands are identical for object files. The D modifier stores zero timestamps and owners, which makes the listing reproducible.

Bash Create, list, read and change an archive

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Replacing a member keeps the archive in place:

Bash Update and append members

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
ar [-]OPERATION[MODIFIERS] ARCHIVE [MEMBER...]
Operation Description
r Insert members; replace ones with the same name.
q Quick append without checking for duplicates.
d Delete members.
t List member names (tv for details).
x Extract members (all if none are named).
p Print members to standard output.
s Write or refresh the symbol index (same as running ranlib).
m Move members within the archive.
Modifier Description
c Create the archive without the "creating" warning.
s Add the symbol index while writing.
v Verbose.
D Deterministic: zero timestamps, UID and GID.
u With r, replace only if the file is newer.

Returns: 0 on success; non-zero on a missing archive, missing member or format problem.

Usage notes

  • Static libraries: compile with gcc -c *.c, then ar rcs libname.a *.o, and link with gcc main.c -L. -lname. The s index lets the linker find symbols quickly; ar t and nm libname.a show what is inside.
  • No directories. Members are stored by base name only; two files with the same name overwrite each other.
  • Names longer than 15 characters are stored using GNU's extended-name table; they still list fine with ar t.
  • Reproducible builds: use D (or build ar with deterministic mode as default) so identical inputs give identical archives.
  • Inspecting .deb files: ar t package.deb lists debian-binary, control.tar.* and data.tar.*; extract with ar x and unpack the inner tarballs with tar.
  • Not for backups. ar loses permissions details, directories and symlinks; use tar for that.
  • Order matters when linking. Static libraries are searched left to right, so place -lname after the objects that need it.