Help answer threads with 0 replies.
Go Back > Blogs > SilversleevesX
User Name


Rate this Entry

'stat' vs 'date' w/re string manipulation in Bash scripts

Posted 11-17-2011 at 06:31 PM by SilversleevesX
Updated 11-17-2011 at 06:42 PM by SilversleevesX

I started browsing the Web tonight looking to do something I had been doing in my Bash scripts with stat and three more lines of string cutting in one line instead.

I had been using stat -c &y file to get a file's modification date in scripts, but I have never liked the nine-digit "blink-of-an-eye" part of the return on that command. My method up till now has been to eliminate it with lines like these:
gdate=$(stat -c %y foo.jpg)
pqdate=$(echo ${gdate%.*})
rdate=$(echo ${gdate##* })
mdate=$(echo -e "$pqdate $rdate")
After reading this post, I looked at the (ostensibly generic) date manual page on, and skimmed through the other format flags, if that's the right name for them, to see if there were any that returned the time and UTC offset (again, not sure of my terms, but it sounds right). After a bit of trial and error, I came up with this code:
date -r foo.jpg "+%F %T %z"
which returns the date of a file in the same format as stat -c &y file does, but without that string of nine extra numbers, which reminds me of nothing so much as the real-time duration of some of the highest-numbered elements on the Periodic Table -- that is, when it's not all zeros.

I hope this gets spidered by the search engines. I'd love to help someone out with the above advice and tips, abstruse as they might be.

Posted in Discussion
Views 563 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 02:27 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration