How to list atime for files?

5,952

Solution 1

ls -lu

where -l will provide a long listing format and -u will sort by access time.

Solution 2

I've always used:

ls -l -u

Alternatively - maybe break out perl?

Perl can use the stat syscall directly

#!/usr/bin/env perl
use strict;
use warnings;
foreach my $filename ( @ARGV ) {
    print "$filename =>", (stat($filename))[8],"\n";
}

You can one-liner this as:

perl -e 'print "$_ ",(stat($_))[8],"\n" for @ARGV' <filename(s)>

If you want to do a prettier timestamp (rather than epoch which is what stat returns):

perl -MTime::Piece -e 'print "$_ ",Time::Piece->new((stat($_))[8]),"\n" for @ARGV'

or

perl -MTime::Piece -e 'print "$_ ",Time::Piece->new((stat($_))[8])->strftime("%F %T"),"\n" for @ARGV'

Which uses strftime and %F %T to give you:

2015-09-06 01:02:33

Expanding the script:

#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece; 
foreach my $filename ( @ARGV ) {
    my $epoch_time = (stat($filename))[8];
    my $time_string = Time::Piece -> new ( $epoch_time ) -> strftime ( "%F %T" );
    print "$time_string => $filename\n";
}

Solution 3

With FreeBSD stat:

stat -f '%Sa' file

%Sa mean you want file access time as String.

Share:
5,952

Related videos on Youtube

Jasmine Lognnes
Author by

Jasmine Lognnes

Updated on September 18, 2022

Comments

  • Jasmine Lognnes
    Jasmine Lognnes over 1 year

    According to this post can stat be used to give the atime on Linux, but FreeBSD 10.1 doesn't have the GNU stat.

    How do I list the atime for files?

    • jordanm
      jordanm over 8 years
      According to this manpage, FreeBSD's stat should contain the same functionality.
    • Rashini Gamalath
      Rashini Gamalath over 4 years
      @jordanm GNU coreutils stat also gives you access, modify, and change times, and it's by default, no extra arguments to make it happen (checked with coreutils version 8.26)
  • jordanm
    jordanm over 8 years
    FreeBSD's ls doesn't have --time.
  • Sobrique
    Sobrique over 8 years
    OK. Updated with an alternative :)