5

How can I list the size of folders as well as their changed date?

I am able to get the size of folders using du. I am also able to list the folders together with their date using

$ls  -ltr $path |  grep '^d'

But I cannot display the size and the date together.

ls -h or stat do not work for me.

2
  • Are you after a du-style sizing with modification time, or an ls style "directory size"?
    – Sobrique
    Dec 22, 2015 at 12:35
  • I'd say ls style "directory size"
    – user148335
    Dec 22, 2015 at 12:40

2 Answers 2

6

On a GNU system, man du reveals --time argument:

du -h --time .

which for directories shows the time of the last modification of any file in the directory, or any of its subdirectories.

2
  • --time is not available for my version
    – user148335
    Dec 22, 2015 at 12:45
  • Maybe something like find . -type d | xargs -I'{}' bash -c "stat --printf='%y ' {} & du -sh {}"
    – Joe
    Dec 22, 2015 at 12:48
1

Don't parse ls it's bad juju. My first start would be - use perl:

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

for ( grep { -d } glob ( "*") ) {
    print join ( "\t", $_, -s, (stat)[9] ),"\n";
}

But this of course, just takes the inode side of the directory, which is probably not what you want. So instead ... actually our easiest might actually be to call du anyway.

for my $dir ( grep { -d } glob ( "*") ) {
    print join ( "\t", (stat $dir)[9], `du -sh $dir`,  );
}

Or perhaps making use of the Filesys::DiskUsage module.

But as a one liner:

perl -e 'for ( grep {-d} glob ( "*" ) ) { print join "\t", (stat)[9], `du -sh $_` }'

Edit: With formatted timestamps:

perl -MTime::Piece -e 'for ( grep {-d} glob ( "*" ) ) { print join "\t", Time::Piece->new((stat)[9])->strftime("%Y-%m-%d"), `du -sh $_` }'

Edit: Version older than 5.9.5 (which you should really consider upgrading, given it's well past EOL):

perl -MPOSIX -e 'for ( grep {-d} glob ( "*" ) ) { print join "\t", strftime("%Y-%m-%d",gmtime((stat)[9]))), `du -sh $_` }'
8
  • perl works great it would be useful to display the seconds in a readable format (for example in mm-dd-yyyy). I have tried to add "+%m/%d/%y" to (stat)[9], but it does not work
    – user148335
    Dec 22, 2015 at 14:13
  • Easy enough. Added.
    – Sobrique
    Dec 22, 2015 at 14:18
  • not so in my case :( - Can't locate Time/Piece.pm in @INC .... Is there a way to maybe download it ?
    – user148335
    Dec 22, 2015 at 14:24
  • Yes, but it's been core in perl since 5.9.5 - which is quite an old version. You could probably use POSIX instead though.
    – Sobrique
    Dec 22, 2015 at 14:28
  • now it works , after removing one extra parentheses from strftime("%Y-%m-%d",gmtime((stat)[9])))
    – user148335
    Dec 22, 2015 at 14:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .