Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does it seem completely random if ls -lrt returns last modifed year or not?

Tags:

unix

ls

Here are the results of a ls -lrt command in unix:

-rw-r--r--   1 blah      blah          846       Apr 18  2013 filetype1.log
-rw-r--r--   1 blah      blah          290338533 Jan  3 00:59 filetype2.log

Why does one conveniently return the year while the other just has month, day, and time stamp? How can I always get the year?

EDIT: Here's an example where the last edit time was NOT within the current year and still no year:

-rw-r--r--   1 blah      blah       689466 Dec 31 23:59 filetype2.log
-rw-r--r--   1 blah      blah       689591 Jan  1 00:59 filetype2.log
like image 838
Hershizer33 Avatar asked Dec 25 '22 15:12

Hershizer33


2 Answers

From the GNU Coreutils manual:

10.1.6 Formatting file timestamps

By default, file timestamps are listed in abbreviated form, using a date like 'Mar 30 2002' for non-recent timestamps, and a date-without-year and time like 'Mar 30 23:45' for recent timestamps. This format can change depending on the current locale as detailed below.

A timestamp is considered to be "recent" if it is less than six months old, and is not dated in the future. If a timestamp dated today is not listed in recent form, the timestamp is in the future, which means you probably have clock skew problems which may break programs like 'make' that rely on file timestamps.

like image 101
BRPocock Avatar answered May 16 '23 06:05

BRPocock


Based on discussion in comments and chat, you're trying to get the modification time of a file on a remote system in a C# program, and you're not able to install any software on the remote system (which is running HP-UX).

(You also mentioned file creation time, but Unix doesn't store that information, just modification time, access time, and time of most recent inode change).

Since the system doesn't have GNU Coreutils installed, you can't use either ls --time-style=... or the stat command.

But since it does have Perl, you can obtain a file's modification time with a very simple Perl script, using the built-in stat function.

The simplest way to display a timestamp from Perl is as the number of seconds since the Unix epoch, 1970-01-01 00:00:00 GMT; that's also likely to be the simplest format to parse from a C# program.

Create this script on the remote system:

#!/usr/bin/perl

use strict;
use warnings;

foreach my $file (@ARGV) {
    my @stat = stat $file;
    if (defined $stat[9]) {
        print "$stat[9] $file\n";
    }
    else {
        die "$file: $!\n";
    }
}

and make it executable (chmod +x script-file-name). You can then invoke it with one or more file names as command-line arguments; for each file, it will print its modification time and its name.

Parsing the output from your C# program is left as an exercise.

If you had the GNU Coreutils stat command, you could do this instead:

stat -c '%Z %n' filename

You can do something similar with ls:

ls --time-style=+%s filename

but that prints additional information and is harder to parse. ls output is intended to be human-readable, not necessarily machine-parsable -- which is probably part of the reason the stat command exists.

like image 31
Keith Thompson Avatar answered May 16 '23 08:05

Keith Thompson