1830178 Members
2255 Online
109999 Solutions
New Discussion

Need shell script.....

 
SOLVED
Go to solution
Hoang Chi Cong_1
Honored Contributor

Need shell script.....

Goodday to all!
I am not good at shell programming...
I need to add in crontab a script to delete the old directory logfile.
For example: I just want to keep for the most 3 days current logfile directory:
drwxr-x--- 2 am users 96 Apr 17 12:00 20050410/
drwxr-x--- 2 am users 96 Apr 17 12:00 20050411/
drwxr-x--- 2 am users 96 Apr 18 12:00 20050412/
drwxr-x--- 2 am users 96 Apr 19 12:00 20050413/
drwxr-x--- 2 am users 8192 Apr 20 12:00 20050414/
drwxr-x--- 2 am users 8192 Apr 16 00:16 20050415/
drwxr-x--- 2 am users 8192 Apr 21 12:00 20050416/
drwxr-x--- 2 am users 8192 Apr 18 00:05 20050417/
drwxr-x--- 2 am users 8192 Apr 19 00:05 20050418/
drwxr-x--- 2 am users 8192 Apr 20 00:05 20050419/
drwxr-x--- 2 am users 8192 Apr 21 00:05 20050420/
drwxr-x--- 2 am users 8192 Apr 22 00:05 20050421/


In this example I just keep for these days: 21, 20, 19 and the older directory will be delete at 00:00 (by crontab)

Any helps will be much appreciated!

HoangChiCong
Looking for a special chance.......
22 REPLIES 22
Robert-Jan Goossens_1
Honored Contributor

Re: Need shell script.....

Hi,

Remove only empty directories ?

Robert-Jan
Joseph Loo
Honored Contributor

Re: Need shell script.....

hi,

a one liner will do:

# find
-atime +2 -type d -exec rm -R {} \;

regards.
what you do not see does not mean you should not believe
renarios
Trusted Contributor

Re: Need shell script.....

Hi,

I prefer using modification time above access time (Joseph's reply). If the directories are not empty, the -f option is necessary.
The syntax then would be:
find
-mtime +2 -type d -exec rm -Rf {} \;


Hope it helps you,

Renarios
Nothing is more successfull as failure
Hoang Chi Cong_1
Honored Contributor

Re: Need shell script.....

To Robert-Jan Goossens
Of course, it is not empty! It is logfile!
To Joseph Loo:
The full path: /home/ams/coms/log/
I tried with you command but nothing deleted!

Any idea?
Thanks!
Regard,
HoangChiCong
Looking for a special chance.......
Hoang Chi Cong_1
Honored Contributor

Re: Need shell script.....

Including empty directory!
But never empty because it contain all of the logfile as my last post!
Looking for a special chance.......
MarkSyder
Honored Contributor

Re: Need shell script.....

Did you do as Renarios suggested and run Joseph's command with mtime rather than atime? I would expect this to work.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Joseph Loo
Honored Contributor

Re: Need shell script.....

oops, u should be using mtime instead:

search first
# find /home/ams/coms/log/ -type d -mtime +2 -exec ls -ld {} ;

before doing this:

# find /home/ams/coms/log/ -type d -mtime +2 -exec rm -R {} ;

regards.
what you do not see does not mean you should not believe
Hoang Chi Cong_1
Honored Contributor

Re: Need shell script.....

Mark,
It runs but as not I want!
Here are directory after run that command (with mtime):

drwxr-x--- 2 am users 8192 Apr 20 12:00 20050414/
drwxr-x--- 2 am users 8192 Apr 21 12:00 20050416/
drwxr-x--- 2 am users 8192 Apr 20 00:05 20050419/
drwxr-x--- 2 am users 8192 Apr 21 00:05 20050420/
drwxr-x--- 2 am users 8192 Apr 22 00:05 20050421/

thanks
HoangChiCong
Looking for a special chance.......
MarkSyder
Honored Contributor

Re: Need shell script.....

The problem being that it deleted the directory for 19/4 and you wanted to keep that one?

I would suggest changing the mtime +2 to mtime +3 (but I expect that will have to wait till Monday to confirm it's worked).

Mark
The triumph of evil requires only that good men do nothing
Naveej.K.A
Honored Contributor

Re: Need shell script.....

Try this:

Find files modified between 3 and 8 days!!

# find /home/ams/coms/log/ -type d -mtime +3 -mtime +8 -exec ls -ld {} ;


# find /home/ams/coms/log/ -type d -mtime +3 -mtime +8 -exec rm -R {} ;

Regards,
Naveej
practice makes a man perfect!!!
Naveej.K.A
Honored Contributor

Re: Need shell script.....

Try this:

Find files modified between 3 and 8 days!!

# find /home/ams/coms/log/ -type d -mtime +3 -mtime -8 -exec ls -ld {} ;


# find /home/ams/coms/log/ -type d -mtime +3 -mtime -8 -exec rm -R {} ;

Regards,
Naveej
practice makes a man perfect!!!
Robert-Jan Goossens_1
Honored Contributor

Re: Need shell script.....

Hi,

Download the caljd.sh (aka A. Clay Stephenson's Date Hammer) script from below link to /usr/local/bin

http://mirrors.develooper.com/hpux/#Contrib

Create a small script.

#!/usr/bin/ksh
CALJD=/usr/local/bin/caljd.sh
# set date 3 days back
RMDATE=$($CALJD -Y -s $($CALJD -p 3))
# remove old log directory
/usr/bin/rm -r /home/ams/coms/log/$RMDATE

The first time you will need to delete the older directories yourself.

Hope this helps,
Robert-Jan
Geoff Wild
Honored Contributor

Re: Need shell script.....

Okay - should be easy - as dirs are dates..

#!/bin/sh
TODAY=`date +%Y%m%d`
DAY3=`expr $TODAY - 2`

for LOG in `ls /home/ams/coms/log`
do
if [ `expr $LOG \< $DAY3` = 1 ]
then
echo $LOG
rm -rf $LOG
fi
done


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Hoang Chi Cong_1
Honored Contributor

Re: Need shell script.....

Thanks to everyone...
Today is weekend so that I can not check for all of your script at home.
I will answer on next monday.
Later for points....

Thanks
HoangChiCOng
Looking for a special chance.......
Hoang Chi Cong_1
Honored Contributor

Re: Need shell script.....

To Mark Syder:
I have already changed mtime to +3 but that is not I expect.
To Naveej:
Had error in your command line:"find: -exec not terminated with ';'"
To Robert-Jan Goossens:
Could you attached that shell file? Silly but I can not find that file (caljd.sh)
To Geoff Wild:
It seems that just delete all of the directory, I have tried to change to :
"rm -Rf $LOG" but the same!

Any idea?
Thanks
HoangChiCong
Looking for a special chance.......
Robert-Jan Goossens_1
Honored Contributor
Solution

Re: Need shell script.....

Here it is (attached and linked) :-)

http://mirrors.develooper.com/hpux/caljd-2.23.sh

Robert-Jan
Geoff Wild
Honored Contributor

Re: Need shell script.....

Ah....I got it:

#!/bin/sh
TODAY=`date +%Y%m%d`
DAY3=`expr $TODAY - 2`

for LOG in `ls /home/ams/coms/log`
do
if [ `expr $LOG \< $DAY3` = 1 ]
then
echo $LOG
rm -rf /home/ams/coms/log/$LOG
fi
done
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Hoang Chi Cong_1
Honored Contributor

Re: Need shell script.....

OK, thanks to everybody!
May be I have to learn shell programming. :):):)
Can anyone show me some usefull document that teaching shell programming? (Anyway, for beginer is suitable for me - I thinks so :(:( ).

Thanks again.
Regard,
HoangChiCong
Looking for a special chance.......
Joseph Loo
Honored Contributor

Re: Need shell script.....

hi,

how about this doc:

http://docs.hp.com/en/B2355-90046/index.html

regards.
what you do not see does not mean you should not believe
Rob van Buiten
Regular Advisor

Re: Need shell script.....

Notice a part of 'man find':

-mtime n
True if the file modification time subtracted
from the initialization time is n-1 to n
multiples of 24 h. The initialization time
shall be a time between the invocation of the
find utility and the first access by that
invocation of the find utility to any file
specified in its path operands.


PME.
Geoff Wild
Honored Contributor

Re: Need shell script.....

Here's some links for you:

http://www.unixguide.net/unix/sedoneliner.shtml

http://pandonia.canberra.edu.au/OS/l3_1.html

http://www.shelldorado.com/shelltips/script_programmer.html

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Hoang Chi Cong_1
Honored Contributor

Re: Need shell script.....

Thanks to everybody who takes a time help me....OK! I will try more.....
Have a good time.
Regard,
HoangChiCong
Looking for a special chance.......