Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.
tar -cvf file.tar --exclude=thumbs/ \
                  --exclude=uploads_event/ \
                  --exclude=uploads_forum/ \
                  --exclude=uploads_admin/ \
                  --exclude=uploads_userpoints/ \
                  --exclude=uploads_group/ \
                  --exclude=up_old/ \
                  --exclude=uploads_user/ \
                  --exclude=uploads_wall/ \
                  directory_to_tar/

Do I have it wrong? Trying to tar entire directory but exclude all those directories and any files in those folders completely.

share|improve this question

2 Answers 2

With GNU tar you can't include the trailing slash on all those excludes. It'd work with BSD tar, though.

share|improve this answer
1  
+1 for being much more correct in many fewer words than me =) –  jj33 Jan 7 '11 at 19:45

Michael Lowman's answer should be accepted, but since I made up some examples anyway I'm going to generalize it and leave it, maybe someone will find value in it.

To repeat Michael's answer, it appears that the trailing slashes on your --exclude options are your problem.

I'll also note that when you have a ton of stuff to exclude, it can be easier to put it in a file and just include the file with the -X option.

Here are a couple of practical examples of using --exclude and -X

g3 0 /home/jj33/swap/t > mkdir -p dir/d1 dir/d2
g3 0 /home/jj33/swap/t > touch dir/{d1,d2}/{f1,f2,f3}
g3 0 /home/jj33/swap/t > find . -type f
./dir/d1/f1
./dir/d1/f2
./dir/d1/f3
./dir/d2/f1
./dir/d2/f2
./dir/d2/f3

#### using the trailing slash, as in your question, doesn't work:
g3 0 /home/jj33/swap/t > tar -cvf file.tar --exclude=d1/f2 --exclude=d2/ dir
dir/
dir/d1/
dir/d1/f1
dir/d1/f3
dir/d2/
dir/d2/f1
dir/d2/f2
dir/d2/f3

# removing the / works better
g3 0 /home/jj33/swap/t > tar -cvf file.tar --exclude=d1/f2 --exclude=d2 dir      
dir/
dir/d1/
dir/d1/f1
dir/d1/f3

# example using a file and -X
g3 0 /home/jj33/swap/t > echo d1/f2 >exclude.txt
g3 0 /home/jj33/swap/t > echo d2 >>exclude.txt  
g3 0 /home/jj33/swap/t > cat exclude.txt 
d1/f2
d2
g3 0 /home/jj33/swap/t > tar -cvf file.tar -X exclude.txt  dir              
dir/
dir/d1/
dir/d1/f1
dir/d1/f3
g3 0 /home/jj33/swap/t >
share|improve this answer
    
no, you can use --exclude with a non-trailing slash directory name and that directory and its contents will not be included in the tar file. try it out :) of course, your way works just as well. It just takes a little extra effort compared to just taking out the trailing slashes –  Michael Lowman Jan 7 '11 at 19:40
    
heh, I was already typing up an edit based on your answer before I saw your comment. You are absolutely right. I honestly don't know if my belief is based on old behavior or I've just always been wrong =). –  jj33 Jan 7 '11 at 19:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.