ar command is used to create, modify, and extract archives.
ar is the short form and first two letters of the
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
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
$ sudo apt-get install build-essential
ar Command 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
arcommand 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
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
fgets.c into a new archive named
$ ar r myfiles.a HelloWorld.java example.txt fgets.c
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 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
$ 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
fgets.c we can also add more according to our wish.
$ ar xv myfiles.a example.txt fgets.c
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 timestamps and ownership and access rights.
$ ar t myfiles.a
List Contents Of Archive with Their Time Stamp, Size, Ownership and Access Rights
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
Read Contents Of Archived Files
Generally, ASCII files are archived and used software development. So these source code, configuration files are in ASCII form and 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
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.txtand its content is printed after that.
Add New Files Into Existing Archive
We can also add new files to the existing archive file. In order to add new files, we will use
q option. In this example, we will append a file named
$ ar qv myfiles.a example.txt
AND we can also add multiple files just by putting them at the end of the command and separating the file names with space. In this example, we will add multiple files names
$ ar qv myfiles.a example.txt fgets.c
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
$ 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 it with spaces.
$ ar dv myfiles.a fgets.c sqlscript.sql