Operating System - HP-UX
1849796 Members
2715 Online
104044 Solutions
New Discussion

Re: create a file with date

 
Tonatiuh
Super Advisor

create a file with date

HP-UX 11.11

How can I create a file or directory using the date and/or time as the name of this file/directory?
4 REPLIES 4
Patrick Wallek
Honored Contributor

Re: create a file with date

Use the formatting options of the date command.

DATE=$(date +%Y%M%D)
TIME=$(date +%h%m)
FILENAME=THISFILE_${DATE}_${TIME}
touch ${FILENAME}
DIR=DIRNAME_${DATE}_${TIME}
mkdir ${DIR}

Check the date man page for all the options to format the date. I can't recall if the numerical date is a capital D or a lower case d.
Hoang Chi Cong_1
Honored Contributor

Re: create a file with date

Hi
Here is an example that create a file by using date command:
#touch `date +"%y%m%d"`
You can modify it.
This is very usefull when you create a script and auto create a file or directory.
For more see on the manpage!

Hope this helps
Hoang Chi Cong
Looking for a special chance.......
Muthukumar_5
Honored Contributor

Re: create a file with date

You can create a file or directory using DATE informations with date command manipulation.

# touch filename_$(date +"%X_%d-%m-%Y")

or

# touch filename_`date +"%X_%d-%m-%Y"`

You can store it in a variable and create it as,

# FILE="filename_$(date +'%X_%d-%m-%Y')

hth.
Easy to suggest when don't know about the problem!
Nguyen Anh Tien
Honored Contributor

Re: create a file with date

Patrick solution has some bug.
I correct it as:
DATE=$(date +%yy%mm%dd)
TIME=$(date +%h%M)
FILENAME=THISFILE_${DATE}_${TIME}
touch ${FILENAME}
DIR=DIRNAME_${DATE}_${TIME}
mkdir ${DIR}
or
DATE=$(date +%y%m%d)
TIME=$(date +%h%M)
FILENAME=THISFILE_${DATE}_${TIME}
touch ${FILENAME}
DIR=DIRNAME_${DATE}_${TIME}
mkdir ${DIR}
~
HP is simple