27
  1. I am looking for a linux command to get all the files exceeding a certain size from the current directory and its sub-directories.

  2. Whats the easiest way to delete all these files?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ran
  • 3,455
  • 12
  • 47
  • 60
  • Also see [How can I find files that are bigger/smaller than x bytes?](http://superuser.com/q/204564/173513) on Super User. – jww Feb 07 '17 at 22:53

4 Answers4

67

Similar to the exec rm answer, but doesn't need a process for each found file:

find . -size +100k -delete
Erik
  • 88,732
  • 13
  • 198
  • 189
16

One-liner:

find . -size +100k -exec rm {} \;

The first part (find . -size +100k) looks for all the files starting from current directory (.) exceeding (+) 100 kBytes (100k).

The second part (-exec rm {} \;) invoked given command on every found file. {} is a placeholder for current file name, including path. \; just marks end of the command.

Remember to always check whether your filtering criteria are proper by running raw find:

find . -size +100k

Or, you might even make a backup copy before deleting:

find . -size +100k -exec cp --parents {} ~/backup \;
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • "Remember to always check whether your filtering criteria are proper by running raw find" was very useful (can't be too careful with the unforgiving `rm` in Linux!), so I'm upvoting this. – warship Jul 31 '17 at 23:03
2

python is installed on all unix based OS, so why not use it instead of bash ?

I always find python more readable than awk and sed magic.

This is the python code I would have written:

import os
Kb = 1024 # Kilo byte is 1024 bytes
Mb = Kb*Kb
Gb = Kb*Kb*Kb
for f in os.listdir("."):
    if os.stat(f).st_size>100*Kb:
        os.remove(f)

And this is the one-liner version with python -c

python -c "import os; [os.remove(f) for f  in os.listdir('.') if os.stat(f).st_size>100*1024]"

And if you want to apply the search recursively, see this

Midavalo
  • 469
  • 2
  • 18
  • 29
Uri Goren
  • 13,386
  • 6
  • 58
  • 110
1

In zsh:

ls -l *(Lk+100)   # list file size greater than 100kb

so:

rm *(Lk+100)

More zsh goodness here.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • This is not recursive. Use `**/*(.Lk+100)` #The dot exclude directories, whose size is irrelevant (and spurious when seeking for small files). – YvesgereY Jan 07 '14 at 19:23