Operating System - Linux
1748195 Members
3922 Online
108759 Solutions
New Discussion юеВ

Re: need forums suggestions/expert opinion

 
Maaz
Valued Contributor

need forums suggestions/expert opinion

there is a directory /tmp/bu-logs

under /tmp/bu-logs directory there are following 5 directories

# ls /tmp/bu-logs
maaz ron kjames rts adn riz

inside maaz ron kjames rts adn riz there are directories for each day, e.g

# ls /tmp/bu-logs/maaz/
01-03-09
02-03-09
03-03-09
04-03-09
05-03-09
etc

each of the directory /tmp/bu-logs/maaz/01-03-09, contains some files

now I wrote a script that is suppose to deletes all those directories(under /tmp/bu-logs/maaz, /tmp/bu-logs/riz, etc) that are older than 30 days


#!/bin/bash
cd /tmp/bu-logs
for i in maaz ron kjames rts adn riz
do
find ./ -ctime +30 -exec rm -rf {} \;
done
exit



will the above code work ?
is there any mistake ?
please share your comments/suggestion

Regards
Maaz
5 REPLIES 5
Ivan Ferreira
Honored Contributor

Re: need forums suggestions/expert opinion

Hi Maaz, I don't know why you use a for loop in that case, you could just start at the parent directory.

Also, I would use the -mtime.

So, the result will be:

find /tmp/bu-logs -type f -mtime +30 -exec rm -fr {} \;
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Steven Schweda
Honored Contributor

Re: need forums suggestions/expert opinion

My first comment/suggestion would be to use
a more descriptive Subject when posting a
question.

> will the above code work ?

Test it and see?

If you have any doubts, and you wish to avoid
deleting the wrong files, then replace "rm"
with something like "echo", and look at what
the script would have done.
Maaz
Valued Contributor

Re: need forums suggestions/expert opinion

Hi thanks Ivan Ferreira for help
> I don't know why you use a for loop in that case, you could just start at the
> parent directory.
Because in /tmp/bu-logs/maaz, there are directories for each day, e.g
/tmp/bu-logs/maaz/03-03-09
/tmp/bu-logs/maaz/04-03-09
.
.
and 03-03-09 only contains the files that was created on 03-March-2009. so I want to delete all those directories, under /tmp/bu-logs/maaz/ that are older then 30 days.

I myself found the bug ;)

find ./ -ctime +30 -exec rm -rf {} \; # BUG

but following works
find $i/ -ctime +30 -exec rm -rf {} \;

#!/bin/bash
cd /tmp/bu-logs
for i in maaz ron kjames rts adn riz
do
find $i/ -ctime +30 -exec rm -rf {} \;
done
exit

> Also, I would use the -mtime.
Thanks, for the suggestion/tip

Dear Steven Schweda
> My first comment/suggestion would be to
> use a more descriptive Subject when posting a question.

thanks for your kind suggestion... Forum(esp Steven Schweda) please accept apology from my side

> If you have any doubts, and you wish to avoid deleting the wrong files,
> then replace "rm" with something like "echo", and look at what the script
> would have done
Thanks

Thanks once again.
Regards
Steven E. Protter
Exalted Contributor

Re: need forums suggestions/expert opinion

Shalom,

You code will hurt nothing.

Test it.

I like Ivan's too.

Test his.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Ivan Ferreira
Honored Contributor

Re: need forums suggestions/expert opinion

Maaz, I think that you are confused, or I'm not having the whole picture.

Doing a loop or not, is the same. Jumping to each directory to find files older then 30 days is the same as finding files older than 30 days on the parent directory.

You could also specify a list of directories to find without a loop, for example:

find maaz ron kjames rts adn riz -type f -mtime +30 -exec rm -f {} \;
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?