Bash comparing date

Cound anyone help me on how to compare date in Unix using if function on bash file?

current=date

if ["$current" = 7/12/06] ###syntax is wrong, could anyone correct it for me
then
rm -rf /usr/local/src
fi

Thank You...

Can anyone give me the syntax for the date to be recognize as date:[yyyy-mm-d]?

Notice the whitespace.

if [ "$current" = "7/12/06" ] ; then
rm -rf /usr/local/src
fi

it still doesn't work. date:command not found.

#!/bin/sh

current=date
if ["$current" = "7/12/06"]
then
echo "test"
fi

it worked!!! I just need to put the white spacing.. Thank You

the bash file worked with no error. But its not comparing the date i wanted it to be executed. Please help. Thank You.

There are plenty posts about date comparison on this forum, try to search first

Can you please give me the link where I can find the script I'm looking for. Thank You.

I think I didn't understand you, may be this is what you searching for

#!/bin/sh

current=`date +"%d/%m/%Y"`
if [ "$current" = "7/12/06" ]
then
echo "test"
fi

it still now working. This is what im trying to do. I want to delete a file for example in a specific date. Example is the date below. and the file to be removed is "/a". Please help. Thank You.

current='date +"%d/%m/%Y"'
if [ "$current" = "7/12/06" ]
then
rm -rf /a
fi

You need month-day-year or day-month-year both places. And month will have a leading zero...

current='date +"%m/%d/%Y"'
if [ "$current" = "07/12/06" ]
then
rm -rf /a
fi

current=`date +"%m/%d/%Y"`

back-quotes, not single-quotes

:stuck_out_tongue:

Opps, thanks for catching that.

#!/bin/sh

current=`stat -c %y /a | sed 's/ .*$//'`
if [ "$current" = "2006-07-12" ]
then
rm -rf /a
fi

Thank you for all you help... It work finally... :slight_smile:

Lastly can i put a greater than sign.. so if it would be greater than the date specified it would initiate the script.

if [ "$current" => "07/12/2006" ] ### is it possible?

Then it is better to use
$ find -name "/a" -ctime 1 -exec rm -rf {} \;

This is what ive done.

#!/bin/sh

current=`date +"%m/%d/%Y"`
if [ "$current" -ge "07/14/2006" ]
then
rm -rf /a
fi

This error occurs everytime I run the script.

[: 07/13/2006: integer expression expected

Please help. I'm really strugling with this comand. I've tried searching but I can't find solution on this script. Thank You.

try
current=`stat -c %Y /a`
You'll get time of last modification as seconds since Epoch
Then compare this seconds as numerical value