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

  • 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

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'
  • 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

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

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

  • "$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. Oct 23 '09 at 12:41

The exclusions can also be done by find :

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

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy