How to append date and time stamp before the two extensions?

hi,

i have some file names. my file names are as follows:

c_abc_new.txt.xls
c_def.txt.xls

i want to append date time stamp in the below manner.

c_abc_new_YYYYMMDD_HH24MISS.txt.xls
c_def_YYYYMMDD_HH24MISS.txt.xls

check the two input file names, they differ in naming. the 1st file name has 2 underscores but the 2nd has only 1.
can anyone tell me how to do this??

Checked this thread?

ya i saw that thread. it was asked by me. but this time its little different from that. this time i want to add date and time stamp before the last two extension. any idea about this.

i am using the below code.

GKE_FILE="s1.txt.xls"
MOD_GKE_File=$(echo ${GKE_FILE%.*.*}_`date +%Y%m%d_%H%M%S%N`.${GKE_FILE##*.})

the above code gives the output as

s1_20130806_020756422012000.xls

it doesnot include the .txt extension
i want it as

s1_20130806_020756422012000.txt.xls

This could fit your needs:

cd in your working directory and:

mydate=`date +%Y%m%d_%k%M%S`
for myfile in `ls *.txt.xls`
do
  newname=`echo ${myfile} | sed "s/\./\_${mydate}\./" `
  mv ${myfile} ${newname}
done

test it in a development environment in order to get sure you are actully getting the intended results

see ya
fra

Try:

for f in *txt.xls; do s=${f#*.}; p=${f%.$suffix}; echo mv $f ${p}_$(date +%Y%m%d_%H%M%S%N).${s}; done

User agn takes the approach I would, but FYI: but he has a coding error.


for f in *txt.xls; do suffix=${f#*.}; p=${f%.$suffix}; echo mv $f ${p}_$(date +%Y%m%d_%H%M%S%N).${s}; done

This code below does the same thing while completely parameterizing the suffix.

datestamp=$(date +%Y%m%d_%H%M%S%N)
suffix=".txt.xls"
ls -1 *$suffix | 
while read f; do
    p=${f%$suffix}
    echo mv \"$f\" \"${p}_${datestamp}${suffix}\"
done

To actually execute the code (the above just echos the commands), pipe the whole thing through sh.

i accept your answers but its not always the ".txt.xls" extension. it can be other extensions also. say

c_abc_new.txt.xls
c_def.lst.xls

someone suggested below code previously.

FILE_NAME="c_abc_new.txt.xls"
$ MOD_FILE_NAME="${FILE_NAME%.*}_$(date +%Y%m%d_%H%M%S%N).${FILE_NAME##*.}"

but this code inserts the date time just before the last extension. but my need is to insert it before the last two extension. can anyone modify the above code to fulfill my needs?

How about:

 FName\_No\_Extension=c\_abc_new
 
 Mod\_File_NAME=$\(echo $FName\_No_Extension\`date \+%Y%m%d_%H%M%S%N\`.txt\)

echo $Mod_File_NAME

Produces the output I think you are looking for. By no means is it pretty; however, it does produce the desired file name. :b:

*Note those are back quotes and not single quotes. Could replace quotes with $() if so desired.

Can you help us with
Bash/perl/php/python ?

how do you want to use the code ? add/call a script

Are these files created on the fly ?

If this thread is not yet solved then you may use the below.

FILE_NAME="c_abc_new.txt.xls"
 
MOD_FILE_NAME="${FILE_NAME%.*.*}_$(date +%Y%m%d_%H%M%S%N).${FILE_NAME#*.}"