LinuxCommandLibrary

ar

Create, modify, and extract archive files

TLDR

E[x]tract all members from an archive

$ ar x [path/to/file.a]
copy

Lis[t] contents in a specific archive
$ ar t [path/to/file.ar]
copy

[r]eplace or add specific files to an archive
$ ar r [path/to/file.deb] [path/to/debian-binary path/to/control.tar.gz path/to/data.tar.xz ...]
copy

In[s]ert an object file index (equivalent to using ranlib)
$ ar s [path/to/file.a]
copy

Create an archive with specific files and an accompanying object file index
$ ar rs [path/to/file.a] [path/to/file1.o path/to/file2.o ...]
copy

SYNOPSIS

ar [options] archive_file [member_files...]
The options typically include one primary action option and zero or more modifier options.
Common primary actions include: d (delete), m (move), p (print), q (quick append), r (replace/insert), t (list), x (extract).

PARAMETERS

d
    Deletes specified members from the archive.

m
    Moves specified members within the archive.

p
    Prints the contents of specified members to standard output.

q
    Quickly appends files to the end of the archive without checking for duplicates.

r
    Replaces or adds files to the archive. Existing members are replaced; new files are added.

s
    Writes an object-file index (symbol table) into the archive. Usually default for object file archives.

t
    Displays a table of contents of the archive, listing its members.

x
    Extracts members from the archive.

a
    Modifier: Place new files after an existing member (requires a position argument).

b
    Modifier: Place new files before an existing member (requires a position argument).

c
    Modifier: Create the archive if it does not already exist.

i
    Modifier: Synonym for 'b'.

u
    Modifier: Only replace files in the archive if they are newer than the existing members.

v
    Modifier: Verbose mode; shows detailed output for each operation.

N
    Modifier: Uses the 'N'th matching member (for archives with multiple members of the same name).

P
    Modifier: Use the full path name to match files (instead of just the filename).

T
    Modifier: Creates a 'thin' archive. Members are symbolic links to their original locations.

S
    Modifier: Do not generate an archive symbol table. Useful for non-object file archives.

V
    Modifier: Display the version number of ar.

DESCRIPTION

ar is a utility program that creates, modifies, and extracts from archives. An archive is a single file containing a collection of other files, often object files or static libraries, along with metadata about them. Unlike other archiving tools like tar, ar does not provide compression capabilities. It is primarily used in software development to manage static libraries (files ending with .a), which are collections of object files that a linker can search to resolve symbol references. ar allows users to add, delete, extract, list, and move members within an archive without needing to unpack and repack the entire archive. Its efficient in-place modification capabilities make it suitable for its niche.

CAVEATS

ar does not provide compression functionality. Its primary role is managing collections of object files for static linking, rather than general-purpose file archiving like tar or zip. While modern GNU ar handles long filenames, older versions or systems might have limitations. It's often used indirectly by build systems rather than directly by end-users.

STATIC LIBRARIES AND BUILD SYSTEMS

ar is crucial for creating static libraries (files ending with .a on Unix-like systems). These libraries bundle together multiple object files (.o) into a single archive. During the linking phase of compilation, the linker (ld) searches these static libraries to resolve external symbol references. This allows developers to reuse code efficiently without recompiling individual source files each time. Modern build systems like Make, CMake, and Autotools often invoke ar automatically as part of the library compilation process, making direct user interaction less common.

HISTORY

The ar utility is one of the oldest standard Unix commands, dating back to the earliest versions of Unix. It was fundamental to the development of C programs, enabling the creation of static libraries which could be linked against multiple programs. Its design reflects the computing environment of the 1970s, where disk space and memory were scarce, making in-place modification of archives desirable. The GNU Binutils project provides its own implementation of ar, which is widely used on Linux systems and is typically more feature-rich than its historical counterparts, supporting features like thin archives and long filenames.

SEE ALSO

ld(1), ranlib(1), nm(1), strip(1), tar(1)

Copied to clipboard