Linux

tar 명령어 사용시 특정 파일을 제외하고자 할 때

검정비니 2022. 3. 29. 14:23
728x90
반응형

알다시피, tar 명령어는 리눅스에서 사용하는 명령어로 그 주 기능은 여러 파일들을 테이핑해서 하나의 압축 파일로 만드는 것이다. 그 유용성 때문에 많은 사람들이 tar를 코드 및 프로그램의 배포등에 사용하곤 한다.

 

이 tar를 쓰다보면 폴더 내의 특정 폴더 혹은 파일을 제외한 나머지 모든 파일들만 담고 싶을 때가 있을 것이다.

바로 이런 경우를 대비해서 tar 명령어는 --exclude라는 옵션을 제공한다. 이 옵션은 "--exclude=file_path" 와 같은 형태로 사용하곤 한다.

 

그런데, 많은 초보자들이 모르는 사실이 있는데, 이 tar를 사용할 때, --exclude 옵션은 다른 옵션들보다 선행되어야 한다.

 

# not working
tar -cvzwf test.tar.gz test --exclude=test/need_to_excluded

# working properly
tar --exclude=test/need_to_excluded -cvzwf test.tar.gz

위의 코드를 보면, 첫번째 줄은 --exclude 옵션이 뒤 쪽에 위치해 있기 때문에 제대로 작동하지 않는다.

그에 반해, 두번째 줄은 --exclude 옵션이 맨 앞쪽에 위치해 있기 때문에 제대로 작동하게 된다.

 

아래 링크는 해당 이슈와 관련된 stackoverflow 질문이다.

https://stackoverflow.com/questions/984204/shell-command-to-tar-directory-excluding-certain-files-folders

 

Shell command to tar directory excluding certain files/folders

Is there a simple shell command/script that supports excluding certain files/folders from being archived? I have a directory that need to be archived with a sub directory that has a number of very...

stackoverflow.com

 

반응형