1

How can I create a file in unix or linux based on variable value ?

Ex:

If I store date in a variable in linux,

YESTERDAY=`date --date='1 day ago' '+%d-%m-%Y'`

it will store value to YESTERDAY as 27-1-2010.

Here I want to create file as name of 27-1-2010,

How can I create file with variable 'YESTERDAY' ?

i want appending operation too. How can i do this ?

Kumar
  • 823
  • 3
  • 20
  • 43
  • 1
    Unless you've got a compelling reason (as in, some application already expects this date format), please do it Y-m-d and keep yourself sane. d-m-Y never sorts right, and over time, you'll be very upset. – Matt Simmons Feb 02 '10 at 04:45
  • @Matt Simmons , Thanks i will change at my real time usage. – Kumar Feb 02 '10 at 05:29

4 Answers4

2
touch $YESTERDAY

or

echo "something" > $YESTERDAY

to append:

echo "something" >> $YESTERDAY
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
1

I don't see anything wrong with echo "foo" >> $YESTERDAY or cat otherfile >> $YESTERDAY

What are you trying to cat? Or alternately what are you trying to put into the file called 27-1-2010?

violet
  • 486
  • 3
  • 6
1

YESTERDAY=date --date='1 day ago' '+%d-%m-%Y'

cat >> $YESTERDAY

It too working well with appending operation

Kumar
  • 823
  • 3
  • 20
  • 43
0

You can use the variable $YESTERDAY in commands. Like touch $YESTERDAY, mv original_file $YESTERDAY

Amandasaurus
  • 31,471
  • 65
  • 192
  • 253