How do you get the last modification date of a file? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How do you get the last modification date of a file?

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-30-2013
Nate18 Nate18 is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 30 January 2013, 11:08 PM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
How do you get the last modification date of a file?

I'm trying to get the date output to be in the form yyyy-mm-dd (e.g. 2013-01-18)

Code:
!/bin/sh
modDate=$(stat -c %y $1)
echo $modDate >> $1

When I run this on another file (by typing ./dateScript theFile.txt), I keep getting this message:

stat: illegal option -- c

What's wrong with my code here or how can I fix it?

What about the following instead? I was told that date -r does what I want, but I can't seem to get it to. Since I can't find much information on how to use the date command, I've put it off to the side unless someone else knows how I can get the last modified date from a file with the date command.

Code:
date -r $1

Any other simple suggestions to get the last modification date of a file so that I can append it to the end of said file would be great too. I've been trying to figure out some variation of this problem for a few hours now and at this point I have no new ideas and am completely stagnant, so any help is really appreciated.
Sponsored Links
    #2  
Old 01-30-2013
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 8 August 2014, 4:56 AM EDT
Posts: 3,057
Thanks: 33
Thanked 658 Times in 637 Posts
you can get the last modification time from your ls command output.


Code:
 
ls -l $1 | awk -v y=`date +%Y` 'BEGIN{a="  JanFebMarAprMayJunJulAugSepOctNovDec"}{if($8~/:/){$8=y}print $8"-"index(a,$6)/3"-"$7}'

Sponsored Links
    #3  
Old 01-30-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 21 September 2014, 1:14 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 4,251
Thanks: 45
Thanked 817 Times in 644 Posts
Which OS do you work on? Linux "stat" knows the "-c" option as per the man page, but that does not necessarily have to be the case for any other UNIXoid derivate out there.

I hope this helps.

bakunin
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
get file modification date in number format (yyyy mm dd hh mm ss) rvdokkum OS X (Apple) 7 08-28-2010 09:21 AM
How do I get at the modification date for a file as a variable for a script? Timespike Homework & Coursework Questions 3 12-13-2009 10:14 AM
List the file or files with last modification date KitFisto Shell Programming and Scripting 6 10-12-2009 02:38 AM
Shell script: last modification date for a file onlyc UNIX for Dummies Questions & Answers 4 07-06-2006 01:01 PM
command for modification date of a file scampsd UNIX for Dummies Questions & Answers 5 12-28-2005 07:14 AM



All times are GMT -4. The time now is 10:12 PM.