facebook youtube pinterest twitter reddit whatsapp instagram

What is ar Command in Linux and How Do I Use It?

ar is a command on a UNIX-Like system, we would be covering the one on Linux Based Distro a.k.a, the GNU ar command.

So...

GNU ar is a command line tool used for creating, modifying, and extracting files from archives. An archive is a single file that contains multiple files, often in a compressed format. GNU ar is part of the GNU Compiler Collection (GCC) and is commonly used on Linux and other Unix-like operating systems.

Using GNU ar

To use GNU ar, you must first open a terminal window on your system. From there, you can use the following commands:

  • ar -q <archive name> <file1> <file2> ...: This command creates a new archive with the specified name and adds the specified files to it.
  • ar -x <archive name>: This command extracts the contents of the specified archive into the current directory.
  • ar -t <archive name>: This command displays a list of the files in the specified archive.
  • ar -d <archive name> <file1> <file2> ...: This command deletes the specified files from the specified archive.
  • ar -m <archive name> <file1> <file2> ...: This command moves the specified files within the specified archive.


By default, GNU ar uses the .a file extension for archives. However, you can specify a different file extension by using the -f flag, like this:

ar -f <file extension>.

GNU ar also provides some additional options that you can use when creating or modifying archives.

For example, you can use the -c flag to create an archive without adding any files to it, or the -s flag to include a symbol table in the archive. You can view the full list of available options by using the man command to view the GNU ar manual.

GNU ar Command Examples

Here are some examples of using GNU ar:

Creating an Archive Using ar Command

To create a new archive called "myfiles.a" and add the "file1.txt" and "file2.txt" files to it, you would use the following command:

ar -q myfiles.a file1.txt file2.txt

Extracting Content Using ar Command

To extract the contents of the "myfiles.a" archive into the current directory, you would use the following command:

ar -x myfiles.a

Display Files in an Archive Without Extraction it Using the ar Command

To display a list of the files in the "myfiles.a" archive, you would use the following command:

ar -t myfiles.a

Deleting Files From Archive Using The ar Command

To delete the "file1.txt" and "file2.txt" files from the "myfiles.a" archive, you would use the following command:

ar -d myfiles.a file1.txt file2.txt

Move Files Within Archive Using the ar Command

To move the "file1.txt" and "file2.txt" files within the "myfiles.a" archive, you would use the following command:

ar -m myfiles.a file1.txt file2.txt

Create an Empty Archive Using the ar Command

To create an archive without adding any files to it, you would use the -c flag, like this:

ar -c -q myfiles.a

Specify Different Extensions for an Archive

To specify a different file extension for the archive, you would use the -f flag, like this:

ar -f .tar -q myfiles.tar file1.txt file2.txt

Verbose Output with ar Command

The command below enables verbose output, which will display additional information about the progress of the operation:

ar -v

Replace File in an Archive with the ar Command

The command below replaces the specified files in the specified archive with new versions of the files.

ar -r <archive name> <file1> <file2> ...

Append To Archive Without Deleting or Replacing Existing File

The below command appends the specified files to the specified archive, without deleting or replacing any existing files.

ar -A <archive name> <file1> <file2> ...

Get the Current ar Version

To display the version number of the GNU ar tool, you would use the --version flag, like this:

ar --version

View ar Manual

To view the manual for the GNU ar tool, you would use the man command, like this:

man ar

These examples demonstrate some cool uses of GNU ar. You can use these commands and options to create more complex and flexible archives on your system.

You are welcome.