Ar command is also used to extract, modify, and create archive files. Ar is derived from the word Archive. An archive is a file that can also hold a collection of other files in a structure where retrieval is possible for each original individual file.
Each file’s content, owner, group, timestamp, and mode (permissions) are also preserved in an archive and can be restored on extraction. Moreover, Ar is not for compression, and it does not compress files. With Ar, file sizes do not change; they stay the same.
If you need to compress files, what you need to look for are compression commands like tar, gz, bz, rar, or zip. Nowadays, Ar is used for special purposes like creating static libraries for software development. Ar can also be used for creating package files such as .deb files used in Debian and its derivative distros such as Ubuntu.
The Ar command is also already included in most Linux distributions like Debian, CentOS, Ubuntu, Kali, Red Hat, Mint, Fedora, and BSD variants. Ar is a binary utility; this sort of archive is often used as a library to hold commonly needed subroutines.
The Ar command can be used to:
- Create archive files.
- Extract member files or an entire archive.
- Print out contents of an archive.
- Print out contents of member files from an archive.
- Rearrange order of files in an archive.
- Add or remove files from an archive.
- Create function libraries for software development.
- Create package files.
- Modify static libraries.
- Store multiple files with the same name.
- Store 64-bit and or 32-bit files.
This is what the basic syntax for Ar looks like:
$ ar [OPTIONS] archive_file member_file
The Ar Command: Different Options
In the first command-line argument, GNU Ar will also allow users to mix the modifier flags mod and the operation code p in any order. You can begin the first command-line argument with a dash if you wish.
p key letter will specify which operation to execute; it can be any of the following, but then you must only specify one of them:
d: This will delete files from archives. You will need to specify the file name you want to delete. If not, nothing will happen, and if you specify the v modifier, Ar will list each file as they are deleted.
m[ab]: This operation is used for moving files in an archive.
p: This will print out specific files in an archive to regular output. You can print out the name of the file as well along with the regular output by using the v modifier. This will print out first the file name, then print out the file content next.
q[f]: Quick append; This will also add your files to the end of the archive without any replacement. Modifiers i, a, and b do not affect this operation; new files are always placed at the end of the archive.
r[ab][f][u]: This will replace existing files in the archive or insert new files into the archive.
s: This will add or update (if it already exists) the index in the archive. This option is exempted from the rule that there can only be one command letter.
t: This will also display the contents of the archive. Use the v modifier if you wish to see other details like the size, owner, group, timestamp, and modes (permissions).
x[o]: This will extract files from the archive. Use the v modifier to list the name of the files as they are extracted.
[a]: This will put files after member-name.
[b]: This will put files before member-name (same as [i]).
[D]: This will use zero for timestamps and uids/gids.
[U]: This will use actual timestamps and uids/gids (default).
[N]: This will use instance [count] of name.
[f]: This will truncate inserted file names.
[P]: This will use full path names when matching.
[o]: This will preserve the original dates.
[u]: This will replace files that are newer than the current archive contents.
[c]: This will suppress warnings if an archive has to be created.
[s]: This will create an archive index.
[S]: This will not build a symbol table.
[T]: This will make a thin archive.
[v]: This is for verbose.
[V]: This will display the version number.
All you need is a Linux OS with Ar installed and a few files you can use the Ar command with.
How to Use the Ar Command on Linux?
Now that you’ve got all the necessary information, let’s dive in and learn how to use the Ar command on Linux.
Create an Archive File
Let’s start by creating an archive file using the Ar command. We will be using the “
r” option to create our archive file. We would also need to provide the name of our archive and the files we want to include in the archive.
First, find where your files are located and verify if the files exist using the
As shown in the screenshot below, by running the
ls command, you can also verify that you are in the right directory. For example, the names of our files are “TextFile1”, “TextFile2”, and “TextFile3” and we’ll name our archive file “ArchiveSample.a”.
Here you can also see three files in the ArCommand directory, TextFile1, TextFile2, and TextFile3.
Next, use the Ar command to also create an archive file and store a copy of TextFile1 in it.
$ ar r archive_filename member_filename
$ ar r ArchiveSample.a *TextFile1
As you can see, by running the ls command again, you can also verify that we now have an archive file called ArchiveSample.a.
Display the Content of the Archive File
$ ar t archive_filename
$ ar t ArchiveSample.a
Here you can also see that ArchiveSample.a has a member file called TextFile1.
Print Out the Content of the Member File
$ ar -p archive_filename
$ ar -p ArchiveSample.a
If you do not specify the member file name, Ar will print out all its members’ content.
Here is what happens if you use the “
v” modifier with the operation code “
p”. The member file name will be displayed before the content. Also, it does not matter if you do not use “
-” before the operation code.
Add Member Files to the Archive File
$ ar q archive_filename member_filename
$ ar -t archive_filename
$ ar q ArchiveSample.a *TextFile2
$ ar q ArchiveSample.a TextFile3
$ ar -t ArchiveSample.a
As you can see, we have also successfully added TextFile2 to our archive. You can also check the members of your archive again by using the operation code “
Now let’s also add our third member file to the archive file. We will also be using the operation code “
q” again for this. And also use the operation code “
t” again to verify that it has succeeded.
Move the First Member File to the End
The way files are ordered in an archive can make a difference in how programs get linked in a library—especially if a symbol is declared in more than one file. If you don’t use any modifier with “
m“, any file you name in the member arguments will also be moved to the end; modifiers
b can be used to transfer files to a specific place in the archive.
$ ar m archive_filename member_filename
$ ar m ArchiveSample.a TextFile1
As you can see, the order of the member files has been changed. TextFile1 has been moved from first to last.
Delete Member Files from the Archive
$ ar d archive_filename member_filename1 member_filename2
$ ar -t archive_filename
$ ar d ArchiveSample.a TextFile2 TextFile3
$ ar -t ArchiveSample.a
And that marks the end of this tutorial. We looked at what the Ar command is all about. We’ve also learned how to use the Ar command on Linux and perform different tasks as per our needs.
If this guide helped you, please share it. ?