-   Programming (
-   -   Get file modification date/time in Bash script (

cmfarley19 03-29-2004 09:10 AM

Get file modification date/time in Bash script
Greeting all.

I am modifying a bash script that generates a web based photo album from a directory of photos.

I want to add text below the thumbnail that displays the date the photo was taken ( file created ).

The stat command gives me part of what I need:

[cfarley@wombat cfarley]$ stat -c %y test.txt
2004-02-20 14:10:37.000000000 -0500


[cfarley@wombat cfarley]$ stat -c %Y test.txt

Is there a command I can pipe the output of stat to that will return the date only?

Any thoughts?


jim mcnamara 03-29-2004 09:49 AM


$ stat -c %Y test.txt | sed 's/test.txt//'

Hko 03-29-2004 10:06 AM


stat -c %y test.txt | cut -d ' ' -f1
or, using sed:

stat -c %y test.txt | sed 's/^\([0-9\-]*\).*/\1/'
Or use bash' string manipulation (may be a little faster):

MODDATE=$(stat -c %y test.txt)

cmfarley19 03-29-2004 11:05 AM

Those seem to work.
Still interested in seeing other solutions.

J_Szucs 04-23-2004 07:42 AM

[cfarley@wombat cfarley]$ stat -c %y test.txt | awk '{ printf $1 "\n"}'

cmfarley19 04-23-2004 08:33 AM

I tried your suggestion and

stat -c %Y vs.exe | awk '{printf $1 "\n"}'

stat -c %Y vs.exe
yield the same results.

J_Szucs 04-23-2004 05:27 PM


However, my suggestion was not:
stat -c %Y vs.exe | awk '{printf $1 "\n"}'

stat -c %y vs.exe | awk '{printf $1 "\n"}'

Please note that letter "y" is lower case in my code. It makes a great difference!

As for the code: it uses awk, which I found one of the most useful tools (almost like grep or sed).
awk treats the input line as a (database) record containing fields, using (by default) the space character as the field separator (but a different field separator can be specified in the command).
Then you can refer to any field of the line using its index ($1, $2, $3, etc.)
stat -c %y vs.exe | awk '{printf $1 ":" $3 "\n"}'
will print the first and the third fields on each line, putting ":" between the fields.
stat -c %y vs.exe | awk '{printf $1 ":" $3 }'
will print the same, but on one single output line, because there is no newline "\n" specified.

Apart from just printing specific parts (fields) of each line, you can perform calculations or sums based on them, and all in one command line.

Just google the web for awk examples.

brsa 11-06-2007 12:02 PM

Thanks, your hints were useful! Another way is to simply use date instead of stat:

date -r $file +%F
See man date for more infos.

errigour 06-26-2010 07:51 PM

HKO I hope your watching this thread.
I browsed google for a way to use a bash script for printing the date of files onto an html page I have going that indexes files in my directory. should show you what Im talking about. Anways I wanted to know if there was a way I could make
MODDATE=$(stat -c %y test.txt)
print out the time and maybe the permissions of the file. for anyone that wants a directory index just run this script in a place that doesn't have an index.html file you plan on keeping and contains html files you want listed.

yech 01-18-2013 03:20 AM


Originally Posted by brsa (Post 2950128)
Thanks, your hints were useful! Another way is to simply use date instead of stat:

date -r $file +%F
See man date for more infos.

excellent, as stat may not exist for small system.

mina86 01-18-2013 06:36 AM

By the way, you know about EXIF right?

David the H. 01-18-2013 11:44 AM

Please note that this is a resurrected necro-thread.

@yech, please don't re-open old threads unless you have something substantial to add to that discussion. I think your comment (even though I agree with it totally) is borderline in that respect.

OTOH, it does allow me to make a substantial addition of my own ;):


stat --printf='%Y' file
The --printf option lets you control the output format you want to use more precisely. Add '\n' if you need a trailing newline.

It's probably only available in gnu stat though.

NevemTeve 01-19-2013 09:37 AM

Also you can use option '-printf' of utility 'find'.


find dir -name filename -printf '%TY-%Tm-%Td\n'

Miati 12-21-2014 11:05 AM

Since this is for a image gallery, and thus dealing with images - there is a much better option:

Using imagemagicks identify, you can grab the image creation date from the exif itself:

identify -format %[EXIF:DateTimeOriginal]

To make a example: (from one of my photos)

$ identify -format %[EXIF:DateTimeDigitized] File.jpg
2008:04:02 18:20:35

$ stat -c %y File.jpg
2014-10-26 19:42:56.353367145 -0700


I realize this is a very old thread, but the original question had "displays the date the photo was taken" and all the other answers would only work in optimal situations (never modified or copied the file) and would otherwise reflect a incorrect time.

konenas 04-14-2015 03:00 PM


Originally Posted by brsa (Post 2950128)
Thanks, your hints were useful! Another way is to simply use date instead of stat:

date -r $file +%F
See man date for more infos.


date is a standard and has more options than stat

date -r $file +'%Y-%m-%d %H:%M:%S'

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