Operating System - HP-UX
1827351 Members
6086 Online
109963 Solutions
New Discussion

Re: Time and Date as part of a files name

 
SOLVED
Go to solution
John Curtis
Occasional Advisor

Time and Date as part of a files name

I've a script that creates a data file for me and I'd like to add the time and date to the file names.

example
FIN_DATA20010918162545.DAT

'yyyymmddhhmmss'=20010918162545

How might I do this?
8 REPLIES 8
Patrick Wallek
Honored Contributor

Re: Time and Date as part of a files name

Put this into your script:

date_time=`date +%Y%m%d%H%M%S`

and then add that onto the filename.
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Time and Date as part of a files name

John,

STAMP=`date +%Y%m%d%H%M%S`
OUTFILE="FIN_DATA${STAMP}.dat

Now you can use $OUTFILE anywhere in
your script.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
John Curtis
Occasional Advisor

Re: Time and Date as part of a files name

I guess I awarded points too soon. Following these examples I end up with a file named FIN_DATAdate or FIN_DATAdate+%Y%m%d%H%M%S.dat.
The date command is not being recognised in the definition. I tried both single and double quote marks. Any further examples?
Edward Alfert_2
Respected Contributor

Re: Time and Date as part of a files name

there are 2 type of single quotes!

'
and
`

notice the difference? you need to use the second one.

on an hp keyboard it is underneath the ~ on the top left key under the ESC key
"Do what you love and you will never work a day in your life." - Confucius
Animesh Chakraborty
Honored Contributor

Re: Time and Date as part of a files name

Hi,
Try this

STAMP="`date '+%d-%m-%y %H:%M'`"


OUTFILE="FIN_DATA${STAMP}.dat"

Be carefull about all '&`.
Besy of luck
Animesh

Did you take a backup?
Suhas_3
Advisor

Re: Time and Date as part of a files name

one liner -
filename="FIN_DATA"`date +%Y%m%d%H%M%S`.dat

regards,
Suhas
Stefan Schulz
Honored Contributor

Re: Time and Date as part of a files name

This forum can be tricky. Somehow the backticks are replaced to single quotes.

What you need is the backtick, not a normal single quote.

If you are using a ksh you might also use:

STAMP=$(date +%Y%m%d%H%M%S)

Hope this helps

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Sridhar Bhaskarla
Honored Contributor

Re: Time and Date as part of a files name

John,

I think we would have been more specific. You need to use "command substituition quotes" instead of regular single and double quotes.

It is found usually with the character "~".

-Sri
You may be disappointed if you fail, but you are doomed if you don't try