ar – Create or modify (including extracting) an archive

The ar command is a Linux utility that is used to create, modify, or extract files from an archive. It is commonly used to create static libraries for object files, but can also be used to create archives of other types of files.

Overview

The basic syntax for the ar command is as follows:

ar [options] archive files

Here, archive is the name of the archive file that you want to create or modify, and files are the files that you want to add to or extract from the archive. The options parameter is optional and can be used to modify the behavior of the ar command.

Creating an archive

To create an archive, use the r option followed by the name of the archive file and the files that you want to add to the archive. For example:

ar r myarchive.a file1.o file2.o file3.o

This will create an archive called myarchive.a and add the files file1.o, file2.o, and file3.o to it.

Modifying an archive

To modify an existing archive, use the r option followed by the name of the archive file and the files that you want to add to the archive. For example:

ar r myarchive.a newfile.o

This will add the file newfile.o to the existing archive myarchive.a.

Extracting files from an archive

To extract files from an archive, use the x option followed by the name of the archive file. For example:

ar x myarchive.a

This will extract all the files from the archive myarchive.a.

Listing the contents of an archive

To list the contents of an archive, use the t option followed by the name of the archive file. For example:

ar t myarchive.a

This will list all the files that are contained in the archive myarchive.a.

Options

The following table lists the available options for the ar command:

Option Description
r Add files to the archive or replace existing files
q Quickly append files to the archive
x Extract files from the archive
t List the contents of the archive
d Delete files from the archive
m Move files within the archive
p Print the contents of the archive to standard output
s Create an index of the archive

Troubleshooting tips

  • If you receive an error message that says “ar: creating archive: No such file or directory”, make sure that the directory where you are trying to create the archive exists and that you have write permissions for that directory.
  • If you receive an error message that says “ar: myarchive.a: File format not recognized”, make sure that the archive file you are trying to modify or extract from is a valid archive file that was created using the ar command.

Notes

  • The ar command is typically used to create static libraries for object files, which can then be linked with other programs using the ld command.
  • The ar command is not recommended for creating archives of other types of files, as it is not as efficient as other archiving utilities such as tar.