3
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.

jj33
  • 11,178
  • 1
  • 37
  • 50
Andrew Fashion
  • 1,655
  • 7
  • 22
  • 26

2 Answers2

8

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

Michael Lowman
  • 3,604
  • 20
  • 36
2

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 >
jj33
  • 11,178
  • 1
  • 37
  • 50
  • 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 07 '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 07 '11 at 19:44