tar 압축시 특정 디렉토리 제외하고 압축하기

tar 로 압축을 할때 특정 디렉토리 (폴더) 는 제외하고 해야 하는 경우가 있습니다.

자주 하는 게 아니니 생각이 잘 안납니다. 어렵지 않으니 기억 해두면 좋습니다.

 

1.특정 1개 디렉토리는 제외하고 압축 하기

[root@buddy2]# ls -al
total 6920
drwxrwxr-x. 2 root root      37 Jun 29 06:50 1.lookup_apple
drwxrwxr-x. 2 root root      21 Jun 27 09:06 2.lookup_orange
-rw-rw-r--. 1 root root     128 Jun 27 09:03 a.lookup_apple.sh
-rw-rw-r--. 1 root root     129 Jun 27 09:04 b.lookup_orange.sh
drwxrwxr-x. 5 root root      74 Jun 15 13:32 venv


명령어: tar cvfz fruits.tar.gz --exclude=venv ./*

 

fruits.tar.gz 로 압축하도 대상은 모두 (하위포함) ./* 이고

venv 디렉토리는 제외하고 진행합니다. (--exclude)

[root@buddy2]# tar cvfz fruits.tar.gz --exclude=venv ./*
./1.lookup_apple/
./1.lookup_apple/list.txt
./1.lookup_apple/main.py
./2.lookup_orange/
./2.lookup_orange/main.py
./a.lookup_apple.sh
./b.lookup_orange.sh


[root@buddy2]# ls -al
total 12
drwxrwxr-x. 5 root root  139 Jun 29 06:56 .
drwxr-xr-x. 4 root root   39 Jun 15 13:30 ..
-rw-rw-r--. 1 root root 3704 Jun 29 06:56 fruits.tar.gz
drwxrwxr-x. 2 root root   37 Jun 29 06:50 1.lookup_apple
drwxrwxr-x. 2 root root   21 Jun 27 09:06 2.lookup_orange
-rw-rw-r--. 1 root root  128 Jun 27 09:03 a.lookup_apple.sh
-rw-rw-r--. 1 root root  129 Jun 27 09:04 b.lookup_orange.sh
drwxrwxr-x. 5 root root   74 Jun 15 13:32 venv
 주의사항: --exclude 로 제외할 디렉토리를 넣을때 이름만 넣어야 합니다.


즉, 

 

( O ) tar cvfz fruits.tar.gz --exclude=venv ./*

( X ) tar cvfz fruits.tar.gz --exclude=venv/ ./*

 

 

>>> venv/ 로 넣으면, 디렉토리로 인식을 하지 않으므로 제외가 되지 않습니다.

[root@buddy2]# tar cvfz fruits.tar.gz --exclude=venv/ ./*
./1.lookup_apple/
./1.lookup_apple/list.txt
./1.lookup_apple/main.py
./2.lookup_orange/
./2.lookup_orange/main.py
./a.lookup_apple.sh
./b.lookup_orange.sh
./venv/
./venv/include/
./venv/lib/
./venv/lib/python3.9/
./venv/lib/python3.9/site-packages/
./venv/lib/python3.9/site-packages/distutils-precedence.pth
./venv/lib/python3.9/site-packages/easy_install.py
./venv/lib/python3.9/site-packages/_distutils_hack/
./venv/lib/python3.9/site-packages/_distutils_hack/__init__.py
./venv/lib/python3.9/site-packages/_distutils_hack/override.py

..
...
.....

 

2.여러 파일/디렉토리 섞어서 제외하고 압축 하기

--exclude 옵션을 여러번 넣고 진행 하면 됩니다.

명령어: tar cvfz fruits.tar.gz --exclude=1.lookup_apple --exclude=b.lookup_orange.sh --exclude=venv ./*

 

fruits.tar.gz 로 압축하고 대상은 모두 (하위포함) ./* 이며 아래 경로는 제외 합니다.

 

  • 1.lookup_apple 디렉토리 제외 (--exclude)
  • b.lookup_orange.sh  파일 제외 (--exclude) 
  • venv  디렉토리 제외 (--exclude) 
[root@buddy2]# ls -al
total 6920
drwxrwxr-x. 2 root root      37 Jun 29 06:50 1.lookup_apple
drwxrwxr-x. 2 root root      21 Jun 27 09:06 2.lookup_orange
-rw-rw-r--. 1 root root     128 Jun 27 09:03 a.lookup_apple.sh
-rw-rw-r--. 1 root root     129 Jun 27 09:04 b.lookup_orange.sh
drwxrwxr-x. 5 root root      74 Jun 15 13:32 venv



[root@buddy2]# tar cvfz fruits.tar.gz --exclude=1.lookup_apple --exclude=b.lookup_orange.sh --exclude=venv ./*
./2.lookup_orange/
./2.lookup_orange/main.py
./a.lookup_apple.sh