LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
-->
 
Search this Thread
Old 03-29-2004, 10:10 AM   #1
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
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:
Code:
[cfarley@wombat cfarley]$ stat -c %y test.txt
2004-02-20 14:10:37.000000000 -0500
or
Code:
[cfarley@wombat cfarley]$ stat -c %Y test.txt
1077304237
Is there a command I can pipe the output of stat to that will return the date only?

Any thoughts?

CMF
 
Old 03-29-2004, 10:49 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 32
Code:
$ stat -c %Y test.txt | sed 's/test.txt//'
 
Old 03-29-2004, 11:06 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,523

Rep: Reputation: 92
Code:
stat -c %y test.txt | cut -d ' ' -f1
or, using sed:
Code:
stat -c %y test.txt | sed 's/^\([0-9\-]*\).*/\1/'
Or use bash' string manipulation (may be a little faster):
Code:
MODDATE=$(stat -c %y test.txt)
MODDATE=${MODDATE%% *}
echo $MODDATE

Last edited by Hko; 03-29-2004 at 11:10 AM.
 
1 members found this post helpful.
Old 03-29-2004, 12:05 PM   #4
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
Excellent.
Those seem to work.
Still interested in seeing other solutions.
 
Old 04-23-2004, 08:42 AM   #5
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

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

Last edited by J_Szucs; 04-23-2004 at 09:25 AM.
 
Old 04-23-2004, 09:33 AM   #6
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
JSzucs...
I tried your suggestion and
Code:
stat -c %Y vs.exe | awk '{printf $1 "\n"}'
and
Code:
stat -c %Y vs.exe
yield the same results.
 
Old 04-23-2004, 06:27 PM   #7
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
Sure.

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

but:
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.)
Examples:
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.

Last edited by J_Szucs; 04-23-2004 at 06:32 PM.
 
Old 11-06-2007, 01:02 PM   #8
brsa
LQ Newbie
 
Registered: Nov 2007
Posts: 1

Rep: Reputation: 1
Thanks, your hints were useful! Another way is to simply use date instead of stat:
Code:
date -r $file +%F
See man date for more infos.
 
Old 06-26-2010, 08:51 PM   #9
errigour
Member
 
Registered: May 2009
Posts: 67

Rep: Reputation: 3
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. http://relik.ath.cx/unused/ 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)
MODDATE=${MODDATE%% *}
print out the time and maybe the permissions of the file.
http://relik.ath.cx/unused/xhtml_html 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.
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
time stamp for file modification youngstorm Linux - Newbie 2 11-01-2005 04:46 PM
Heeelp! How to preserve file date modification attribute Slackovado Slackware 1 03-30-2005 10:36 AM
File Modification Date Comparison MadTurki Programming 7 03-08-2004 05:02 PM
Warning: File 'sendmail.cf' has modification time in the future G-Rad Linux - Newbie 0 03-07-2004 12:09 AM
perl, Net::Ftp, and Novell file modification time neo77777 Programming 0 01-30-2003 10:41 PM


All times are GMT -5. The time now is 04:46 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
Open Source Consulting | Domain Registration