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.
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.
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:
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
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, thenar rcs libname.a *.o, and link withgcc main.c -L. -lname. Thesindex lets the linker find symbols quickly;ar tandnm libname.ashow 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 buildarwith deterministic mode as default) so identical inputs give identical archives. - Inspecting
.debfiles:ar t package.deblistsdebian-binary,control.tar.*anddata.tar.*; extract withar xand unpack the inner tarballs withtar. - Not for backups.
arloses permissions details, directories and symlinks; usetarfor that. - Order matters when linking. Static libraries are searched left to right, so place
-lnameafter the objects that need it.
Related
tar,cpio,strings,file- Lessons: Archives and Compression, Compiling from Source