There were multiple ways to append the file :
From stackoverflow :
- Get the date as a string
This is pretty easy. Just use the date command with the + option. We can use backticks to capture the value in a variable.
$ DATE=`date +%d-%m-%y`
You can change the date format by using different % options as detailed on the date man page.
- Split a file into name and extension.
This is a bit trickier. If we think they'll be only one . in the filename we can use cut with . as the delimiter.
$ NAME=`echo $FILE | cut -d. -f1
$ EXT=`echo $FILE | cut -d. -f2`
However, this won't work with multiple . in the file name. If we're using bash - which you probably are - we can use some bash magic that allows us to match patterns when we do variable expansion:
$ NAME=${FILE%.*}
$ EXT=${FILE#*.}
Putting them together we get:
$ FILE=somefile.txt
$ NAME=${FILE%.*}
$ EXT=${FILE#*.}
$ DATE=`date +%d-%m-%y`
$ NEWFILE=${NAME}_${DATE}.${EXT}
$ echo $NEWFILE
somefile_05-10-15.txt
And if we're less worried about readability we do all the work on one line (with a different date format):
$ FILE=somefile.txt
$ FILE=${FILE%.*}_`date +%d%b%y`.${FILE#*.}
$ echo $FILE
somefile_05oct15.txt
Other ways would be :
$ date
Wed Oct 16 19:20:51 EDT 2013
If you truly want filenames like that you'll need to wrap that string in quotes.
$ touch "foo.backup.$(date)"
$ ll foo*
-rw-rw-r-- 1 saml saml 0 Oct 16 19:22 foo.backup.Wed Oct 16 19:22:29 EDT 2013
You're probably thinking of a different string to be appended would be my guess though. I usually use something like this:
$ touch "foo.backup.$(date +%F_%R)"
$ ll foo*
-rw-rw-r-- 1 saml saml 0 Oct 16 19:25 foo.backup.2013-10-16_19:25
See the man page for date for more formatting codes around the output for the date & time.
Additional formats
If you want to take full control if you consult the man page you can do things like this:
$ date +"%Y%m%d"
20131016
$ date +"%Y-%m-%d"
2013-10-16
$ date +"%Y%m%d_%H%M%S"
20131016_193655