LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-12-2013, 12:41 AM   #1
allanm78
LQ Newbie
 
Registered: May 2013
Posts: 10

Rep: Reputation: Disabled
Script to cleanup older packages


Hi All,

I have a filesystem which houses application packages. The packages are of following format type (for a given APP) -

APPNAME-20130702-090325-6a33852ae7234898b54508d0e66e636d
APPNAME-1-8-20130801-090321-22


where 20130801-090321 & 20130702-090325 are timestamps.
I want to get a list of packages sorted based on the timestamps for all apps and it should skip the last three latest packages of each app.

Earlier I used the following script to get the list sorted out and this would skip the latest package and format of package was of format first (listed above).

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

Please advise on how I can use this script or a different one based on my requirements.

Thanks.
Allan
 
Old 08-12-2013, 07:44 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Well, if I understood your problem, here's one solution:
Code:
#!/bin/gawk -f
BEGIN {
  cmd="ls -1"
  while (cmd | getline input) { 
    n = split(input, apps, "-")
    if (n < 2) {
      print "Problem getting appname for " input " (There were no dashes in the name.)" > "/dev/stderr"
    }
    else {
      appname = apps[1]
      k = ++app_count[appname]
      applications[appname][k] = input 
    }
  }
  close(cmd)
  for (appname in app_count) {
    print appname
    for (k=1; k < app_count[appname] - 2; ++k) {
      printf("\t%s\n", applications[appname][k])
    }
  }
}
For the output of this test case generator:
Code:
#!/bin/gawk -f
BEGIN {
  for (i=1; i<11;++i) {
    system("touch testapp-20130812-" i "-0")
    for (j=1;j<=i;++j) {
      system("touch testapp" i "-20130812-version" i "-" j)
    }
  }
}
and using chmod to make the scripts executable, I got this:
Code:
$ ./allenm78.awk 
Problem getting appname for allenm78.awk (There were no dashes in the name.)
Problem getting appname for make_test_data.gawk (There were no dashes in the name.)
testapp8
        testapp8-20130812-version8-1
        testapp8-20130812-version8-2
        testapp8-20130812-version8-3
        testapp8-20130812-version8-4
        testapp8-20130812-version8-5
testapp9
        testapp9-20130812-version9-1
        testapp9-20130812-version9-2
        testapp9-20130812-version9-3
        testapp9-20130812-version9-4
        testapp9-20130812-version9-5
        testapp9-20130812-version9-6
testapp10
        testapp10-20130812-version10-1
        testapp10-20130812-version10-10
        testapp10-20130812-version10-2
        testapp10-20130812-version10-3
        testapp10-20130812-version10-4
        testapp10-20130812-version10-5
        testapp10-20130812-version10-6
testapp1
testapp2
testapp3
testapp4
        testapp4-20130812-version4-1
testapp5
        testapp5-20130812-version5-1
        testapp5-20130812-version5-2
testapp6
        testapp6-20130812-version6-1
        testapp6-20130812-version6-2
        testapp6-20130812-version6-3
testapp7
        testapp7-20130812-version7-1
        testapp7-20130812-version7-2
        testapp7-20130812-version7-3
        testapp7-20130812-version7-4
Notes:
  1. The out[ut section:
    Code:
      for (appname in app_count) {
        print appname
        for (k=1; k < app_count[appname] - 2; ++k) {
          printf("\t%s\n", applications[appname][k])
        }
      }
    could be rewritten as
    Code:
      for (appname in app_count) {
        for (k=1; k < app_count[appname] - 2; ++k) {
          printf("%s\n", applications[appname][k])
        }
      }
    to produce only the files you wish to delete.
  2. If you use the script as part of a pipeline, be sure to direct the error output to /dev/null.
  3. By changing the options to the ls command in line 2 of the script, you can change the file ordering. The output of the script should be all but the last three files in the order listed by the ls command.
  4. If you want a different number of files for each application excluded from the list, replace the 2 in line 19 of the script with one less than the number of files you wish to exclude.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Where are 'older' patch packages for 13.37? larrybpsu Slackware 5 04-05-2012 09:51 PM
bash simple cleanup script then curl sir-lancealot Programming 1 02-02-2011 01:59 PM
Need older current packages macbeth8 Slackware 8 12-01-2007 07:55 AM
installing older packages... ozzie Linux - Newbie 1 06-02-2004 08:40 AM
log cleanup script teacup Programming 2 09-24-2003 12:32 PM


All times are GMT -5. The time now is 10:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration