adding date to a filename
Spliting filename & extension :-
Option, 1
=========================================================
[rajesh@rhel6 ~]$ FILE=somefile.txt
[rajesh@rhel6 ~]$ echo $FILE
somefile.txt
[rajesh@rhel6 ~]$ NAME=${FILE%.*}
[rajesh@rhel6 ~]$ echo $NAME
somefile
[rajesh@rhel6 ~]$ EXT=${FILE#*.}
[rajesh@rhel6 ~]$ echo $EXT
txt
[rajesh@rhel6 ~]$ DATE=`date +%d-%m-%y`
[rajesh@rhel6 ~]$ echo $DATE
18-06-13
[rajesh@rhel6 ~]$ NEWFILE=${NAME}_${DATE}.${EXT}
[rajesh@rhel6 ~]$ echo $NEWFILE
somefile_18-06-13.txt
[rajesh@rhel6 ~]$
Option,2 :However, this won't work with multiple
=========================================================
[rajesh@rhel6 ~]$ echo $FILE
somefile.txt
[rajesh@rhel6 ~]$
[rajesh@rhel6 ~]$ NAME=`echo $FILE | cut -d. -f1 `
[rajesh@rhel6 ~]$ echo $NAME
somefile
[rajesh@rhel6 ~]$ EXT=`echo $FILE | cut -d. -f2`
[rajesh@rhel6 ~]$ echo $EXT
txt
Option, 1
=========================================================
[rajesh@rhel6 ~]$ FILE=somefile.txt
[rajesh@rhel6 ~]$ echo $FILE
somefile.txt
[rajesh@rhel6 ~]$ NAME=${FILE%.*}
[rajesh@rhel6 ~]$ echo $NAME
somefile
[rajesh@rhel6 ~]$ EXT=${FILE#*.}
[rajesh@rhel6 ~]$ echo $EXT
txt
[rajesh@rhel6 ~]$ DATE=`date +%d-%m-%y`
[rajesh@rhel6 ~]$ echo $DATE
18-06-13
[rajesh@rhel6 ~]$ NEWFILE=${NAME}_${DATE}.${EXT}
[rajesh@rhel6 ~]$ echo $NEWFILE
somefile_18-06-13.txt
[rajesh@rhel6 ~]$
Option,2 :However, this won't work with multiple
"."
in the file name.=========================================================
[rajesh@rhel6 ~]$ echo $FILE
somefile.txt
[rajesh@rhel6 ~]$
[rajesh@rhel6 ~]$ NAME=`echo $FILE | cut -d. -f1 `
[rajesh@rhel6 ~]$ echo $NAME
somefile
[rajesh@rhel6 ~]$ EXT=`echo $FILE | cut -d. -f2`
[rajesh@rhel6 ~]$ echo $EXT
txt
Comments
Post a Comment