How to tar.gz everything under xx MB?

4

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

nixnub

Posted 2009-10-23T12:01:25.700

Reputation:

If the other backups are also .tar.gzs then you may as well exclude them the same way you exclude jpg/gif/pngs – None – 2009-10-23T12:04:25.477

Answers

5

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'

wds

Posted 2009-10-23T12:01:25.700

Reputation: 210

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. – None – 2009-10-23T12:23:37.590

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 – 2009-10-23T12:28:32.573

It's not a -T limitation. It's a shell thing. Try it. – None – 2009-10-23T12:31:05.017

3If 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 – 2009-10-23T12:33:22.530

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. – None – 2009-10-23T12:49:06.197

That's what xargs is there to prevent. You must have had the shell expand "*" into too many things. – glenn jackman – 2009-10-23T13:21:25.133

just to sum it up: there is no limitation whatsoever regarding the -T flag of tar. tar will read the filenames from a 'file' called 'stdin'. it has absolutely nothing to do with 'number of arguments the shell can handle'. fullstop. – akira – 2009-10-25T08:01:03.633

Recent Linux kernels eliminate the need for xargs entirely. This happened sometime around 2.6.23, if I'm remembering right. – kquinn – 2009-10-25T10:12:39.127

3

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

Arthur Reutenauer

Posted 2009-10-23T12:01:25.700

Reputation: 131

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. – None – 2009-10-23T12:16:14.507

1

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.

Ewan

Posted 2009-10-23T12:01:25.700

Reputation: 111

"$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 – 2009-10-23T12:41:54.200

1

The exclusions can also be done by find :

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

Paused until further notice.

Posted 2009-10-23T12:01:25.700

Reputation: 86 075

0

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.

secureBadshah

Posted 2009-10-23T12:01:25.700

Reputation: 1 411