- Forums
- Linux Systems
- How To Exclude A Directory When You Tar Linux Command
this page will show you the linux command you need to execute fo how to exclude a directory in a tar linux command [3018], Last Updated: Sat May 18, 2024
wallpaperama
Sun Oct 12, 2008
7 Comments
13496 Visits
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