how to ar x filename.a to different directory
Asked Answered
R

2

17

Is there any ar option to extract objects to a different directory ? Any way to extract them to tmp below ?

[test]# ls -l
total 1828
-rw-r--r-- 1 root root 1859628 Aug 24 02:10 libclsr11.a
drwxr-xr-x 2 root root    4096 Aug 24 02:12 tmp
[test]# ar x libclsr11.a
[test]# ls -l
total 3760
-rw-r--r-- 1 root root  157304 Aug 24 02:13 clsrcact.o
-rw-r--r-- 1 root root   19304 Aug 24 02:13 clsrcclu.o
-rw-r--r-- 1 root root   55696 Aug 24 02:13 clsrccss.o
..
drwxr-xr-x 2 root root    4096 Aug 24 02:12 tmp
[test]#
Rabi answered 23/8, 2016 at 19:40 Comment(0)
P
19

The solution depends on the version of ar. You can use ar --version to display the version of ar on your system.

For ar / GNU binutils before version 2.34:

Unfortunately, ar before version 2.34 does not provide a way to specify the directory where the files shall be extracted. (At least I was unable to find one.) It always uses the current directory. However, there is a simple workaround: Change to the target directory before extraction and use the relative path to the archive instead:

# cd ./tmp/
# ar x ../libclsr11.a

This way you should end up with clsrcact.o, clsrcclu.o and clsrccss.o inside the ./tmp/ directory.

For ar / GNU binutils version 2.34 or later:

Version 2.34 of binutils introduced the --output for the ar program. (See the changelog.) It can be used to specify the directory where the contents shall be extracted:

# ar x --output tmp libclsr11.a

That way the archive contents will land inside the tmp directory without having to use the workaround for earlier ar versions.

Photomicroscope answered 23/8, 2016 at 19:54 Comment(0)
C
4

Actually it is possible, maybe was changed recently, --output option is what you need.

In your example:

ar x libclsr11.a --output tmp
Compurgation answered 28/10, 2020 at 12:57 Comment(1)
Yes. The --output option was introduced in version 2.34 of ar / binutils which was released in February 2020, more than three years after the initial answer. I've updated my answer to include the new option for more recent versions.Photomicroscope

© 2022 - 2024 — McMap. All rights reserved.