웹사이트 검색

초보자를 위한 Linux ar 명령 자습서(예제 5개)


이 페이지에서

  1. Linux ar 명령
  2. Q1. ar을 사용하여 아카이브를 만드는 방법은 무엇입니까?\n
  3. Q2. ar을 사용하여 아카이브의 내용을 나열하는 방법은 무엇입니까?\n
  4. Q3. 아카이브에 포함된 파일의 내용을 직접 표시하는 방법은 무엇입니까?\n
  5. Q4. 아카이브에 새 구성원을 추가하는 방법은 무엇입니까?\n
  6. Q5. 아카이브에서 구성원을 삭제하는 방법은 무엇입니까?\n
  7. 결론

Linux에는 아카이브를 생성할 수 있는 몇 가지 명령줄 유틸리티가 있습니다. 그러한 유틸리티 중 하나는 ar입니다. 이 자습서에서는 이해하기 쉬운 몇 가지 예를 사용하여 이 명령줄 도구의 기본 사항에 대해 설명합니다. 하지만 그 전에 이 기사에 포함된 모든 예제가 Ubuntu 18.04 LTS 시스템에서 테스트되었음을 언급할 가치가 있습니다.

리눅스 ar 명령

ar 명령을 사용하면 아카이브를 생성, 수정 또는 추출할 수 있습니다. 구문은 다음과 같습니다.

ar [OPTIONS] archive_name member_files

이 도구에 대한 매뉴얼 페이지의 내용은 다음과 같습니다.

The GNU ar program creates, modifies, and extracts from archives. An archive is a single file 
holding a collection of other files in a structure that makes it possible to retrieve the original
individual files (called members of the archive).

The original files' contents, mode (permissions), timestamp, owner, and group are preserved in the
archive, and can be restored on extraction.

GNU ar can maintain archives whose members have names of any length; however, depending on how ar is
configured on your system, a limit on member-name length may be imposed for compatibility with
archive formats maintained with other tools.  If it exists, the limit is often 15 characters
(typical of formats related to a.out) or 16 characters (typical of formats related to coff).

ar is considered a binary utility because archives of this sort are most often used as libraries
holding commonly needed subroutines.

ar creates an index to the symbols defined in relocatable object modules in the archive when you
specify the modifiers. Once created, this index is updated in the archive whenever ar makes a change
to its contents (save for the q update operation).  An archive with such an index speeds up linking
to the library, and allows routines in the library to call each other without regard to their
placement in the archive.

다음은 ar의 작동 방식에 대한 좋은 아이디어를 제공하는 몇 가지 Q&A 스타일의 예입니다.

Q1. ar을 사용하여 아카이브를 만드는 방법은 무엇입니까?

이것은 r 명령 옵션을 사용하여 수행할 수 있습니다. 매뉴얼 페이지에 따르면 "아카이브에 기존 파일을 교체하거나 새 파일을 삽입"할 수 있습니다.

예를 들면 다음과 같습니다.

ar r test.a *.txt

위의 명령은 현재 디렉토리의 모든 txt 파일을 포함하는 아카이브 test.a를 생성합니다.

Q2. ar을 사용하여 아카이브의 내용을 나열하는 방법은 무엇입니까?

이것은 t 명령줄 옵션을 사용하여 수행할 수 있습니다. 예를 들어 다음 명령을 실행합니다.

ar t test.a

아카이브에 포함된 모든 파일 목록을 표시했습니다.

Q3. 아카이브에 포함된 파일의 내용을 직접 표시하는 방법은 무엇입니까?

이것은 p 명령 옵션을 사용하여 수행할 수 있습니다. 예를 들면 다음과 같습니다.

ar p test.a

다음은 이 명령으로 생성된 출력입니다.

따라서 세 텍스트 파일의 내용이 모두 출력에 표시되었음을 볼 수 있습니다(이 파일은 서로의 복사본이므로 내용은 세 경우 모두 동일함).

Q4. 아카이브에 새 구성원을 추가하는 방법은 무엇입니까?

r 명령 옵션을 사용하면 이 작업도 수행할 수 있습니다. 예를 들어 새 텍스트 파일인 tes3.txt를 기존 아카이브 test.a에 추가하려면 다음 명령을 사용했습니다.

ar r test.a test3.txt

Q5. 아카이브에서 구성원을 삭제하는 방법은 무엇입니까?

그것도 쉽습니다. d 명령 옵션을 사용하고 삭제할 구성원의 이름을 지정하면 됩니다.

예를 들어 test3.txt를 삭제하기 위해 ar 명령을 다음과 같이 사용했습니다.

ar d test.a test3.txt

다음 스크린샷은 파일이 성공적으로 삭제되었음을 보여줍니다.

결론

ar 명령은 아카이브를 생성하거나 편집할 때 유용한 작은 도구입니다. 또한 프로그램이 링크되는 정적 라이브러리를 생성하기 위해 프로그래밍에 사용됩니다. 우리는 여기서 표면을 긁었습니다. 도구에 대한 자세한 내용은 매뉴얼 페이지를 참조하십시오.