Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm currently using this: tar -pczf backup.tar.gz *

to tar.gz everything. There are some other backups though (in other folders), these are typically the only files > 50mb. How can i exclude them? Also, anyone know how i can exclude jpg/gif/pngs? thanks

share|improve this question

migrated from stackoverflow.com Oct 25 '09 at 7:41

This question came from our site for professional and enthusiast programmers.

    
If the other backups are also .tar.gzs then you may as well exclude them the same way you exclude jpg/gif/pngs –  Artelius Oct 23 '09 at 12:04

5 Answers 5

tar has the options to exclude files matching certain patterns, though not size.

--exclude=PATTERN
exclude files matching PATTERN
-X, --exclude-from=FILE
exclude files matching patterns listed in FILE

"ubuntuforum" has a nice script to backup files and exclude files based on size.

share|improve this answer

You can combine tar with find to select only the files of a certain size: find . -size -50M returns a list of all the files under 50 MB, which you can then pipe to tar.

Edit Didn't notice your second question when I first answered: in order to exclude files based on their extension, just pipe the output of find to grep: find ... | fgrep -v \*.jpg \*.png etc. (untested).

share|improve this answer
    
That being said, Artelius is right: what you want to exclude from your general backup is, I suppose, all the partial backups that you already made, so you may want to detected them by their file extension (tar.gz etc.), not their size. –  Arthur Reutenauer Oct 23 '09 at 12:16

Extending Arthur's suggestion, tar's r option is for append. So something like this might do it.

 find /path/to/dir  -size -50M -exec tar -rvpf backup.tar \;

EDIT: -exec needs terminating string "\;" (w/o quotes).

You can't append to a compressed archive, so you will have to gzip it (or bzip2 it) separately.

Combine with wds' solution to exclude images.

share|improve this answer
    
"$PATH" is probably a problematic choice for this variable as it has a well-defined meaning and generally doesn't refer to a single directory. –  Joachim Sauer Oct 23 '09 at 12:41

you can pass the list of files to tar directly (with the T switch), i.e.:

find dir/ -size -50M | tar -czf test.tgz -T - --exclude '*.gif' --exclude '*.jpg'
share|improve this answer
    
This will work up to the point that the number of arguments to tar exceeds some limit. That is, when the output from find becomes large. Beyond that point, you need to bring the tar call into find's -exec arg. –  Ewan Oct 23 '09 at 12:23
    
The tar documentation documents no such limitation on the size of the input to -T, so I have no idea what you're talking about really. –  wds Oct 23 '09 at 12:28
    
It's not a -T limitation. It's a shell thing. Try it. –  Ewan Oct 23 '09 at 12:31
3  
If you're talking about the command line size limitation, that's not an issue here, as we're piping the output of find directly into tar. –  wds Oct 23 '09 at 12:33
    
Alright. I cannot reproduce it with 183971 files. In fact, I remember now that when I saw this issue it was to do with xargs. I had to change a find that piped to xargs for a tar append call into a find with an inline exec call for tar. –  Ewan Oct 23 '09 at 12:49

The exclusions can also be done by find :

find -maxdepth 1 -size -50M -not -regex ".*/.*\.\(jpg\|gif\|png\)$" | tar . . .
share|improve this answer

Your Answer

 
discard

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