find by Size, Age & Then Act
Filtering by size and time - then doing something
This is where find becomes a superpower: match on size or age, then run a command on every match.
| Test | Matches |
|---|---|
-size +100M | Larger than 100 MB (- = smaller) |
-mtime -7 | Modified in the last 7 days |
-mtime +30 | Not modified in over 30 days |
-user alice | Owned by alice |
-perm 777 | With exact permissions |
Act on results with -exec or -delete:
# The disk-hog ticket, solved:
$ find /var -type f -size +100M -exec ls -lh {} \;
# Delete logs older than 30 days:
$ find /var/log -name '*.log' -mtime +30 -delete
In -exec, {} is each matched file and \; ends the command.
Danger:find ... -deleteand-exec rmare irreversible - there is no recycle bin. Always run the search first withlsto see what will match, then add the delete. Pros do this every single time.