LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-25-2009, 09:15 PM   #1
enine
Senior Member
 
Registered: Nov 2003
Distribution: Slackʍɐɹǝ
Posts: 1,488
Blog Entries: 4

Rep: Reputation: 283Reputation: 283Reputation: 283
Shell scripting help, time/date manipulation


I'm still learning scripting and need some direction on modifying time/date strings.

Some background, I learned early on that most unix/linux file system don't track the file creation date/time but it doesn't matter anyway since something as simple as e-mailing a file invalidates it. I learned about metadata inside files such as the jpg exif and similar. Now I keep a lot of information electronic and sometimes have to scan things and am working on using the metadata to track some info about what I've scanned. This leads me to todays script.

Using xscanimage I get a .pnm which I convert to a .tif then add in the metadata with a script something like this:


Code:
exiftool -IFD0:ModifyDate="2009:02:11 22:24:14" *.tif
exiftool -IFD0:ImageDescription="Receipt" *.tif
Now I'm trying to automate this a bit more by taking from the konqueror service menu for the convert to .tif I make my script

Code:
convert %f "`echo %f | perl -pe 's/\.[^.]+$//'`.tif"
I want to take the output of
Code:
 stat -c %y %f
which looks like

Code:
2009-02-11 22:24:14.000000000 -0500
and feed it into the command

Code:
exiftool -IFD0:ModifyDate="2009:02:11 22:24:14" %f.tif
So what happens is the original .pnm gets a time and date set on the file system when its written by xscanimage. I then use the script called by the Konqueror service menu which converts to .tif and in addition take the file system date/time and feeds it into exiftool to set it in the file's metadata therefore preserving it.
My difficulty lies in converting the format of the time and date from the output of one to the proper format for the other and passing the variable through as I don't sting stat will take %f like the original service menu script did since stat uses % as its option delimiter.

Of course any suggestions of other tools that may do this easier will help too. I'm setting more than just the time/date, I set other fields that save things like the scanner make and model, and descriptions. For example I have a IRA account that the bank doesn't allow electronic statements so I have to scan the paper ones. I then put what they are in the descriptions.
I use the same for my pictures, instead of trying to rename 100 .jpg's from a family vacation I used exiftool and wildcards to edit comments into several at once.

Last edited by enine; 02-25-2009 at 09:18 PM.
 
Old 02-26-2009, 05:24 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
I am not sure I fully understand your problem (it is early in the morning) but I think your it is about converting one date format to the other. For date format problems date is you friend.

With "-d" you can feed ANY readable date into date and get an output in any format.


Code:
jlinkels@jlinkels-lt:~$ date -d "2009-02-11 22:24:14.000000000 -0500"
Thu Feb 12 03:24:14 GMT 2009
or:
Code:
jlinkels@jlinkels-lt:~$ date -d "2009-02-11 22:24:14.000000000 -0500" +%Y-%m-%d
2009-02-12
You can assign the string to a variable:
Code:
jlinkels@jlinkels-lt:~$ mydate=$(date -d "2009-02-11 22:24:14.000000000 -0500" +%Y-%m-%d)
jlinkels@jlinkels-lt:~$ echo $mydate
2009-02-12
You can use $mydate in a shell script in any command.

jlinkels
 
Old 02-26-2009, 02:45 PM   #3
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 32
Quote:
Originally Posted by enine View Post
My difficulty lies in converting the format of the time and date from the output of one to the proper format for the other and passing the variable through as I don't sting stat will take %f like the original service menu script did since stat uses % as its option delimiter.
Yup, date is your friend. Don't take the output of stat, so you don't have to convert formats.

In bash (and most other shells), $ does variable substituion. Try it out:
Code:
file=some_image.tif
echo $file
date -r $file "+%F %T"
Of course, substitute the name of some image of yours. You can use it like this:
Code:
exiftool -IFD0:ModifyDate=`date -r $file "+%F %T"` $file
You lost me where you don't sting. Maybe rephrase that part?
/Quigi
 
Old 02-26-2009, 07:26 PM   #4
enine
Senior Member
 
Registered: Nov 2003
Distribution: Slackʍɐɹǝ
Posts: 1,488

Original Poster
Blog Entries: 4

Rep: Reputation: 283Reputation: 283Reputation: 283
Ok, looks like date can do the conversion. I need the date of the file, not the data currently so I still need to stat the file to get its date then convert.
 
Old 02-26-2009, 07:38 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Quote:
Originally Posted by enine View Post
Ok, looks like date can do the conversion. I need the date of the file, not the data currently so I still need to stat the file to get its date then convert.
You can use the -r option of date to retrieve the time of last modification of a file instead of the current time, as previously shown by Quigi.
 
Old 02-26-2009, 08:04 PM   #6
enine
Senior Member
 
Registered: Nov 2003
Distribution: Slackʍɐɹǝ
Posts: 1,488

Original Poster
Blog Entries: 4

Rep: Reputation: 283Reputation: 283Reputation: 283
Ahh, sorry, I was thinking it was pulling the system date, not the file date, I'll try again.
Thanks
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting problem: string manipulation Philip87 Linux - General 4 09-27-2008 01:29 PM
java date and time manipulation? trscookie Programming 1 07-13-2006 06:51 PM
shell scripting and 'date' matttail Programming 8 05-02-2006 10:56 PM
Shell scripting: Doing MySQL Dump base on date Swakoo Linux - General 2 09-21-2005 10:23 PM
date manipulation in shell peal_ss Programming 4 03-29-2004 04:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:55 AM.

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
Open Source Consulting | Domain Registration