Operating System - HP-UX
1826269 Members
3780 Online
109692 Solutions
New Discussion

How do you get the current date in Shell Script ??

 
SOLVED
Go to solution
Praveen Hari
Advisor

How do you get the current date in Shell Script ??

How do you get the current date in shell script ?
I want to change the file name a.txt to
a.yymmdd.txt in Shell script with current date.
Any help is appreciated.
Thanks
5 REPLIES 5
Tim Adamson_1
Honored Contributor
Solution

Re: How do you get the current date in Shell Script ??

Use the date command:

date_format=`date +%y%m%d`

mv a.txt a.${date_format}.txt


Tim
Yesterday is history, tomorrow is a mystery, today is a gift. That's why it's called the present.
Patrick Wallek
Honored Contributor

Re: How do you get the current date in Shell Script ??

The better way to do it is to do:

curr_date=$(date +%y%m%d)

The use of the back ticks (`) has basically been deprecated as it can be difficult to distinguish the back ticks from an apostrophe / single quote (') in some fonts.

The $(command) is much more readable and does exactly the same thing.
Steven E. Protter
Exalted Contributor

Re: How do you get the current date in Shell Script ??

Patrick,

You deserve a rabbit. I must have missed the class on the back tick getting deprecated.

I've been punching out scripts left and right and I always hated the back tick because it was hard to read.

Thanks for your post.

You didn't just help the person that started the thread this time. You helped a fellow towel-head.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Patrick Wallek
Honored Contributor

Re: How do you get the current date in Shell Script ??

No problem Stephen. Glad to be of service!

Don't feel bad. It has taken me a while to get used to not using the back tick as well. I'm just now starting to consistently use the $(command) syntax.

Will Couillard
Occasional Advisor

Re: How do you get the current date in Shell Script ??

Without the need to assign and call a variable:

mv a.out a.$(date '+%y%m%d').out