0

I have a requirement to insert date and timestamp to file name

Example: If input filename is abc.xyz.123.txt, then output should be like :

abc.xyz.123.yyyyddmmhhssmm.txt

It has to find the last place where . occurs in filename and insert date and timestamp at that place.

Please help.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
pasha
  • 9
  • 1

3 Answers3

0
basename=`basename "$fullfile"`
extension="${basename##*.}"
now=`date +"%Y%d%m"`
replacement="$now.$extension"
file=${fullfile/$extension/$replacement}

Also take a look at man date how to format the date output. You can write data to your file like so:

echo "file contents" > "$file"

Edit/Update: I've read over the "last . occurence" - part. Sorry, I changed the code above to do what you want.

kriskbx
  • 107
  • 2
  • 8
0
outputfilename=${filename%.*}$(date +%Y%m%d%H%M%S).${filename##*.}

See also: Extract filename and extension in Bash

Community
  • 1
  • 1
Jan Böcker
  • 170
  • 5
0
f=abc.xyz.123.txt
extension="${f##*.}"
filename="${f%.*}"
dt=`date +%Y%d%m%H%M%S`
echo "$filename.$dt.$extension"
ISanych
  • 21,590
  • 4
  • 32
  • 52