15

How do I recursively remove files that are less than 1MB in size from a directory?

1

5 Answers 5

25

This can be done with find:

find . -type f -size -1M -exec rm {} +

Note that this will recursively descend into subdirectories, and will unconditionally delete all files smaller than 1 megabyte. Be careful.

9
  • you're missing the path argument to find
    – Useless
    Feb 8, 2012 at 12:25
  • @Useless: That's GNU find. :) Feb 8, 2012 at 12:29
  • 2
    @DanielAndersson: find restricts the number of arguments to the called process to fit into the system's limits, in contrast to rm *, which is guranteed to be a single process invocation. find will invoke multiple instance of rm if necessary. And I'm pretty sure that special characters are treated correctly, including newline characters. I prefer -exec rm over -delete for flexibility reasons -- as an example, the latter offers no way to delete write-protected files. Feb 13, 2012 at 11:57
  • 1
    @Invoker: I reverted your change since it was incorrect. -1M means less than one megabyte as desired. Your version would delete all files with exactly one megabyte in size, which seems to be a somewhat pointless operation. Oct 5, 2015 at 15:28
  • 2
    For anyone interested, if you want to remove all files greater than 1M, use the command find . -type f -size +1M -exec rm {} +. Note the +1M instead of -1M. Oct 6, 2015 at 20:36
11

This should do the job:

$ find <directory> -type f -size -1M -delete
3
  • I don't think we need to out hyphen in from of 1M.
    – Invoker
    Oct 5, 2015 at 14:42
  • 3
    @Invoker, I believe the - sign is a minus sign meaning "less than 1M". If you run find <directory> -type f -size +1M -delete you will delete all files larger than 1M. Oct 6, 2015 at 20:10
  • yes you are right my bad
    – Invoker
    Oct 9, 2015 at 22:37
2

Just for variety and a possible (probably marginal) performance gain:

find <directory> -type f -size -1M -print0  | xargs -0 rm
2
  • How is this supposed to be faster? It starts an additional xargs process. Feb 8, 2012 at 12:33
  • Now you can have two CPUs contending for the same block device! More sensibly, the stat/readdir operations aren't synchronously blocked by the unlink operation. Whether this is likely to be better obviously depends on the subtree size, number of files, device etc.
    – Useless
    Feb 8, 2012 at 12:36
1

Try

find . -size -1M -exec rm {} \;

1
  • 1
    This is great for non-GNU users. Thanks! the same as @Sven's answer, but with \; at the end instead of +
    – hamx0r
    Aug 18, 2016 at 18:16
-1

You can checkout this link http://ayaz.wordpress.com/2008/02/05/bash-quickly-deleting-empty-files-in-a-directory/ , it has exactly what you want.

for file in *;
  do
    file_size=$(du $file | awk '{print $1}');
    if [ $file_size == 0 ]; then
        echo "Deleting empty file $file with file size $file_size!";
        echo "rm -f $file";
    fi;
done

You can iterate through all the files with a for loop and then use du and awk to find the filesize like in the above example.

5
  • Answers on SO should be self-contained -- don't post a mere link. (Moreover, the code in the linked post deletes empty files rather than files smaller than 1M.) Feb 8, 2012 at 12:32
  • @SvenMarnach can't we use $file_size < 1M in the given code example link.
    – Ravia
    Feb 8, 2012 at 12:36
  • No, we can't, since the shell won't understand 1M. Feb 8, 2012 at 12:43
  • By 1M I meant 1048576 converting 1MB to byte
    – Ravia
    Feb 8, 2012 at 13:01
  • 1
    Well, if you test if this really works and copy the code to your answer, this might become an SO answer. Feb 8, 2012 at 13:06

You must log in to answer this question.