How to add the dat time stamp just before the last extension of a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add the dat time stamp just before the last extension of a file?
# 1  
Old 07-25-2013
How to add the dat time stamp just before the last extension of a file?

hi,

A file name is in the form inside a variable
Code:
FILE_NAME="s1.txt.xls"

i want to append date and time stamp inbetween s1.txt and .xls. so after appending date time stamp the file name would be
Code:
MOD_FILE_NAME="s1.txt.201307251422.xls"

currently i am using the below code
Code:
FILE_NAME="s1.txt.xls"
MOD_FILE_NAME=`echo $FILE_NAME | cut -d"." -f 1-2`
MOD_FILE_NAME=$MOD_FILE_NAME.`date +%Y%m%d%H%M%S%N`.xls

the above code will not work if there are more than 2 dots in between the file name.
i always want to insert the date time stamp just before the final extension of the file. how can i do?
# 2  
Old 07-25-2013
Code:
$ echo ${FILE_NAME%.*}.DATE_GOES_HERE.${FILE_NAME##*.}
s1.txt.DATE_GOES_HERE.xls

$ FILE_NAME=1.2.3.4.5.6.7
$ echo ${FILE_NAME%.*}.DATE_GOES_HERE.${FILE_NAME##*.}
1.2.3.4.5.6.DATE_GOES_HERE.7

# 3  
Old 07-25-2013
Code:
echo ${FILE_NAME%.*}.$(date +%Y%m%d%H%M%S%N).${FILE_NAME##*.}

# 4  
Old 07-25-2013
Thanks for the solution. i want to store the filename with date time stamp in a variable. how can i do that??

Code:
FILE_NAME="s1.txt.xls.gk"
MOD_FILE_NAME=`echo ${FILE_NAME%.*}.``date +%Y%m%d%H%M%S%N``.${FILE_NAME##*.}`
echo "FILE_NAME = {$FILE_NAME}"
echo "MOD_FILE_NAME = {$MOD_FILE_NAME}"

the output of the above script is
Code:
$ sh datetime.sh
datetime.sh: line 2: .gk: command not found
FILE_NAME = {s1.txt.xls.gk}
MOD_FILE_NAME = {s1.txt.xls.20130725030022368251000}

its not adding the last extension to the modifies file name and also gives an error on line 2. can you tell me how can i store the new file name in a variable please??
# 5  
Old 07-25-2013
You've gone overkill on the backquotes.

The $(...) method of command substitution is easier on the eyes, and replaces the 'obsolete' `...` method.
Code:
$ MOD_FILE_NAME="${FILE_NAME%.*}.$(date +%Y%m%d%H%M%S%N).${FILE_NAME##*.}"
$ echo "$MOD_FILE_NAME"
s1.txt.xls.20130725120827N.gk

These 2 Users Gave Thanks to Scott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add current time stamp column in existing csv file

Hi , I want to add a new column 'current_time stamp' in my existing csv file with current time stamp for all the records.I tried something this but this is printing 0 with date & time and printed date one line above header.Please help awk -F "," 'BEGIN{ OFS="," } {$6=system("date... (5 Replies)
Discussion started by: netdbaind
5 Replies

2. Shell Programming and Scripting

Copying multiple files and appending time stamp before file extension

Hi, I have multiple files that read: Asa.txt Bad.txt Gnu.txt And I want to rename them using awk to Asa_ddmmyytt.txt and so on ... If there is a single command or more efficient executable please share! Thanks! (4 Replies)
Discussion started by: Jesshelle David
4 Replies

3. Shell Programming and Scripting

add string and time stamp on each line of file

I have file A.txt A 1023 B 123 C 1223 I want output Hello_12PM_A 1023 Hello_12PM_B 123 Helll_12PM_C 1223 Add Hello and time stamp in AM and PM. (4 Replies)
Discussion started by: asavaliya
4 Replies

4. Shell Programming and Scripting

Check file time stamp

Hi, I need help to read file in a directory on basis of time stamp. e.g. If file access in last 2 minutes it should not be copy to remote directory. Below is my script. #!/bin/ksh DATE=`date +"%Y-%m-%d_%H%M"` SEPARATER=" " exec < out_interfaces.cfg while read source_path... (10 Replies)
Discussion started by: qamar.alam
10 Replies

5. Shell Programming and Scripting

file time stamp

Hi All, I am facing small problem. i want to print file time stamp on which date file has placed in the server. i have given some code but its not giving the year. any help appreciated. regards rajesh. (4 Replies)
Discussion started by: rajesh_pola
4 Replies

6. Shell Programming and Scripting

creating a file with time stamp

Hi guys, Here my scenario is to find the files of previous days if the previous day load had not done. for that i created a file with time stamp and this file is created after the load completes. so every dau i search for the this file with previous days time stamp. i want to create a file... (1 Reply)
Discussion started by: apple2685
1 Replies

7. Shell Programming and Scripting

Change time stamp of a file

Hi, As i know , we can change the time stamp of a file by touch command, i did change in a file and it is looking as given # ls -l abcd -rw-r--r-- 1 batsoqa sicusers 0 Feb 17 2010 abcd actually i want to see the output like this -rw-r--r-- 1 batsoqa sicusers ... (3 Replies)
Discussion started by: apskaushik
3 Replies

8. Shell Programming and Scripting

change the time stamp of file

can we change the timestamp of a file to old date. -rwxrwxrwx 1 root other 330 Jul 1 16:03 abc.txt it shows creation time is 16.03 can i change it to previous time :) (2 Replies)
Discussion started by: anish19
2 Replies

9. UNIX for Dummies Questions & Answers

time stamp of file create

Hi, Sounds a simple request but I also need (would like) to gather the seconds too. I'm not even sure if this is held. I would think it is, somewhere??!!?! I belive that stat would/could work but I don't do C (we'll not yet). Is there any comamnd line util I can use? SunOS. Cheers... (7 Replies)
Discussion started by: nhatch
7 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
Login or Register to Ask a Question