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.

lese
  • 2,726

2 Answers2

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.

Joe
  • 544
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 $_` }'
Sobrique
  • 4,424