ar command: Create, modify, and extract Unix archives


ar: Create, modify, and extract Unix archives

ar is a classic Unix tool for working with simple ar archives. You’ll mostly see it used with static libraries (.a) and Debian package archives (.deb). If you’re familiar with tar, ar operates on a more limited, index-based archive format.

Quick start: what ar does

  • Extract all members from an archive:
ar x path/to/file.a
  • List contents in an archive:
ar t path/to/file.ar
  • Replace 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
  • Insert an object file index (like ranlib):
ar s path/to/file.a
  • Create an archive with specific files and an object index:
ar rs path/to/file.a path/to/file1.o path/to/file2.o
ar rs path/to/file.a path/to/file1.o path/to/file2.o

Note: For many modern workflows, you’ll use ar primarily to create or modify static libraries (libsomething.a) and to package components for Debian-style archives. The equivalent workflow for libraries on Linux is to archive object files, then run ranlib (or use ar s alongside rs).

Common tasks

1) Create a static library from object files

ar rs libmylib.a foo.o bar.o
  • rs means replace or add; it creates the archive if it doesn’t exist.
  • If you add more objects later, use the same command to update the archive; you don’t need a separate update step.

2) Update a Debian-like archive with specific components

ar r mypackage.deb debian-binary control.tar.gz data.tar.xz
  • ar can be used to assemble archives, but Debian packaging has a specific format. For real Debian packages you’ll typically use dpkg-deb or debhelper tools, not manual ar steps.

3) List contents to inspect what’s inside

ar t libmya.a
  • Useful before linking to ensure you’re exporting the symbols you expect.

4) Extract files from an archive

ar x libmya.a
  • This extracts all members to the current directory. You can specify individual member names if you know them.

Common pitfalls

  • Mixing formats: ar archives are flat lists of files with an index. They’re not compression-aware in the sense of tar with gzip/bzip2/xz. If your archive is compressed, ar will not compress/decompress it by itself; you’d typically use a separate compression step or a toolchain that supports compressed archives.
  • Not a replacement for tar for general-purpose backups: tar handles path names, multi-volume archives, and compression. Use ar only when you’re specifically dealing with libraries or Debian-style ar archives.
  • File ordering matters when creating libraries: the order of object files can affect link-time behavior in the final executable. Avoid relying on arbitrary order; test builds to confirm symbols are resolved as expected.
  • Running ar s to create an index is mostly for compatibility with older toolchains. Modern linkers may not require a separate index step, but some toolchains still expect it.

Tips and best practices

  • Prefer using the standard toolchain for libraries: compile-object-files → ar rs libfoo.a → ranlib libfoo.a (or ar s and let the toolchain skip ranlib if not needed).
  • If you’re packaging software, use high-level packaging tools (dpkg, debhelper) rather than assembling .deb files with ar by hand.
  • Verify archives after creation: ar t libfoo.a to confirm the expected objects are present.

Quick reference cheat sheet

  • Create or update: ar rs archive.a file1.o file2.o
  • List: ar t archive.a
  • Extract: ar x archive.a
  • Create index: ar s archive.a

If you’re coming from tar, think of ar as a lightweight archive manager tuned for a small, symbol-focused use case: libraries and simple Debian-like bundles. Practice with a small test library (e.g., create a few .o files, run ar rs libtest.a, then ar t libtest.a) to get comfortable with the workflow.

See Also