how to exclude a directory in a tar linux command

Mobile
feeds
Welcome Login | Register

how to exclude a directory in a tar linux command
Reply Your Comments:
Click this button if you are interested in replying to this topic and leave your comments
Sent To Friend
CLick this button if you want to send this page to a friend.
Subsribe To Rss Feeds
Subscribe to RSS
CLick this button if you want to subscribe to this RSS Feed. You can use your browsers feeds burners if you have mozilla or internet explorer 7 or higher and keep up with updates.
  Forums Index
      » Linux Forums
        » » how to exclude a directory in a tar linux command
how to exclude a directory in a tar linux command
Post Description: how to exclude a directory in a tar linux command Linux
Post Tags:
This Post Has Been Viewed 582 Times Since Sun Oct 12, 2008 4:08 pm Author hostman with 5 replies
Next Post »» how to set static ip red hat linux 4
how to exclude a directory in a tar linux command
Advertise On This Page




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


Leave Your Comments

Your Name
Your Email Address (Will Not Be Published)
Notify Me When Someone Replies to this Page
(An email will be sent to you when someone replies to your comments)
Your Comments
Include A Picture with your comments
Share
| More
Share this page by putting this URL in your comments to other websites like myspace, Facebook, Twitter friendster, Hi5, Groups, Boards, Forum or others. Just Copy and Paste this Code
URL:
To embed this topic, just copy the code from the "Embed" box. Once you've copied the code, just paste it into your website or blog to embed it.
Embed:
BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums
BBCODE:
Subscribe Feeds
Webmasters - Exchange Links With Us. Add related websites to this topic
Add Link:
Links Related to : how to exclude a directory in a tar linux command

Comments and replies About how to exclude a directory in a tar linux command
:: 1 :: Reply #75185 Reply By wallpaperama On Sun Oct 12, 2008 4:10 pm
wallpaperama:
for zip, you use the -x option instead of --exclude
:: 2 :: Reply #75192 Reply By wallpaperama On Sun Oct 12, 2008 4:39 pm
wallpaperama:
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 :: Reply #100052 Reply By tzwil On Sat Apr 11, 2009 4:15 pm
tzwil:
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 :: Reply #130231 Reply By serg On Sat Mar 20, 2010 12:09 am
serg:
Thank you

It helped
:: 5 :: Reply #136593 Reply By ir0nwarez On Mon Jun 21, 2010 7:08 am
ir0nwarez:
It works perfectly to me!


Thanks for your tutorial