Post

Linux tar

1. tar

여러개의 파일을 모아서 하나의 파일(tar 형식)로 만들어 준다.

2. 주요 옵션

옵션내용
-c파일 묶음을 생성
-x파일 묶음을 해제
-z파일을 gzip 압축하면서 묶음을 생성
-v처리 상황을 출력
-t파일 묶음에 들어있는 파일 목록을 출력
-P절대 경로 처리
–exclude파일 제외
–delete파일 삭제

3. 사용예제

1) 파일 압축

1
2
3
4
5
6
7
8
9
10
11
# foo, bar를 archive.tar로 만듬
tar -cf archive.tar foo bar

# archive.tar 파일 안에 묶여 있는 내용을 확인
tar -tf archive.tar

# foo, bar를 gzip 압축하여 archive.tar.gz 으로 만듬
tar -zcvf archive.tar.gz foo bar

# source_dir의 .log, .attatch 로 끝나는 파일, folder_name 디렉토리 제외하고 file.tar.gz 으로 압축
tar -zcvf file.tar.gz source_dir --exclude="*.log" --exclude="*.attach*" --exclude="./folder_name"

2) 파일 해제

1
2
3
4
5
# archive.tar 파일 묶음 해제
tar -xf archive.tar

# archive.tar.gz 압축 파일의 묶을 해제
tar -zxvf archive.tar.gz

경고: tar: Removing leading '/' from member names

tar로 압축을 진행할 때 절대 경로로 설정되어 있으면 발생하는 경고이다. 절대 경로를 제거하고 압축을 진행한다. 경고 이기 때문에 참고만 하면 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 다음과 같은 경우 실제 압축은 etc/sample/conf로 진행
$ tar -zcf sample.tgz /etc/sample/conf

# 파일 압축 상태 확인
$ tar -tf sample.tgz
etc/sample/conf/file_a
etc/sample/conf/file_b

$ tar -zcfP sample2.tgz /etc/sample/conf

# 파일 압축 상태 확인
# / 를 기준으로 압축이 진행됨
$ tar -tf sample2.tgz
/etc/sample/conf/file_a
/etc/sample/conf/file_b

3) 파일 삭제

압추 파일안에 파일을 삭제 한다. --file 옵션과 함께 사용해야 한다.

1
2
3
4
5
6
7
8
$ tar -tf txt.tar
a.txt
b.txt

$ tar --delete --file=txt.tar a.txt

$ tar -tf txt.tar
b.txt

[출처 및 참고]

This post is licensed under CC BY 4.0 by the author.