Title: how to exclude a directory in a tar linux command
Description: this page will show you the linux command you need to execute fo how to exclude a directory in a tar linux command Linux
Tags: linux,computer
Info: This Post Has Been Viewed 12209 Times SinceSun Oct 12, 2008 4:08 pm Author hostman With 7 Replies #6486

how to exclude a directory in a tar linux command

ok, if you are like me, i do backups all the time, and the best way to backup my files is to create tarballs. i have a linux server and i use it to host my wallpaper websites. as an admin, i have to backup my files frequently. today i came accross a problem. i wanted to create a backup file but i didnt want to include this huge file so i wanted to exclude it from my backups. but then i noticed that i had another directory i didnt want to be included in my backups. so now i have multiple (two or more) directories that i wanted to avoid including in my tar file. excluding these files would make my backups smaller and thus saving me space.

so the questions is, how do you tar a backup without including these two directories?

well the answer is easy, when you are done reading this short tutorial, you will learn how you can exclude a file or a directory in your tars.

ok, here is my example,

1. - i want to keep the same permission on each file and directory (-pczf)
2. - the name of my backup tar file is called mywebsites.tar.gz
3. - i wanted to backup my entire /var/www directory
4. - the two directories i did not want to put in my tar backup are:
    - /var/www/mydomain/images/
    - /var/www/otherdomain/pictures/

now that i have all these elements, i can construck the shell command im going to use in my linux server. so here is the command


tar -pczf mywebsites.tar.gz /var/www/ --exclude="/var/www/mydomain/images/" --exclude="/var/www/otherdomain/pictures"


so basically if you want to exclude a file that you dont want to be included in your tar, just use the --exclude option. easy huh

thats it

hope this helps

UPDATE, you can try this small test tutorial to see how it works: just send these command when you are logged into your shell terminal:


lets make a test directory and two other directories, one good and the other bad.
mkdir -p test/good test/bad


now lets create the archive of the test directories and its subdirectories, but exclude the bad/ directory
tar -pczf test.tar.gz test --exclude="test/bad"


we remove the old test directory
rm -rf test


now we extract the test directory
tar xvfz test.tar.gz



you will see a the verbose:

test/
test/good/

shows that test/bad/ was not created in the tarball

Comments (7)
View Top Comments
Leave Your Comments...
#1
wallpaperama:
3 years ago
#75185
wallpaperama Sun Oct 12, 2008 4:10 pm
for zip, you use the -x option instead of --exclude
#2
1
wallpaperama:
3 years ago
#75192
wallpaperama Sun Oct 12, 2008 4:39 pm
ok here is a tutorial you can follow. login as root and then follow these steps:

1. create some test files in your root directory for us to play with

mkdir /root/tartest
mkdir /root/tartest/mydirectory
mkdir /root/tartest/mydirectory/avoid
mkdir /root/tartest/mydirectory/not_tar
mkdir /root/tartest/mydirectory/tarred


2. once you have create our test directories, go to our test directory:
cd /root/tartest


3. we just created three directories, our of all these three, we are only going to tar the directory called tarred/ and leave out avoid/ and not_tar/ so you send this command to do that:

tar -pczf mydirectory.tar.gz mydirectory/ --exclude "mydirectory/not_tar" --exclude "mydirectory/avoid"


4. now send the list command to see the new tar file you just created
ls


5.. now we just rename our original mydirectory/ to mydirectory-old
mv mydirectory mydirectory-old


6. now we untar the files
tar xvfz mydirectory.tar.gz


7. you will only see that the directory called tarred/ was the only one included in our tar. this is the output:
[root@host tartest]# tar xvfz mydirectory.tar.gz
mydirectory/
mydirectory/tarred/
[root@host tartest]#



8. these commands are if you want to try again from step three:

rm -rf mydirectory
rm -rf rm -rf mydirectory.tar.gz
mv mydirectory-old mydirectory

#3
tzwil:
3 years ago
#100052
tzwil Sat Apr 11, 2009 4:15 pm
thank you i wanted to know how i can exclude a directory or a file from the comannd and your tutorial helped in in excluding a file that i didnt want to include when i send the linux shell command it didnt included the .sql file i didnt want to be packaged which is the exception
#4
serg:
2 years ago
#130231
serg Sat Mar 20, 2010 12:09 am
Thank you

It helped
#5
ir0nwarez:
1 year ago
#136593
ir0nwarez Mon Jun 21, 2010 7:08 am
It works perfectly to me!


Thanks for your tutorial
#6
MightyFeldar:
8 months ago
#164862
MightyFeldar Thu Aug 18, 2011 3:09 am
I'm not sure if it's a version issue or not, but --exclude did not work for me without an '=' before the value: --exclude='www/dirtoexclu
#7
1
pierre:
7 months ago
#166686
pierre Thu Sep 15, 2011 2:45 am
I have to had * to exclude files --exclude="/var/www/html/
Leave Your Comments...
Share
| More