·터미널 명령어

tar

압축·전송

쓰는 꼴

tar -czf <archive.tar.gz> <path>

파일을 한 묶음으로 묶기만 하고 그 자체로는 압축하지 않습니다 — 줄이는 일은 `-z`(gzip)·`-j`(bzip2)·`-J`(xz)가 맡고, `-c` 만들기·`-x` 풀기·`-t` 들여다보기 가운데 하나만 골라 앞에 두며, 옛 꼴에서는 `-f`가 옵션 가운데 마지막이라 그 바로 뒤에 묶음 이름이 옵니다.

자주 쓰는 옵션

옵션
-c / -x / -tCreate / extract / list. Exactly one of the three, and it comes first.
-f <file>The archive name, and it must be the last flag before that name.
-z / -j / -Jgzip / bzip2 / xz. GNU tar detects the format when extracting, so -z is optional with -x.
-vPrint the file names as they go past.
-C <dir>Change into that directory first. Position in the command line matters.
--exclude='node_modules'Skip matching paths. Write it before the source paths.
--strip-components=1Drop the top-level directory when extracting.
--zstdUse zstd instead of gzip (GNU tar 1.31 and newer).

예시

tar -czf site.tar.gz -C /var/www .

Archive the contents of a directory without the leading path.

tar -xzf site.tar.gz -C /tmp/restore

Extract into a directory that already exists.

tar -tzf site.tar.gz | head

Look inside before extracting, so nothing lands in $PWD by surprise.

압축과 묶음은 다른 일입니다. tar가 묶고 gzip이 줄이며, .tar.gz는 그 둘을 겹친 것입니다.

읽는 방법

  • 대괄호 [ ]는 넣어도 되고 안 넣어도 되는 자리입니다.
  • 점 셋 …은 여러 개를 이어 쓸 수 있다는 뜻입니다.
  • 옵션은 대소문자를 가립니다 — -r과 -R이 다른 명령도 있습니다.

자주 묻는 것

Q. tar은 무엇을 하나요?

파일을 한 묶음으로 묶기만 하고 그 자체로는 압축하지 않습니다 — 줄이는 일은 `-z`(gzip)·`-j`(bzip2)·`-J`(xz)가 맡고, `-c` 만들기·`-x` 풀기·`-t` 들여다보기 가운데 하나만 골라 앞에 두며, 옛 꼴에서는 `-f`가 옵션 가운데 마지막이라 그 바로 뒤에 묶음 이름이 옵니다.

Q. 어떻게 치나요?

tar -czf <archive.tar.gz> <path> — 대괄호는 생략할 수 있는 자리입니다.

Q. 옵션은 몇 개나 자주 쓰나요?

여기 정리한 것은 8개입니다. 전체 목록은 man tar에 있습니다. 이 명령은 압축·전송 갈래입니다.

같이 보는 명령

man 페이지: man tar