본문 바로가기

coding/Linux

[Linux] 아카이브와 압축 - Archive & Compression

 

  리눅스에서는 모든 것을 파일이나 디렉터리로 다루기 때문에 백업을 할 때도 여러 파일을 하나로 모으는 것이 좋다. 이처럼 여러 개의 파일이나 디렉터리를 모아서 파일 하나로 만드는 것 아카이브(archive)라고 한다.

  주의할 점은 윈도우의 zip파일과는 다르다는 것이다. 아카이브는 하나로 모으는 것이고 파일의 크기를 작게 만드는 것 압축(compression)이다. 즉, zip파일은 아카이브와 압축을 한꺼번에 수행한 결과물이다.

archive vs compression

 

 

1. Archive - tar 명령어

1-1. 아카이브하기, 해제하기

  tar는 여러개의 파일이나 디렉터리를 하나의 아카이브 파일로 모으거나 복원하기 위해 사용하는 명령어이다. 원본의 속성을 유지한 상태로 여러개의 파일을 묶어주거나, 묶어진 파일을 원본으로 되돌릴 수 있다.

 

#아카이브
tar -cf <아카이브 파일 이름> <아카이브로 묶을 파일 경로>

#되돌리기
tar -xf <아카이브 파일>

 

참고로 cf 옵션에서 c는 create, 즉 새로운 아카이브 파일을 만드는 것을 의미하며, f(file)은 새롭게 만들 아카이브 파일의 이름을 지정할 때 사용한다. tar 명령어를 사용할 때 파일을 지정하려면 언제나 f 옵션을 사용해야 한다.

 

 

위에 4.0K짜리 datadir 디렉터리를 tar로 아카이브해보면

 

 

놀랍게도 1.7M가 되었다(...) 압축한 것이 아니니 용량이 작아지지 않았다.

아무튼 tar 명령어를 사용하면 디렉터리 전체를 아카이브 할 수 있다.

 

1-2. 아카이브 파일 내용 확인하기

다음 명령어를 통해 tar파일의 콘텐츠를 확인할 수도 있다.

 

tar -tvf <아카이브 파일>

 

 

[참고] Options

  • -c, --create : create an archive file.
  • -x, --extract : extract an archive file.
  • -v, --verbose : show the progress of the archive file.
  • -f, --file : filename of the archive file.
  • -t, --list : viewing the content of the archive file.
  • -j, --bzip2 : filter archive through bzip2.
  • -J, --xz : filter archive through xz.
  • -z, --gzip,  filter archive through gzip.

 

 

2. Compression - gzip, bzip2, xz / zip 명령어

2-1. 파일 압축하기

  gzip, bzip2, xz 명령어들은 본질적으로 file 단위로만 압축하는 명령어이다. 또한, 압축하면 원본 파일은 삭제되고 압축파일만 남겨진다는 특징도 갖고 있다.

 

#gzip으로 압축하기, 내용보기, 압축 해제
gzip <압축파일 이름>
zcat <압축파일 이름>
gunzip <압축파일 이름> 또는 gzip -d <압축파일 이름>

#bzip2으로 압축하기, 내용보기, 압축 해제
bzip2 <압축파일 이름>
bzcat <압축파일 이름>
bunzip2 <압축파일 이름> 또는 bzip2 -d <압축파일 이름>

#xz으로 압축하기, 내용보기, 압축 해제
xz <압축파일 이름>
xzcat <압축파일 이름>
unxz <압축파일 이름>
unxz <압축파일 이름> 또는 xz -d <압축파일 이름>

 

2-2. 디렉터리 압축하기

  그럼 디렉토리는 압축 못하나? No. zip [-r] 옵션을 통해 디렉토리 압축 가능하다.

 

zip -r <디렉토리 압축파일 이름> <압축할 디렉토리 경로>

 

tar파일과 용량을 비교해보면 compression까지 한번에 되었음을 알 수 있다.

 

 

 

3. Tar + Compress utility

  마지막으로 tar와 gzip, bzip2, xz를 함께 사용해서 디렉터리를 백업할 수 있다. 위의 tar 옵션란에서 살펴보긴 했다.

 

#gzip
tar -czvf <백업파일 이름> <백업할 디렉토리 경로>
tar -xzvf <해제할 백업파일 이름>

#bzip2
tar -cjvf <백업파일 이름> <백업할 디렉토리 경로>
tar -xjvf <해제할 백업파일 이름>

#xz
tar -cJvf <백업파일 이름> <백업할 디렉토리 경로>
tar -xJvf <해제할 백업파일 이름>

 

zip vs tar+compress utility ?

  두 방법의 차이가 뭘까? 일단 tar+{gzip, bzip2, xz} 조합이 압축력이 더 강하다(가장 월등한건 xz)

 

압축파일 용량이 zip > gz > bz2 > xz 순으로 작다

 

본질적인 차이는 다음 스택오버플로우 질의응답에서 확인할 수 있다.

Usually you use gzip along with tar to compress the resulting tarball, thus achieving similar results as with zip.
For reasonably large archives there are important differences though. A zip archive is a collection of compressed files. A gzipped tar is a compressed collection (of uncompressed files). Thus a zip archive is a randomly accessible list of concatenated compressed items, and a .tar.gz is an archive that must be fully expanded before the catalog is accessible.
The caveat of a zip is that you don't get compression across files (because each file is compressed independent of the others in the archive, the compression cannot take advantage of similarities among the contents of different files); the advantage is that you can access any of the files contained within by looking at only a specific (target file dependent) section of the archive (as the "catalog" of the collection is separate from the collection itself).The caveat of a .tar.gz is that you must decompress the whole archive to access files contained therein (as the files are within the tarball); the advantage is that the compression can take advantage of similarities among the files (as it compresses the whole tarball).

 

  zip 아카이브는 압축된 파일들의 모음이다(각 파일이 아카이브의 다른 파일과는 독립적으로 압축됨). 따라서 zip 아카이브는 압축된 항목을 임의로 액세스할 수 있다.

  반면 gzipped tar는 압축된 콜렉션(압축되지 않은 개별 파일들 전체의 압축)으로, 액세스하기 전에 다시 restore해야 한다. 대신 장점으로 전체 tarball을 압축할 때 파일 간의 유사성을 활용할 수 있다는 것이다(그래서 압축률이 더 높나봄..)

 

 

 

 

자료 출처: https://firedev.tistory.com/entry/Linux-Unix-%EB%A6%AC%EB%88%85%EC%8A%A4%EC%8B%9C%EC%8A%A4%ED%85%9C-%EB%94%94%EB%A0%89%ED%86%A0%EB%A6%AC-%EA%B5%AC%EC%A1%B0%EC%99%80-%EA%B8%B0%EB%8A%A5

'coding > Linux' 카테고리의 다른 글

[Linux] ext 파일 시스템과 inode - Linux File System Hierarchy  (0) 2022.08.02