The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Processing a log file based on date/time input and the date/time on the log file primp Shell Programming and Scripting 4 03-16-2008 08:23 AM
find formatted filename with date time dpath2o UNIX for Advanced & Expert Users 6 02-05-2008 08:20 PM
insert filename into file using SED (or AWK) USER#5 Shell Programming and Scripting 2 08-06-2006 07:08 PM
Insert Time and Date Stamp aajmani Shell Programming and Scripting 1 10-13-2005 12:15 PM
Renaming files to have date/time in filename wayneb UNIX for Dummies Questions & Answers 5 01-19-2005 07:49 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 11-07-2005
Registered User
 

Join Date: Nov 2005
Posts: 3
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
Reply With Quote
Forum Sponsor
  #2  
Old 11-07-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
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 05:45 AM.
Reply With Quote
  #3  
Old 11-07-2005
Registered User
 

Join Date: Nov 2005
Posts: 3
Thanks for that Vino, BUT

I get the following error:

`NEW_NAME=$FILE_START$' unexpected

any ideas ?

thanks again
Reply With Quote
  #4  
Old 11-07-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
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
Reply With Quote
  #5  
Old 11-07-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
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 07:32 AM.
Reply With Quote
  #6  
Old 11-07-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
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' !
Reply With Quote
  #7  
Old 11-07-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,003
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0