UNIX ls -l date format varies

1887 views shell
-5

I am using SunOS 5.10. I would like the contents of an "ls -l" to be directed into a file that can be read into a database. However the time format varies. Below is a sample of the output of an ls -l. Why do the files ls_txt.sh and nohup.out have a timestamp and not a year value?

-rw-rw-r--   1 gilmog   other         57 Jul 25  2017 fnd2.txt
-rw-rw-r--   1 gilmog   other        702 Jan 24  2018 handySh
-rw-rw-r--   1 gilmog   other        189 Nov  7 23:20 ls_txt.sh
-rw-------   1 gilmog   other       3915 Sep 12 03:58 nohup.out
-rw-rw-r--   1 gilmog   other       1655 Jan 24  2018 npiFn.sas  

answered question

1 Answer

0

Caution: do not parse the output of ls. Its output is meant for human consumption, to understand the contents of the filesystem. If you want a program to know time information about a file, use stat1.


Now, with that out of the way, I'll answer your question. The time varies because that's how it's defined to work. From the POSIX documentation on ls:

The field shall contain the appropriate date and timestamp of when the file was last modified. In the POSIX locale, the field shall be the equivalent of the output of the following date command:

date "+%b %e %H:%M"

if the file has been modified in the last six months, or:

date "+%b %e %Y"

(where two characters are used between %e and %Y ) if the file has not been modified in the last six months or if the modification date is in the future, except that, in both cases, the final produced by date shall not be included and the output shall be as if the date command were executed at the time of the last modification date of the file rather than the current time. When the LC_TIME locale category is not set to the POSIX locale, a different format and order of presentation of this field may be used.

This definition makes a horrible mess for a program to parse. So, to reiterate: do not parse ls output.


1 If you don't have stat on your Solaris box, then you might just have to rely on ls. I'm sorry. The command for that is approximately ls -siv -@ -/ c -%all z.

posted this

Have an answer?

JD

Please login first before posting an answer.