Skip to content

Tar

타르(tar)는 컴퓨터에서, 테입 아카이브를 위해 고안된 파일 형식과 이런 형식의 파일을 다루는데 사용되는 프로그램을 의미한다. 파일 형식은 초기 유닉스 시대에 만들어졌고 POSIX.1-1988 과 POSIX.1-2001 에 의해 표준화되었다.

초기에는 테입 백업 목적으로, 순차적 입출력 장치에 직접 쓰도록 개발되었으나, 현재는, 배포 또는 아카이브 용도로 많은 파일을 디렉토리 구조, 파일 속성들을 보존하면서 하나의 큰 파일로 묶는 데 주로 사용된다.

Flags

  • -c: 압축 할 때.
  • -x: 압축 풀 때.
  • -t: 압축파일 내용 확인할 때.
  • -f: 압축파일 사용할 때.
  • -p: 압축 할 때, 풀 때 퍼미션 유지.
  • -v: 압축 할 때, 풀 때 과정보기.
  • -z: gzip 사용.
  • -j: bzip 사용.

List the contents

List the contents of a tar file
$ tar -tvf file.tar
List the contents of a tar.gz file
$ tar -ztvf file.tar.gz
List the contents of a tar.bz2 file
$ tar -jtvf file.tar.bz2

출력 디렉토리 경로 지정

출력 디렉토리 경로 지정은 -C옵션을 사용한다.

tar -xf file.tar.gz -C $HOME/output

CURL로 Pipe 받기

curl -L https://github.com/slackhq/nebula/releases/download/v1.9.1/nebula-linux-amd64.tar.gz | tar xzf - -C ~/bin

주의할 점은

  • Pipe 는 -가 필요.
  • 출력 위치 변경은 -C {dir}이 필요한데, - 뒤에 와야함 (즉 인자 순서 중요)
  • curl 앞에 sudo 불가. 따라서 sudo -s 같이 우선 진입하고 시작해야함.

Absolute path

압축시 자동으로 제거되는 절대경로를 제거하고 싶지 않다면 --absolute-names 또는 -P 옵션을 사용하면 된다.

$ tar fczP bkup.tar.gz /home/foo/
$ tar fcz bkup.tar.gz --absolute-names /home/foo

Remove first-depth directory

압축된 파일의 디렉토리 경로를 제거한 후 압축 해제하고 싶을 경우 --strip-components=NUMBER 옵션을 사용하면 된다.

 --strip-components=NUMBER
       strip NUMBER leading components from file names on extraction

Your command would be:

tar xfz /var/www/site.gz --strip-components=2 -C /tmp

See also

Favorite site