1

In have a big directory tree in OSX (Mountain Lion), and I need to delete all the files below a certain threshold filesize. I could create a duplicate of the directory excluding the small files, or simply delete them from the original directory.

I thought about using "find" but I can figure out how.

Thanks

2
  • Find has -size n[ckMGTP] Which is true if the file's size, rounded up, in 512-byte blocks is n. Is that precise enough or do you need to delete small files yet keep other than 512 bytes? (Syntax for files up to 10kb would be find . -size +10k -print. Combined with -exec and rm, or -delete)
    – Hennes
    Dec 12, 2012 at 21:19
  • Needless to say, first test with echo rather than rm. E.g. with find . -size +100k -exec echo Would delete {} \;
    – Hennes
    Dec 12, 2012 at 21:31

1 Answer 1

2

You can use find. To remove all files smaller than 12345 bytes:

find somedir -type f -size -12345c -delete

Please test this out before you use it! You can see which files will be affected by running the find without the delete first:

find somedir -type f -size -12345c

The c modifier to the size tells it to count bytes. Hennes above was on the right track, but both GNU find and the BSD find that comes with OSX support the c byte-count modifier. See the man page for find; there are other options available if you only care about the size in kilobytes or megabytes.

2
  • Or simply find … -delete – no need for the pipe and and xargs.
    – slhck
    Dec 12, 2012 at 21:47
  • Thanks. I have some sort of mental block against -delete. Been using it with xargs for like 20 years. :) Dec 20, 2012 at 17:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .