Create Timestamp in Bash Script

email me

You are able to use the bash date function by assigning the date to a variable, thenĀ formatting that variable so it fits appropriately as a timestamp. See the following…

current_time=$(date "+%Y.%m.%d-%H.%M.%S")
echo "$current_time YourAppName" >>$HOME/YourLogFile

or

logger -s "$current_time YourAppName">>$HOME/YourLogFile

 

Notes

Append timestamp to file name

new_fileName=$file_name.$current_time
echo “New FileName: ” “$new_fileName”