Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


share|improve this question
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 '12 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 '12 at 21:31
up vote 2 down vote accepted

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

find somedir -type f -size -12345c -print0 | xargs -0 rm -f

Please test this out before you use it! You can see which files will be affected by running the find without the rm 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.

share|improve this answer
Or simply find … -delete – no need for the pipe and and xargs. – slhck Dec 12 '12 at 21:47
Thanks. I have some sort of mental block against -delete. Been using it with xargs for like 20 years. :) – Jim Stewart Dec 20 '12 at 17:23

You must log in to answer this question.

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