Insert date/time within a filename | 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 !!

Insert date/time within a filename

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-07-2005
cooperman cooperman is offline
Registered User
 
Join Date: Nov 2005
Last Activity: 7 December 2005, 6:22 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Insert date/time within a filename

Hi Guys,

I need to script the renaming of files as followins:

files:

firstjd
secondjo
thirdjv

My script needs to insert the date/time infront of the last 2 characters of the filenames above, any ideas greatly received
the letters before the last 2 characters could change, I'm only interested in inserting date/time in front of the last 2 characters, whatever they maybe.

Many thanks
Sponsored Links
    #2  
Old 11-07-2005
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Check this out.


Code:
#! /bin/sh

for FILE in firstjd secondjo thirdjv
do
        NAME_LEN=${#FILE}
        FILE_END=${FILE:$NAME_LEN-2:$NAME_LEN}
        FILE_START=${FILE%$FILE_END}
        NEW_NAME=$FILE_START$(date +%s)$FILE_END

        mv $FILE $NEW_NAME
done

Edit: You might want to look into man date for other possible output formats of date.

vino

Last edited by vino; 11-07-2005 at 07:45 AM..
Sponsored Links
    #3  
Old 11-07-2005
cooperman cooperman is offline
Registered User
 
Join Date: Nov 2005
Last Activity: 7 December 2005, 6:22 AM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for that Vino, BUT

I get the following error:

`NEW_NAME=$FILE_START$' unexpected

any ideas ?

thanks again
    #4  
Old 11-07-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 24 April 2014, 8:13 PM EDT
Location: Foxborough, MA
Posts: 7,541
Thanks: 137
Thanked 539 Times in 506 Posts

Code:
#!/bin/ksh

for iter in  firstjd secondjo thirdjv
do
   _time=$(date +%Y%m%d-%H%M%S)
   new=$(echo ${iter} | sed "s/\(.*\)\(..\)/\1${_time}\2/")
   echo "old->[${iter}] new->[${new}]"
done

Sponsored Links
    #5  
Old 11-07-2005
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Quote:
Originally Posted by cooperman
Thanks for that Vino, BUT

I get the following error:

`NEW_NAME=$FILE_START$' unexpected

any ideas ?

thanks again
This ?


Code:
NEW_NAME=${FILE_START}$(date +%s)${FILE_END}


Last edited by vino; 11-07-2005 at 09:32 AM..
Sponsored Links
    #6  
Old 11-07-2005
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Quote:
Originally Posted by vgersh99
Code:
#!/bin/ksh

for iter in  firstjd secondjo thirdjv
do
   _time=$(date +%Y%m%d-%H%M%S)
   new=$(echo ${iter} | sed "s/\(.*\)\(..\)/\1${_time}\2/")
   echo "old->[${iter}] new->[${new}]"
done

Oh well, a sed solution will work as well.

I was trying to avoid an external call.

Cheers' !
Sponsored Links
    #7  
Old 11-07-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 24 April 2014, 8:13 PM EDT
Location: Foxborough, MA
Posts: 7,541
Thanks: 137
Thanked 539 Times in 506 Posts
here's a "pure" ksh:

Code:
#!/bin/ksh

for iter in  firstjd secondjo thirdjv
do
   _time=$(date +%Y%m%d-%H%M%S)
   root="${iter%%??}"
   suf="${iter##${root}}"
   echo "old->[${iter}] new->[${root}${_time}${suf}]"
done

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
append a filename with system date and time amsn08 Shell Programming and Scripting 1 07-30-2011 11:34 AM
Adding Date & time stamps to filename manish.s UNIX for Dummies Questions & Answers 1 03-13-2009 04:00 AM
find formatted filename with date time dpath2o UNIX for Advanced & Expert Users 6 02-05-2008 10:20 PM
Insert Time and Date Stamp aajmani Shell Programming and Scripting 1 10-13-2005 03:15 PM
Renaming files to have date/time in filename wayneb UNIX for Dummies Questions & Answers 5 01-19-2005 09:49 AM



All times are GMT -4. The time now is 05:12 AM.