4

I'm trying to evaluate storage usage on a linux server.

Most of the bulk of storage is coming from the images. I'd like to see if I can get a sense of the size of files modified in the last year. and maybe also the last 5 years, to see how that compares to the total size of the images directory (which is 40GB)

I have tried du -h and that is handy, but I cannot see how to filter those results by modified date.

3 Answers 3

4

If the num-utils package is installed, numsum provides an easy way to add many numbers; while numfmt simplifies the output:

find . -type f -mtime -365 -printf '%s\n' | numsum | numfmt --to=iec

(find code borrowed from Ziggy Crueltyfree Zeitgeister.)

For the last 5 years do:

find . -type f -mtime -$((365 * 5)) -printf '%s\n' | numsum | numfmt --to=iec

If an accurate day count is needed, date will provide that. Here's a shell function that returns the exact number of days in the last n years:

# covert Last _n_ Years to Days
# usage:  ly2d n
ly2d() \
    { echo $(( \
               $(( `date -d today +%s` - \
                   `date -d $1" years ago" +%s` )) \
           / 60 / 60 / 24 )) ; \
    }

Using ly2d for the last 5 years:

find . -type f -mtime -$(ly2d 5) -printf '%s\n' | numsum | numfmt --to=iec
2
  • i seem to not have numfmt available. does that require a different library?
    – Damon
    May 9, 2016 at 17:09
  • Debian's coreutils package has numfmt, or check the upstream source. If that fails, remove the numfmt, the sum would then be in bytes.
    – agc
    May 9, 2016 at 22:21
2

You can use perl to sum the output of find:

find . -type f -mtime -365 -printf '%s\n' |\
  perl -e 'my $s=0; while(<>) { $s += $_; } print "$s\n"'
3
  • 3
    If the num-utils package is installed, numsum is simpler than all that perl: find . -type f -mtime -365 -printf '%s\n' | numsum
    – agc
    May 9, 2016 at 7:10
  • Appending | numfmt --to=iec helps too.
    – agc
    May 9, 2016 at 7:20
  • @agc You should post it as an answer, no reason not to. I would happily upvote it. May 9, 2016 at 8:12
0

The following approach has the benefit relying only on common utilities du and find, which are available out of the box on pretty much all Linuxes.

du -hc $(find . -type f -mtime -365)

This works by finding all of the modified files using find, then passing all those files to du to calculate the sizes. The -c option ensures a summed total is printed at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.