1829404 Members
1902 Online
109991 Solutions
New Discussion

Calculating Date

 
SOLVED
Go to solution
Rafael Mendonça Braga
Regular Advisor

Calculating Date

Hello All,

I'm trying to create a script to remove files older than 90 days from a specific directory automatically. I have never done this before. I have no idea from where to start, so I'm calling your help.

I don't know the best way to take the current date and subtract from it 90 days.
I was taking the current date this way:
DATE=`date "+%b %d"`
This way, i was receiving the DATE like this "FEB 22". I was thinking in use it because when we do "ls -al" we got the output like this one:

-rw------- 1 ssbrmb users 1200 Jan 10 08:50 .ICEauthority
-rw------- 1 ssbrmb users 154 Jan 10 08:50 .TTauthority
-rw------- 1 ssbrmb users 98 Jan 10 08:50 .Xauthority
-r--r--r-- 1 ssbrmb users 832 Nov 14 2000 .cshrc
drwxr-xr-x 11 ssbrmb users 8192 Jan 12 09:25 .dt
-rwxr-xr-x 1 ssbrmb users 5451 Dec 15 16:40 .dtprofile
-r--r--r-- 1 ssbrmb users 347 Nov 14 2000 .exrc
-r--r--r-- 1 ssbrmb users 334 Nov 14 2000 .login
-r--r--r-- 1 ssbrmb users 439 Nov 14 2000 .profile
-rw------- 1 ssbrmb users 2440 Feb 21 08:52 .sh_history

So i thought the best way to subtract the date was using the same format...

But I don't get the solution...

Can you help me?

Thanks,

Rafel M. Braga
4 REPLIES 4
Jean-Luc Oudart
Honored Contributor
Solution

Re: Calculating Date

Hi,

why not use the find command ?

find -mtime "+90"

Regards
Jean-Luc
fiat lux
Pete Randall
Outstanding Contributor

Re: Calculating Date

To expand on that:

find /dir -type f -mtime +90 -exec rm {} \;

- or -

find /dir -type f -mtime +90 |xargs rm


Pete

Pete
Henk Geurts
Esteemed Contributor

Re: Calculating Date

hi Rafel

you can use the find command:

find /path/to/dir -mtime +30 -exec ll {} \;
(gives a long list of files modified more than 30 days ago)

find /path/to/dir -mtime +30 -exec rm {} \;
(to delete them)


regards.
Rafael Mendonça Braga
Regular Advisor

Re: Calculating Date

Thanks guys!!!