tags:

views:

109

answers:

3

I want to delete all files in a folder that are less than 200 in size.

Just want to be sure here, when i do a ls -la on my macbook, the file size says 171 or 143, I am assuming this is kb correct?

+7  A: 

This does directory and all subdirectories:

import os, os.path

for root, dirs, files in os.walk(dir):
    for f in files:
        fullpath = os.path.join(root, f)
        if os.path.getsize(fullpath) < 200 * 1024:
            os.remove(fullpath)

Or:

import os, os.path

fileiter = (os.path.join(root, f)
    for root, _, files in os.walk(dir)
    for f in files)
smallfileiter = (f for f in fileiter if os.path.getsize(f) < 200 * 1024)
for small in smallfileiter:
    os.remove(small)
hughdbrown
That's almost exactly what I was thinking :) +1
JoshD
+1  A: 

Generally ls -la is in bytes.

If you want it in "human readable" form, use the command ls -alh.

Thomas
+10  A: 

you can also use find

find /path -type f -size -200k -delete
ghostdog74
+1 This is a cool, unix-y answer.
hughdbrown
Nice! Another +1 from me, cool answer.
Santiago Lezica