Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
arch command in Linux with examples
Next article icon

ar command in Linux with examples

Last Updated : 11 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The ‘ar’ command in Linux is a versatile tool used for creating, modifying, and extracting files from archives. An archive is essentially a collection of files bundled together in a specific structure, making it easy to manage multiple files as a single entity. Each file within the archive is referred to as a member of the archive. The ‘ar’ command is commonly used for creating static libraries, which are crucial in software development for linking compiled code into executable programs.

Syntax

ar [OPTIONS] archive_name member_files

Here, OPTIONS are flags that define the action you want to perform, ‘archive_name‘ is the name of the archive file you want to create or modify, and ‘member_files‘ are the individual files you wish to include in the archive.

Key Options for the ‘ar’ Command

Below are the most commonly used options with the ‘ar’ command, along with examples to demonstrate their use:

1. r (Insert Files into Archive)

This is used to create archive, insert files in archive. This is different from q as it deletes any previously existing members. If any member filename does not exist it throws an error. By default, it adds a new member at end of the file.

Example: Assume there is a file named star1 and you want to create archive named “super” without modifier then you can use the following command:

ar r super.a *star1

This will create an archive with member ‘star1′. With modifier it looks like as:

ar rv super.a *star1.txt

Note: The modifier ‘v’ gives a line or output with letter ‘a’ or ‘r’ indicating if the file is appended or not.

2. d (Delete Files from Archive)

Deletes modules from archive. Specify names of the modules as member…; When you add modifier ‘v’, ar lists each module as it is deleted.

ar d super.a star1.txt

In our previous case, we used ‘star1.txt’ in archive ‘super.a’, now we will delete that file from there.In that we had ‘super.a’ archive storing ‘star1.txt’, after using ‘d’ without modifier it just deleted the file now lets see the same example with modifier ‘v’.

ar dv super.a star1.txt

With using modifier ‘v’ it listed module as they are deleted.

3. p (Print Archive Members)

This option is used to print the specified members of a archive in a standard output file if you do not use modifier it will print member as it is an output file whereas if you use modifier v then it will show member name before it is copied to output file.

ar p super.a

Here it gave the output which was written in our files stat1.txt and star2.txt, now let us check what happens when we use modifier v in it.

ar pv super.a

With the use of the modifier, first get the file name and then content written inside it.

4. t (List Archive Contents)

It displays the contents of the archive in a listed manner, usually, it shows the contents of the archive but if we use modifier O then it also shows the corresponding offset of each member.

ar t super.a (taking our old files and examples)

Here ‘t’ displayed the members of the archive, now lets see what it shows when we use modifier.

ar tO super.a

This time with the use of modifier we get the corresponding offset of each member.

5. x (Extract Archive Members)

It extracts each named member from the archive if you do not name the member to be extracted it extracts the whole archive. We can use ‘v’ modifier to list name of each member which is extracted. The modifier ‘v’ displays each file which is extracted and also we did not specify a name so it extracted the whole archive if we specify names of the member it extracts only that member. This command does not extract thin files as in our case, it just displays that it has extracted the archive. The above image shows extraction based on specified member name, and ‘v’ modifier lists file is extract.

Conclusion

The ‘ar‘ command is a useful tool in Linux for handling archives, especially for developers who create and manage static libraries. It offers various options to create, change, and extract files from archives, giving you complete control over what’s inside. Learning how to use the ar command can make managing files on Linux much easier and more efficient.


Next Article
arch command in Linux with examples

M

Madhusudan_Soni
Improve
Article Tags :
  • Linux-Unix
  • linux-command
  • Linux-file-commands

Similar Reads

  • ACPI command in Linux with examples
    ACPI is the command in Linux that helps the users in managing power settings. It also helps in monitoring the hardware status efficiently. It facilitates with providing essential information such as battery health, CPU temperatures, and fan speeds, providing support in system maintenance and perform
    4 min read
  • acpi_available command in Linux with examples
    acpi_available is a command in Linux that tests whether the ACPI (Advanced Configuration and Power Interface) subsystem is available or not. Syntax$ acpi_availableReturn Statuses of acpi_availableThe command returns one of three possible statuses: Status 0: Indicates that the ACPI subsystem is avail
    2 min read
  • acpid command in Linux with Examples
    The acpid daemon provides intelligent power management on a system and allows to query battery and configuration status by supporting the Advanced Configuration and Power Interface (ACPI). The ACPI events are notified to the user-space programs by acpid. The ACPI (Advanced Configuration and Power In
    3 min read
  • addr2line command in Linux with Examples
    'addr2line' command in Linux is used to convert addresses into file names and line numbers. When the executable files/object files are run with the 'objdump' command, the file is de-assembled and the machine code is displayed. These machine instructions for the executable are displayed along with th
    4 min read
  • agetty command in Linux with Examples
    agetty is a Linux version of getty. getty short for "get tty" is a Unix program running on a host computer that manages physical or virtual terminals to allow multi-user access. Linux provides a virtual terminal(tty) which is similar to the regular Linux terminal. agetty command opens a virtual term
    4 min read
  • How to Create and Use Alias Command in Linux
    Imagine you're lost in a maze of complicated Linux commands. You stumble upon a secret doorway marked "Alias," and inside you find shortcuts to all your favorite commands! That's what creating aliases is like. You get to make your own mini-commands for the long ones you use all the time, making thin
    6 min read
  • amixer command in Linux with Examples
    amixer is a command-line mixer for ALSA(Advanced Linux Sound Architecture) sound-card driver . amixer can support multiple soundcards. amixer with no arguments will display the current mixer settings for the default soundcard as well as the device. This is a good way to see a list of the simple mixe
    2 min read
  • aplay command in Linux with examples
    aplay is a command-line audio player for ALSA(Advanced Linux Sound Architecture) sound card drivers. It supports several file formats and multiple soundcards with multiple devices. It is basically used to play audio on command-line interface. aplay is much the same as arecord only it plays instead o
    2 min read
  • aplaymidi Command in Linux with Examples
    aplaymidi command in Linux is used to play standard MIDI(Musical Instrument Digital Interface) files, by sending the content of a MIDI file to an ALSA(Advanced Linux Sound Architecture) MIDI port, sound renderer like timidity or a hardware MIDI device is required to play MIDI files. Syntaxaplaymidi
    3 min read
  • apropos command in Linux with Examples
    Linux/Unix comes with a huge number of commands and thus it become quite difficult sometimes to remember each and every command. apropos command becomes useful in such cases. apropos command helps the user when they don't remember the exact command but knows a few keywords related to the command tha
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences