Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support How to append date and time stamp before the two extensions?
# 1  
Old 08-06-2013
How to append date and time stamp before the two extensions?

hi,

i have some file names. my file names are as follows:
Code:
c_abc_new.txt.xls
c_def.txt.xls

i want to append date time stamp in the below manner.
Code:
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??
# 2  
Old 08-06-2013
# 3  
Old 08-06-2013
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.
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
Code:
s1_20130806_020756422012000.xls

it doesnot include the .txt extension
i want it as
Code:
s1_20130806_020756422012000.txt.xls

# 4  
Old 08-06-2013
This could fit your needs:

cd in your working directory and:

Code:
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
# 5  
Old 08-06-2013
Try:

Code:
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

# 6  
Old 08-06-2013
User agn takes the approach I would, but FYI: but he has a coding error.

Code:
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.

Code:
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.
# 7  
Old 08-07-2013
i accept your answers but its not always the ".txt.xls" extension. it can be other extensions also. say
Code:
c_abc_new.txt.xls
c_def.lst.xls

someone suggested below code previously.
Code:
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?

Previous Thread | Next Thread
Test Your Knowledge in Computers #504
Difficulty: Medium
Typically, programs operate in a reverse-linear flow of control.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl:Script to append date and time stamp

Help with Perl script : I have a web.xml file with a line <display-name>some_text_here</display-name> Need to append the current date and time stamp to the string and save the XML file Something like <display-name>some_text_here._01_23_2014_03_56_33</display-name> -->Finally want... (5 Replies)
Discussion started by: gaurav99
5 Replies

2. Shell Programming and Scripting

Files with date and time stamp

Hi Folks, Need a clarification on files with date and time stamp. Here is my requirement. There is a file created everyday with the following format "file.txt.YYYYMMDDHHMMSS". Now i need to check for this file and if it is available then i need to do some task to the file. I tried... (6 Replies)
Discussion started by: jayadanabalan
6 Replies

3. Shell Programming and Scripting

Set date and time stamp of one file to another

Hi I use "touch -t xxxxxxxx" command to set date/time stamp of a file. My requirement is to read the date/time stamp of a file and apply it to another file. Is there anyway to do it simple instead of manually taking date/stamp of first file? TIA Prvn (2 Replies)
Discussion started by: prvnrk
2 Replies

4. UNIX for Dummies Questions & Answers

ls -ltr for a future date/time stamp file

Hi When i do ls -ltr <file1> then it shows me the date and time of the file if - for whatever reason file has future date/time stamp then ls -ltr is not showing the time, it just shows only date part ... even if time is ahead by 2 hr than current time. suppose a file was copied from INDIA... (3 Replies)
Discussion started by: reldb
3 Replies

5. Shell Programming and Scripting

append date time stamp via ftp script

I have searched several thread and not found my solution, so I am posting a new qustion. I have a very simple script on an AIX server that FTPs 2 files to a MS FTP server. These 2 files are created on the AIX server every hour, with a static name. I need to FTP the files to the MS server, but... (1 Reply)
Discussion started by: sknisely
1 Replies

6. UNIX for Advanced & Expert Users

rsync - date/time stamp

Hi, We are using RSYNC for syncing remote directories and working great. Our requirement is to have the destination files with date/time stamp of when they're copied on to the destination server, NOT the date/time stamps of source files/directories. As RSYNC, by default, preserving the same... (4 Replies)
Discussion started by: prvnrk
4 Replies

7. UNIX for Dummies Questions & Answers

Date/Time Stamp

Hi All, Wondering if there is have a date added at the end of a test string. I have a hypothetical text file day one: John Paul George When the file day one is output, I'd like it to read something like this: John 101406 Paul 101406 George 101406 Day two, when the same text file... (0 Replies)
Discussion started by: JimmyFlip
0 Replies

8. Shell Programming and Scripting

Insert Time and Date Stamp

I have a directory with following files in it ABC.000.DAT ABC.001.DAT ABC.002.DAT ABC.003.DAT I want to insert time and date stamp in file names like ABC.000.YYYYMMDDHHMM.DAT I able to insert the time and date stamp at the end of filename Kindly help (1 Reply)
Discussion started by: aajmani
1 Replies

9. Shell Programming and Scripting

Date Time Stamp

I'm trying to write a script that checks the DTS of a file the compares it to the current time. If greater that 60 mins has gone by and the file has not been written to alert. So far I have the time pulled from the file but I dont know how to compare the times against a 60 min difference. ... (2 Replies)
Discussion started by: jarich
2 Replies

10. UNIX for Dummies Questions & Answers

File date and time stamp

I have to capture the creation date and time stamp for a file. The ls command doesn't list all the required information. I need year, month, day, hour, minute and second. Any ideas... (1 Reply)
Discussion started by: Xenon
1 Replies

Featured Tech Videos