LINK="#3366FF" VLINK="#A000A0">



[ Table Of Contents ][ Answer Guy Current Index ] greetings   Meet the Gang   1   2   3   4   5   6   7   8   9 [ Index of Past Answers ]


(?) The Answer Gang (!)


By Jim Dennis, Ben Okopnik, Dan Wilder, Breen, Chris, and the Gang, the Editors of Linux Gazette... and You!
Send questions (or interesting answers) to tag@lists.linuxgazette.net

There is no guarantee that your questions here will ever be answered. Readers at confidential sites must provide permission to publish. However, you can be published anonymously - just let us know!

TAG Member bios | FAQ | Knowledge base


(?) printing the timestamp of a given file

From Matthias Arndt

Answered By Dan Wilder

Dear Editor,

I was looking for a solution to extract the timestamp of a file with plain shell methods. So I browsed through a book and found the command cut.


ls -l <some filename> | cut -b44-55

does the job pretty well and simply prints the timestamp of the given file. It works with the GNU ls. If the output of your ls -l command differs, you'll need to adjust the positions after the -b switch. It even works with a list of filenames but this would only print the timestamps not the corresponding filenames.

cheers,
Matthias

(!) However "ls" produces dates in two different formats, according to the age of the file:
ls -l host.conf
-rw-r--r--    1 root     root           26 Sep 25  1995 host.conf

ls -l passwd
-rw-r--r--    1 root     root         1179 Nov 12 17:10 passwd

(?) The fields are correct in their width. The output should be ok in any case as long as ls doesn't format the long version with other field widths.

(!) OK, but annoyingly different in its format, according to the age of the file. That works for human readers, but causes complications for machine readers.
I use this sort of thing in web page scripting, where the output will be parsed by other programs, and it saves me time in writing those other programs.
(!) A slight elaboration allows consistent formatted output. As a shell script:

#!/bin/sh
date -d "$(/bin/ls -l $1 | cut -b44-55)" +"%b-%d-%Y"
I use "/bin/ls" to evade the likelyhood that "ls" may be an alias.

(?) I guess in most cases ls will not point to a much different binary. But it is more consistent.

(!) Also works for me, who normally makes "ls" an alias for something or other that changes with time.
(!)
mylittledatescript host.conf
Sep-25-1995

mylittledatescript passwd
Nov-12-2001

(?) Nifty! The whole script is much better than my quick'n'dirty solution. But it doesn't work on my machine.


$ ./filedate filedate
date: ung?ltiges Datum Dez 6 22:22'

which translates to "invalid date Dec ...." and stands for an error

Very strange....

(!) Araugh. "date" seems to change what it'll accept as input with each version ... hopefully it'll stabilize some day.

date --version
date (GNU sh-utils) 2.0.11
My little script will no doubt need modification if your version of date is different from the above.
(!) The "date" command does a great job of formatting a wide range of inputs. The "+" string to the date command offers many different output formats. See "man date".

(?) I didn't have the idea to pipe the output through date as well because the simple date field from ls is sufficient for me. This adds new perspectives however.

(!) A variation allows printing the Discordian date of a file using "ddate" which is much fussier about its input than "date":
#!/bin/sh
ddate $(date -d "$(/bin/ls -l $1 | cut -b44-55)" +"%d %m %Y")

mylittledatescript passwd
Sweetmorn, The Aftermath 24, 3167 YOLD
"ddate" also has formatting options.

(?) This notation does not make any sense to me. I don't need it now. But for someone who likes this sort of output, it might be handy.

many thanks,
Matthias

(!) A distraction, joke or diversion: http://www.kbuxton.com/discordia


This page edited and maintained by the Editors of Linux Gazette Copyright © 2002
Published in issue 74 of Linux Gazette January 2002
HTML script maintained by Heather Stern of Starshine Technical Services, http://www.starshine.org/


[ Table Of Contents ][ Answer Guy Current Index ] greetings   Meet the Gang   1   2   3   4   5   6   7   8   9 [ Index of Past Answers ]