I love using Ubuntu OS for development. I need to create a shell script that saves new file with a timestamp. Following are steps that I’ve followed:
Understanding date command.
nishant@nishantvaity:~$ date Wed Sep 18 13:53:49 IST 2019
Use any of following command to know options for date command.
nishant@nishantvaity:~$ date --h
OR
nishant@nishantvaity:~$ man date NAME date -- display or set date and time SYNOPSIS date [-ju] [-r seconds] [-v [+|-]val[ymwdHMS]] ... [+output_fmt] date [-jnu] [[[mm]dd]HH]MM[[cc]yy][.ss] date [-jnu] -f input_fmt new_date [+output_fmt] date [-d dst] [-t minutes_west]
Display date on Terminal
nishant@nishantvaity:~$ date +"%Y/%m/%d" 2019/09/18
Display date with timestamp
nishant@nishantvaity:~$ echo $(date "+%Y.%m.%d_%H.%M.%S") 2019.09.18_14.02.43
Create a shell script that saves new file with a timestamp.
#!/bin/sh file_name="my_file_name_here_" current_time=$(date "+%Y.%m.%d_%H.%M.%S") file_extension=".txt" new_fileName=$file_name$current_time$file_extension echo "Content here that goes to file" > $new_fileName echo "Some more content to append" >> $new_fileName