LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Blogs > SilversleevesX
User Name
Password

Notices

Rate this Entry

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

Posted 11-17-2011 at 05:31 PM by SilversleevesX
Updated 11-17-2011 at 05: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:
Code:
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 linux.die.net, 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:
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.

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

Comments

 

  



All times are GMT -5. The time now is 02:17 PM.

Main Menu
Advertisement

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