1755136 Members
3175 Online
108830 Solutions
New Discussion

Need a clean up script

 
SOLVED
Go to solution
allanm77
Frequent Advisor

Need a clean up script

Hi!

 

I have a dirctory which has a list of packages (both old and ones which are currently running)-

 

dir1> ls -ltr

app1-oldpackage

app2-oldpackage

app3-oldpackage

app1-current-package

app2-current-package

app3-current-package

 

I currently run ps -ef |grep <package-name> to find out which are currently running and then rm -fr old-packages to create space.

 

Is there a better way to create a script and cleanup using that.

 

Thanks,

Allan

 

 

 

4 REPLIES 4
Dennis Handly
Acclaimed Contributor

Re: Need a clean up script

If your packages don't contain a file with the current running PID there isn't much else you can do.

Or if you aren't using them, move them into an "OLD" directory, until you know for sure you want to remove them.

allanm77
Frequent Advisor

Re: Need a clean up script

Hi Dennis,

 

One thing I missed mentioning here,

 

I do an ls -ltr |awk ... |sort to get a list of packages with that dir, the packages have date/time stamp on them and they get sorted out, I want to delete the packages which are of older timestamp.

 

Timestamp is of this pattern - <appname>-20120330-143912-456557f3f5b34a8091dcecd69ec123dd

 

I want to delete any packages which are older than the new package.

 

ls -ltr |awk ... |sort

 

Appname1-20120330-143912-756557f3f5b34a8091dcecd69ec12423
Appname1-20120402-173057-e30fc8463a1d444185983ccb6d319c33

Appname1-20120404-182519-c32abb08f0bb4ae0bb887818d5c14022

 

Appname2-20120402-173057-e30fc8463a1d444185983ccb6d319c33

Appname2-20120404-182519-c32abb08f0bb4ae0bb887818d5c14022

 

Thanks,

Allan.

 

allanm77
Frequent Advisor

Re: Need a clean up script

I am thinking of using -atime with find to have this taken care of.

 

Do agree that a longer term solution is to push the older packages to differnt fs.

 

Thanks,

Allan.

Dennis Handly
Acclaimed Contributor
Solution

Re: Need a clean up script

>I want to delete any packages which are older than the new package.

 

Assuming there is a "-" between the app and the timestamp, this should do it:

ls -l | awk '
BEGIN { prev = ""; app = "" }
{
# get appname
n = split($9, apps, "-")
if (n == 0) next  # total line
if (n < 2) {
   print "problem getting appname for", $9
   next
}
appname = apps[1]
if (appname == app) {
   print prev  # older
   prev = $9   # save
   next
}
# ignore prev, since last/only
app = appname
prev = $9
} '

 

This prints all but the last of any application group.