Create file with TimeStamp in Shell Script

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.

[email protected]:~$ date
Wed Sep 18 13:53:49 IST 2019

Use any of following command to know options for date command.

[email protected]:~$ date --h


[email protected]:~$ man date
date -- display or set date and time
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

[email protected]:~$ date +"%Y/%m/%d"

Display date with timestamp

[email protected]:~$ echo $(date "+%Y.%m.%d_%H.%M.%S")

Create a shell script that saves new file with a timestamp.

current_time=$(date "+%Y.%m.%d_%H.%M.%S")
echo "Content here that goes to file" > $new_fileName
echo "Some more content to append" >> $new_fileName

Nishant Vaity

I am Software Engineer, Technology Mentor & Entrepreneur. I am passionate about the technologies I use and always eager to share & learn more from that passion. I am an experienced Software Engineer with 8+ years of experience in coding high-traffic websites. Proficient in HTML, JavaScript, jQuery, CSS with LAMP / MAMP / WAMP Stack. Contributor for WordPress open source plugins and Translator (Marathi) for WordPress 4.5.x.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: