Operating System - HP-UX
1832660 Members
3117 Online
110043 Solutions
New Discussion

Re: date format with a file

 
Robert DJ
Frequent Advisor

date format with a file

Hi,

I wanted to add date format along with a file, can anyone help...

for e.g
date +%Y%m%d which gives 20051123, this should be attached to a file to say like "test.20051123".......

Thank you in advance.

Regards,
Robs
Robert DJ
10 REPLIES 10
Muthukumar_5
Honored Contributor

Re: date format with a file

Then do as,

tocuch test.$(date +%Y%m%d)

hth.
Easy to suggest when don't know about the problem!
Luk Vandenbussche
Honored Contributor

Re: date format with a file

Hi

Try this

dateA=`date +%Y%m%d`
cat abc > test.$dateA
Muthukumar_5
Honored Contributor

Re: date format with a file

typo as,

touch test.$(date +%Y%m%d)

or as,

cat > test.`date +%Y%m%d`
hi
ctr+d

or

cp /dev/null test.`date +%Y%m%d`

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

Re: date format with a file

Luk,

A note for you.

If we are using the code of,

dateA=`date +%Y%m%d`
cat abc > test.$dateA

with shell script then,

dateA will be with 20051123.

test.($2)0051123

if we give argument with more than one then, second argument is substitued (say example ./script.ksh first luk third)
then,

file will be like,

test.luk0051123

to over ride that,

dateA=`date +%Y%m%d`
cat abc > test.${dateA}

that is all.

hth.
Easy to suggest when don't know about the problem!
Robert DJ
Frequent Advisor

Re: date format with a file

Hi Guys,

The thing is it works if it is a simple file creation....

I would like to append to the file with each output that arrives through my commands,

for e.g,
the commands i try might bdf, swapinfo and so on....

these outputs needs to be appended along with the file "test.20051123"

The normal way of the using the date command doesnt seem to accept the redirections....

i tried using the following,

dateA='date +%Y%m%d'
touch log.$dateA

bdf >> log.$dateA

can u help........

Thank you.

Regards,
Robs
Robert DJ
saju_2
Respected Contributor

Re: date format with a file

HI

Have u used 'date +%Y%m%d' or `date +%Y%m%d`

There is difference in the quotes being used.

IF you want the variable dateA to be available in that shell, do

export dateA=`date +%Y%m%d`
touch log.$dateA
bdf>>log.$dateA

It works for me...!!!!

Regrds
CS
Muthukumar_5
Honored Contributor

Re: date format with a file

See my above reply.

Use like,

FILE="log.$(date +%Y%m%d)"
bdf >> ${FILE}
swapinfo -tam >> ${FILE}

else
FILE="log.$(date +%Y%m%d)"
(
bdf
swapinfo -tam
hostname
ls
) > ${FILE}

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

Re: date format with a file

Use another way as,

{
command1
command2
...
commandn
} > test.$(date +%Y%m%d)

To get that result in console also then,

{
command1
command2
...
commandn
} | tee -a test.$(date +%Y%m%d)

tee with -a will append results more.

Easy to suggest when don't know about the problem!
Robert DJ
Frequent Advisor

Re: date format with a file

Thank you Muthu...It worked......

Regards,
Robs
Robert DJ
Robert DJ
Frequent Advisor

Re: date format with a file

It worked......Thank you.
Robert DJ