윈도우나 맥에서는 주로 zip으로 압축된 경우가 많지만, 리눅스에서는 tar 또는 tar.gz로 압축된 파일을 다운로드하는 경우가 많습니다.
기본적으로 apt/yum 등에서 패키지 관리가 가능하지만, 직접 다운 받은 파일 또는 지금 있는 파일들을 압축하고 싶을 때 어떻게 해야 할까요?
바로 그 답은 tar입니다 (물론 gzip도 있고 다른 util들도 있습니다)
압축하기 (tar)
$> tar -cvf [파일명.tar] [폴더명]
ex)
$> tar -cvf targetFolder.tar targetFolder
→ targetFolder를 targetFolder.tar로 압축
따라 해 보기

다음과 같이 임시로 targetFolder를 생성해주고 조회해 보면 아래와 같이 targetFolder가 생성된 것을 확인 가능합니다.

그다음으로
$> tar -cvf targetFolder.tar targetFolder
라는 명령을 호출하면

아래와 같이 콘솔창에 targetFolder/ 가 압축되었다는 내용이 호출됩니다. (만약 내부에 내용이 있으면 압축되는 내용이 목록으로 나열됩니다.)

그다음으로
$ ls -alt
를 해보면 다음과 같이 targetFolder.tar가 생성됨을 알 수 있습니다.

압축해제 (tar)
$> tar -xvf [파일명.tar]
ex)
$> tar -xvf targetFolder.tar
→ targetFolder.tar 압축 파일을 현재 폴더에 압축해제
따라해보기
먼저 상단의 targetFolder를 삭제하기 위해
$> rm -ivfr targetFolder
라는 명령을 수행해주고
$> ls -alt
를 하게 되면 아래와 같이 targetFolder.tar만 남고 targetFolder가 삭제되었음을 알 수 있습니다.

이 상태에서
아래와 같이
$> tar -xvf targetFolder.tar
명령어를 수행해주면

압축해제된 내용(현재는 폴더내 파일이 없었으므로 targetFolder/만)이 표시되는 것을 확인할 수 있습니다.
그 다음
$> ls -alt
를 해보면 아래와 같이 targetFolder라는 폴더가 생성되었음을 확인할 수 있습니다.

압축하기
$> tar -zcvf [파일명.tar.gz] [폴더명]
tar와의 차이는 옵션인 cvf 앞에 z를 써준다는 차이로 보면 간단할겁니다.
압축해제
$> tar -zxvf [파일명.tar.gz]
tar와의 차이는 옵션인 xvf 앞에 z를 써준다는 차이로 보면 간단할겁니다.
자세한 옵션 내용은 man페이지에서 확인 가능합니다. (해당 명령어는 root권한에서 가능합니다.)
$> man tar
이 중에서 제가 방금 이용한 압축하기(zcvf)와 압축풀기(zxvf)가 가장 일반적으로 쓰이는데 옵션의 내용은 각각 아래와 같습니다
z: gzip(ex 확장자가 gz)으로 압축된 파일을 압축하거나 압축 해제하려고 하는 경우
c: 압축하려는 경우
x: 압축을 해제하려는 경우
v: 압축/압축해제 할때 해당 파일의 내용을 콘솔창에 보여줌 (상단의 명령어 수행시 /targetFolder라고 노출되는 이유)
f: 파일이름을 지정
'개발 창고 > Server' 카테고리의 다른 글
[CentOS] 공용폴더를 위한 samba 활용하기 (0) | 2021.03.16 |
---|---|
[CentOS7] crontab 활용하기 (0) | 2021.01.28 |
[Tomcat] The AJP Connector is configured with secretRequired="true" but the secret attribute is either null or "". This combination is not valid. (2) | 2020.10.22 |
[서버이야기] 웹서버(WS)와 와스(WAS)서버 (0) | 2020.09.07 |
[CentOS] 관련 링크 (0) | 2020.08.04 |