Linux ar Command Tutorial for Beginners (5 Examples)

In Linux, there are several command-line utilities that let you create archives. One such utility is ar. In this tutorial, we will discuss the basics of this command line tool using some easy to understand examples. But before we do that, it's worth mentioning that all examples included in the article have been tested on an Ubuntu 18.04 LTS machine.

Linux ar command

The ar command allows you to create, modify, or extract archives. Following is its syntax:

ar [OPTIONS] archive_name member_files

And here's what the man page says about this tool:

The GNU ar program creates, modifies, and extracts from archives. An archive is a single file 
holding a collection of other files in a structure that makes it possible to retrieve the original
individual files (called members of the archive).

The original files' contents, mode (permissions), timestamp, owner, and group are preserved in the
archive, and can be restored on extraction.

GNU ar can maintain archives whose members have names of any length; however, depending on how ar is
configured on your system, a limit on member-name length may be imposed for compatibility with
archive formats maintained with other tools.  If it exists, the limit is often 15 characters
(typical of formats related to a.out) or 16 characters (typical of formats related to coff).

ar is considered a binary utility because archives of this sort are most often used as libraries
holding commonly needed subroutines.

ar creates an index to the symbols defined in relocatable object modules in the archive when you
specify the modifiers. Once created, this index is updated in the archive whenever ar makes a change
to its contents (save for the q update operation).  An archive with such an index speeds up linking
to the library, and allows routines in the library to call each other without regard to their
placement in the archive.

Following are some Q&A styled examples that should give you a good idea on how ar works.

Q1. How to create an archive using ar?

This you can do using the r command option, which according to the man page lets you "replace existing or insert new file(s) into the archive."

So for example:

ar r test.a *.txt

The above command creates an archive 'test.a' that contains all the txt files from the current directory.

Q2. How to list contents of archive using ar?

This can be done using the 't' command line option. So for example, running the following command:

ar t test.a

displayed a list of all files included in the archive.

How to list contents of archive using ar

Q3. How to directly display contents of files included in archive?

This can be done using the 'p' command option. Here's an example:

ar p test.a

Here's the output produced by this command:

How to directly display contents of files included in archive

So you can see the content of all three text files was displayed in output (since these files were copies of each other, so content was same in all three cases).

Q4. How to add a new member to archive?

The 'r' command option lets you do this as well. For example, to add a new text file - tes3.txt - to existing archive test.a, I used the following command:

ar r test.a test3.txt

How to add a new member to archive

Q5. How to delete a member from archive?

That's easy as well. Just use the 'd' command option and specify the name of the member to delete.

For example, to delete test3.txt, I used the ar command in the following way:

ar d test.a test3.txt

The following screenshot shows the file was successfully deleted:

How to delete a member from archive

Conclusion

The ar command is a handy little tool for when you want to create or edit archives. It's also used in programming to create static libraries that programs link to. We've just scratched the surface here. For more info on the tool, head to its man page.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

Suggested articles

0 Comment(s)

Add comment