1831320 Members
4771 Online
110023 Solutions
New Discussion

Script

 
Waqar Razi
Regular Advisor

Script

I am storing the configuration information in a directory and then append the date to it. I am fine upto this point. I want to add some more functionality to it. i-e after storing the information, I want to keep the last 5 directories (5 most recent directories ) and delete the other.

Directories names have the following format:

drwxrwxrwx 2 root users 8192 Sep 12 11:50 lvm.120908
drwxrwxrwx 2 root users 8192 Aug 13 11:49 lvm.130808
3 REPLIES 3
Steven Schweda
Honored Contributor

Re: Script

> Directories names have the following format:

Step 1: Use a date format like yyyy=mm-dd,
so that the names naturally sort into date
order. Then simple programs like "head" or
"tail" can be used to find the last or first
N items in the list.
Pete Randall
Outstanding Contributor

Re: Script

Or - you could just use ls -t or ls -rt to get the names in order of time modified, then use head or tail.


Pete

Pete
Dennis Handly
Acclaimed Contributor

Re: Script

>Pete: you could just use ls -t or ls -rt to get the names in order of time modified,

This would fail if you just happened to remove a junk file in the directory. The right solution as Steven says is to use rational date formats and not chauvinistic ones.
ISO 8601
http://www.iso.org/iso/date_and_time_format