Sometimes I need to calculate a total disk usage of files older than a specified interval (days, months, years).
Here is a 1 liner to do that:
find . -type f -mtime +7 -print0 | du -hc --files0-from - | tail -n 1
Detailed info ?
Use find
to search for files (-type f
) modified more than 7 days (-mtime +7
). Print the full file name on the standard output, followed by a null character (instead of the newline character that -print
uses).
Pass files list to du
to get a total disk usage (-h
), in human readable format (-h
), of the NUL-terminated file names specified from standard input (--files0-from -
).
Finally, show only the last line, Total
from du
output (tail -n 1
).
Other examples
- Find disk usage of files older than 1 year:
find . -type f -mtime +365 -print0 | du -hc --files0-from - | tail -n 1
- Find and
!!! DELETE !!!
files older than 1 year:
find . -type f -mtime +365 -print -delete
Have fun :)