Track size of a directory over time

directorylogssize;

I have a directory and I want to log, from now on, its size every day. That is, at some time of day, every day, I want to compute the size of this directory, and append it in some file along with the date.

NOTE: By size of the directory I mean the size of the all the contents, recursively.

What's the recommended way of doing this?

Best Answer

You could run something du from a cron job. With GNU tools, du -sb dir/ would give you the size in bytes of the directory and all files within it, recursively (plus the directory name, but we can remove that). And you can get the date with date. E.g.

$ printf "$(date +"%F %T") $(du -sb /tmp)\n" 
2018-07-03 15:25:57 24246930    /tmp

Then put that in a cron job and direct the output to a file. A crontab entry to run at 06:00 every day could be something like this (of course, I'm only using /tmp here as just an example):

0 6 * * * printf "$(date +"\%F \%T") $(du -sb /tmp)\n" >> /tmp/tmp-size.log

The percent signs need to be escaped for cron.

You could use du -sk for kilobytes, or du -sh for "human-readable" auto-scaling output. The options accepted by du may be different on another system.

Use something like du ... | sed -e 's/[[:blank:]].*//' instead, if you want to remove the pathname that du prints.

Related Question