ar Command in Linux



The ar command is in Linux systems to create, modify, and extract files from an archive file. The archive file on the other hand is a collection of one or more files that are bundled together into a single file. This type of file is a compressed version of different files arranged together in a group for easy management and to reduce the space on the system.

If you are searching for help regarding the use of ar command on Linux systems, follow this tutorial for a detailed overview. Here, you will get to know about the following −

Table of Contents

How to Install ar Command in Linux?

The ar command typically comes preinstalled on most Debian-based operating systems, like Ubuntu, Debian, Kali Linux, and Linux Mint. However, if it is not available, simply execute the below-given command −

sudo apt install binutils

The binutils package includes the ar command line utility.

On CentOS and similar Red Hat-based systems, the ar command is part of the binutils package, which you can install from the following command −

sudo yum install binutils

The Arch Linux users can install ar command on their system if it is not available by using the below-given command −

sudo pacman -S binutils

For Alpine Linux, the binutils package can be installed using the following command −

apk add binutils

If you are using Fedora OS, the following command will be used for the installation of ar command in case it is not available on your system −

sudo dnf install binutils

Syntax for ar Command in Linux

The basic syntax to use the ar command in Linux is provided below −

ar [OPTIONS] archive_name member_files

Where, archive_name is the name of the archive file, while member_files are the individual files that you want to include in the archive.

Options Available for ar Command

With ar command, you can use several options, some of them are described below in the table −

Option Description
r With this option, you can create an archive and insert files into it. It throws an error if the member file doesn’t exist and adds a new member at the end of the archive by default.
d This option deletes specific members from the archive. You specify the name of the members or modules you want to delete. If you use the v option with d, the ar will list each module as it is deleted.
p This option prints the content of the specified members or modules to standard output. Without a modifier, it directly prints the member contents. When using the v option, you will be able to see the member name before copying it to the output file.
t This option allows you to see the content of the archive file in a listed manner. When you use the o option with it, you will get the corresponding offset of each member.
x This option is used to extract each named member from an archive file. If you don’t specify the name, it will extract the entire archive. If you use the v modifier with x, you will get the list of the names of extracted files.
m Allows you to move members in a file.
q Quickly appends files to the end of the archive without checking for duplicates.
s Adds or updates the archive’s symbol table.
a Inserts the files after a specific member in the archive.
b Inserts the files before a specific member (same as -i)
c Creates the archive without displaying a message.
i Inserts the files before a specific member (same as -b).
l Uses a long format for filenames in the archive.
u Updates files in the archive if they have changed since being archived.
o Preserves the original dates when extracting files.
v Operates verbosely, showing additional information.
V Displays the version of ar.

Examples of ar Command in Linux

Now, let’s go through some examples to better understand the working of the ar command in Linux.

Creating a New Archive

To create a new archive file on a Linux system, you can use the ar command with the rcs flags. The following example illustrates the creation of an archive named libexample.a, which includes file1.o and file2.o

ar rcs libexample.a file1.o file2.o
ar Command Linux 1

Here in the above example, we use the ar command to extract, build, and modify archives. While the rcs option includes 3 flags associated with the ar command, each flag shows different behavior, which are discussed below −

  • r − This option will append the specified files to the archive; if the archive does not exist, it will be created.
  • c − It creates an archive; it has no effect if the archive already exists.
  • s − This option creates an index (symbol table) for the archive, speeding up access to the files within the archive.

Further, libexample.a is the name of the archive file we created, this file will be different in your case.

file1.o file2.o are object files to put in the archive; ensure the files are placed at the current location.

Viewing the Contents of an Archive

To display the contents of an existing archive on a Linux system in a listed manner, employ the ar command with the t flag. The example given below demonstrates how to list the files contained within the archive libexample.a

ar t libexample.a 
ar Command Linux 2

In the above example −

  • ar − The archiver command.
  • t − Lists the table of contents of the archive.
  • libexample.a − The name of the archive file.

In the above command, t displays the lists of the files contained in the archive without extracting or modifying them.

If you want to print the contents of the specified files within an archive to the standard output, you can use the p option. While an additional v option can be used to display the name of each file before its content. The output generated by the pv option will be file names enclosed in the angular brackets to visually separate the file name with its content. The following example illustrates the process −

ar pv libexample.a
ar Command Linux 3

Extracting Files from an Archive

To extract all files from an archive on a Linux system, you can use the x option with the ar command. Let’s explore the following example that extracts all files from libexample.a archive −

ar x libexample.a
ar Command Linux 4

For extracting a file according to your choice, simply type the file name at the end to force the command to extract only the desired file from the archive. For example, the following command will extract a single file called file1.o from the archive −

ar x libexample.a file1.o

The x option in the above command will extract files from the archive. Ensure specific file names to extract only certain files from the system.

Deleting a File from an Archive

For any reason, if you want to delete a file from an archive, you can use the d option. An example of such a process is given below −

Here, in the below-given example, we delete a file file1.o from libexample.a archive file using the following command −

ar d libexample.a file1.o 
ar Command Linux 5

Ensure the file you want to delete must be there in your desired archive file. The d option is used to remove a specified file from the archive. Multiple files can be specified for deletion by adding the file names separated by a space.

Updating Files in an Archive

In case you want to update files in your archive file, you can use the r option. This will allow you to add files to a pre-existing archive file without damaging the file. The following example will add a file named file2.o in libexample.a archive file and update it −

ar r libexample.a file2.o
ar Command Linux 6

Here, you can replace the file names according to your choice, but ensure these files are present at the current location. Also, to add multiple files, use the file names separated by a space. The r option will help you add or update files in the archive; if the file exists, it is replaced; if not, it is added.

Conclusion

ar command is an effective command that you can use to work on the libraries and manage your archive on your Linux system. This guide has explored the basic syntax of ar command on Linux, along with options you can use with the command. Apart from that, it has also discussed different examples that will help users learn the basics of ar command and use it to handle their files in a better way.

Advertisements