Handy Linux tricks with find

I often find the need to delete files of a particular size. Instead of viewing files in a graphical interface, arranging by size and then picking the ones to delete, there is an easier way. This is taken from the following SE posts:


As mentioned there, the + or - prefixes are neither documented in the man pages nor within info. So in order to find files greater than 10MB but less than 100MB, use this:

find  .  -size +10M -size -100M  -exec rm -f  -- {} \;

The syntax of why the backslash and semi-colon is required is described here: semi-colon within find. The double-dash (--) tells the getopt interpreter that rm uses to ignore anymore options, and so allow deletion of filenames that begin with a dash(-). (Why would anyone have filenames like those👺: rm with special characters)

Alternatively, you could use xargs for the deletion.

find  .  -print0  -size +10M -size -100M  |  xargs  -0 rm -f  --

The arguments “-print0” and “-0” are required to interpret files with spaces in the names correctly. (find and xargs).

If you want to list all files based on their modification times, use this:

find  .  -type f  -mtime 0

will show all files in your current directory modified (or created) within the past 24 hours. If you’d like find to count from the beginning of today (rather than 24 hours ago), use the option “-daystart” before any time options.

The same + or - prefixes can be used here to limit the search to strictly greater/less than conditions.

ps: David Wheeler has written an excellent article on the ridiculous use of certain characters in POSIX filenames: No Sharp Corners