• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Keep only last 3 versions of a file

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tar files get stored in a directory with name in the following format

<file>21Jun0922:19:46.tar.gz

I need to write a script which when run should keep only the latest 3 versions of the file and delete the other versions. This could be achieved by reading the date and time form the tar name. But how to determine which ones are the latest 3 versions.

Could anyone please help me regarding the problem?
 
Saloon Keeper
Posts: 28761
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can rely on the file's own filesystem date, the output of "ls -l[r]t" can be used. Something like:

ls -lrt | tail -l -3 | xargs rm

WARNING: Major corrections probably required.

I prefer my naming conventions to be more in the line of "fileYYYYMMDD.tar.gz" myself. Not only can you use a simple sort, it avoids confusion about American/English date formats (MM/DD/YY vs. DD/MM/YY) - not to mention other countries. Named months are not well-suited for date arithmetic, even when international issues don't figure in.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic