LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX
User Name
Password
Other *NIX This forum is for the discussion of any UNIX platform that does not have its own forum. Examples would include HP-UX, IRIX, Darwin, Tru64 and OS X.

Notices

Reply
 
Search this Thread
Old 07-08-2009, 05:35 PM   #1
thelordmule
LQ Newbie
 
Registered: Jul 2006
Location: Australia
Distribution: Mac OSX 10.6, Ubuntu 10.10
Posts: 23

Rep: Reputation: 0
Talking extract modification date of file as yyyymmdd


hi there,

quick question, I am trying to get the modification date from a file. I found a solution:

Code:
touch mynewfile.txt
stat -f "%Dm" mynewfile.txt > tseconds
date -r `cat tseconds` +"%Y%m%d"
Is there a better or perhaps more elegant method?

I am working on OS X Leopard 10.5, GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0)
 
Old 07-09-2009, 09:52 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
I don't know in Mac OS X, but using GNU ls you can try the --time-style option, to custom the way the modification time is displayed. For example:
Code:
ls --time-style=+%Y%m%d -l mynewfile.txt
 
Old 07-10-2009, 07:38 PM   #3
thelordmule
LQ Newbie
 
Registered: Jul 2006
Location: Australia
Distribution: Mac OSX 10.6, Ubuntu 10.10
Posts: 23

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by colucix View Post
I don't know in Mac OS X, but using GNU ls you can try the --time-style option, to custom the way the modification time is displayed. For example:
Code:
ls --time-style=+%Y%m%d -l mynewfile.txt
thanks for the suggestion colucix. It seems that option is not supported in standard mac osx. but thats ok.

see i could alternatively have:
Code:
date -r `stat -f "%Dm" mynewfile.txt ` +"%Y%m%d"
but then I have this problem with using that in a script and assigning to a variable.

Code:
MODDATE=`date -r `stat -f "%Dm" mynewfile.txt ` +"%Y%m%d"`
has to become:

Code:
MODDATE=`date -r $(stat -f "%Dm" mynewfile.txt) +"%Y%m%d"`
ok I think I am nitpicking now...you may close this thread
 
Old 07-11-2009, 02:28 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
That's right. The backticks don't support nesting, whereas the $(...) syntax does!
 
  


Reply

Tags
date, file, modified, stat


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get file modification date/time in Bash script cmfarley19 Programming 12 01-19-2013 09:37 AM
Tar: Preserve file creation/modification date Sum1 Linux - Software 2 05-15-2009 03:50 PM
find YYYYMMDD and YYYYMMDD difference kpachopoulos Programming 1 09-22-2007 10:37 AM
Heeelp! How to preserve file date modification attribute Slackovado Slackware 1 03-30-2005 09:36 AM
File Modification Date Comparison MadTurki Programming 7 03-08-2004 04:02 PM


All times are GMT -5. The time now is 09:03 AM.

Main Menu
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