Linux ar Command Tutorial with Examples For Archiving

Linux ar command is used to create, modify and extract archives. ar is the short form and first two letters of the archive. ar is provided by most of the Linux distributions like Ubuntu, Debian, Kali, Mint, CentOS, Fedora, Red Hat, and BSD variants.

Ar Is Archiving Not Compression

Archiving is just putting multiple files into a single file without any extra operation. This means archiving do not compress files put into an archive. Archived files sized do not change and stays the same but in compression, the compressed file sizes are decreased. tar is another compression format and command used to archive files but gz, bz , zip , rar are all of them are compression formats, algorithms, and tools.

Install ar On Ubuntu, Mint, Kali, Debian

ar command is an auxiliary tool for the development and building packages. So it comes with the package named build-essential where we have to install this package to use ar command.

sudo apt-get install build-essential

Syntax

ar command has very simple syntax where we provide the option and then file and directory parameters.

ar OPTION ARCHIVE_NAME FILES
  • OPTION is the `ar` command option which specifies the action and behavior of the `ar` command.
  • ARCHIVE_NAME is the name of the archive to be created, edited or listed
  • FILES part provides the files  which will be used during operation like compress, extract or list

Create An Archive

We will start with an archive created with the ar command. r option is used to create an archive and we need to also provide the archive name and the files we want to put into an archive. In this example, we will add files named HelloWorld.java, example.txt, fgets.c into a new archive named myfiles.a.

$ ar r myfiles.a HelloWorld.java example.txt fgets.c
Create An Archive
Create An Archive

Archives Can Hold Multiple Files with The Same Name

We have to say that before starting to work on ar command. The archives can store files with the same name in a single archive. As an example, example.txt can be seen multiple times in a single archive file.

Extract All Contents Of Archive

We can extract all contents of an archive with the x option. We will also use v option in order to print the file names extracted in verbose mode.

$ ar xv myfiles.a
Extract All Contents Of Archive
Extract All Contents Of Archive

Extract Specified Files From The Archive

We can also extract single or multiple files from the archive. We will again use the x option but we will also provide the file we want to extract. In this example, we will extract the file named example.txt.

$ ar xv myfiles.a example.txt

AND we can also extract multiple files with a single command by adding the file names at the end of the command. In this example, we will extract files named example.txt , fgets.c we can also add more according to our wish.

$ ar xv myfiles.a example.txt fgets.c
Extract Specified Files From The Archive
Extract Specified Files From The Archive

List Contents Of Archive

We can list currently existing archive files and their names with the t option. This will only list the names and do not provide extra attributes like timestamp and ownership and access rights.

$ ar t myfiles.a
List Contents Of Archive
List Contents Of Archive

List Contents Of Archive with Their Time Stamp, Size, Ownership and Access Rights

By using t option we only list the archives file names but we can also list the attributes of the archived files with an extra v option. This will list time stamp, size, ownership, access rights.

$ ar tv myfiles.a
List Contents Of Archive with Their Time Stamp, Size, Ownership and Access Rights
List Contents Of Archive with Their Time Stamp, Size, Ownership and Access Rights

Read Contents Of Archived Files

Generally, ASCII files are archived and used software development. So these source code, configuration files are in ASCII form an can be printed to the screen without extracting/de-archiving them. We can use pv option which will print and display all files contents to the screen and prefix contents with its file name.

$ ar pv myfiles.a
Read Contents Of Archived Files
Read Contents Of Archived Files

We can see from the output that the first file is HelloWorld.java file which is printed as <HelloWorld.java> before its content. The second file is <example.txt>and its content is printed after that.

Add New Files Into Existing Archive

We can also add new files into the existing archive file. In order to add new files, we will use q option. In this example, we will append a file named example.txt.

$ ar qv myfiles.a example.txt

AND we can also add multiple files just by putting them the end of the command and separating the file names with space. In this example, we will add multiple files names example.txt and fgets.c

$ ar qv myfiles.a example.txt fgets.c
Add New Files Into Existing Archive
Add New Files Into Existing Archive

Delete/Remove Files From An Archive

We can delete or remove files from an existing archive with the d option. In this example, we will delete the file named example.txt from the archive named myfiles.a.

$ ar dv myfiles.a example.txt

AND we can also remove and delete multiple files in a single command by adding them to the end of the command and separating with spaces.

$ ar dv myfiles.a fgets.c sqlscript.sql

Leave a Comment