8

I'm trying to archive files with tar and ignoring files larger than 100MB. Is that possible?
PS: Not have to use tar.

Alvar
  • 17,058
Faruk Uzun
  • 1,280

2 Answers2

9

Yeah there are probably a few more ways of doing this but you're probably best off with find as it's so tunable.

find /path/to/dir -size -100M | xargs tar cvf archive.tar

or

tar cvf archive.tar $(find /path/to/dir -size -100M)

If there are other directories in the current folder which you do not want to archive, then use:

find /path/to/dir -maxdepth 1 -size -100M | xargs tar cvf archive.tar

Per the comments below, spaces can throw out some errors but in my testing they still seemed to get added to the archive when using the first method (a little strange in itself) but if you're worried, you can fix the find command to replace spaces with escaped versions.

As ever, there are a billion ways of doing this.

find /path/ -size -100M | sed 's/ /\\ /g' | xargs tar cvf archive.tar

find /path/ -size -100M -exec echo '"{}"' \; | xargs tar cvf archive.tar
Oli
  • 293,335
  • 2
    Both options could give problems if the OP has filenames containing spaces. – enzotib Feb 21 '12 at 12:20
  • ... agreed - will need to use -print0 in the find command as well as --null for the tar command to cope with this scenario. – fossfreedom Feb 21 '12 at 13:12
  • While creating a huge archive tar cvf myfile.tar command fails. I strongly suggest use to tar rvf myfile.tar command. – Faruk Uzun Feb 21 '12 at 16:21
  • In case someone uses this to filter out files of smaller size, it might be useful to read this first: https://unix.stackexchange.com/questions/275925/why-does-find-size-1g-not-find-any-files . TL;DR -size -1k doesn't work as expected, and might select only files that have a size of 0. You might need to use ! -size +1k. (Same for 1M, 1G, etc.) – Michele Piccolini Aug 31 '21 at 13:26
  • And in case you use xargs, make sure to add -d '\n' or xargs will break if find finds files with whitespaces in their names. See https://stackoverflow.com/questions/16758525/make-xargs-handle-filenames-that-contain-spaces – Michele Piccolini Aug 31 '21 at 13:51
0

Just exclude the big files like this:

tar cf archive.tar /path/to/archive --exclude-from <(find /path/to/archive -size +100M)

eshwar
  • 808