Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.