1822678 Members
3780 Online
109644 Solutions
New Discussion юеВ

Re: Script help

 
SOLVED
Go to solution
Anthony khan
Frequent Advisor

Script help

Hi All,

There are subdir and files under /prod/moe/db directory, the subdir are Feb07_2002, Feb12_2002 and Feb18_2002, I want to write a script which looks for latest 3 directories and remove the rest (only directory not files), like if system create 1 dir today which is Mar_01_2002, so I want to remove Feb07_2002, any suggestion

Thanks in advance
10 REPLIES 10
A. Clay Stephenson
Acclaimed Contributor

Re: Script help

If this is an option, the first thing I would do is to get rid of the month names and substitute number. You don't have to do this but it adds a level of complexity to what would be rather simple.
I would also turn the naming convention around so that the directories are YYYY_MM_DD, then problem then becomes very easy.
If it ain't broke, I can fix that.
Eric Ladner
Trusted Contributor

Re: Script help

Ouch.. can you change the name of the directories? YYYYMMDD names work much nicer for this type of stuff (they sort into the right order with a regular sort).

Other than that, you'd probably have to do something with date conversion in perl, etc.
Volker Borowski
Honored Contributor

Re: Script help

Hi,

what about this:

mkdir /prod/moe/park
cd /prod/moe/db
DIRS=`ls -1rt | tail -n 3`
echo Directories to be saved: $DIRS
mv $DIRS /prod/moe/park
rmdir *
mv /prod/moe/park/* .


If the directories are not empty, script may fail with rmdir. check if "rm -r" would be a solution, but be careful with this.
May be you need to qualify wildcards in the "ls" if the directory contains other stuff.
Option to ls "one", not "ell"!

Hope this helps
Volker




Rodney Hills
Honored Contributor

Re: Script help

Assuming their are no other directories than the month_day_year and the last modify date on these directories reflects that date, then you can do the following-

cd /yourdbpath
ll -t | awk '/^d/{s=s+1 ; if (s > 3) system("rn -R " $9)}'

This will sort the output from a ll command by last modify date, pipe the output to awk who will only look for directories (/^d/), and only execute the "rm -r" after the first 3 have passed by.

-- Rod Hills

There be dragons...
Anthony khan
Frequent Advisor

Re: Script help

Wolker,

I like it but there is one problem that there are some files related to latest directory which i don't want to remove and these dir not empty it got 20 big files, thats the reason i want to get rid of old directries, Any idea
Volker Borowski
Honored Contributor

Re: Script help

Hi again,

at least you must be distinctive in the naming of the other files.

Would *_*_* as a wildcard give you only the directories you need, or is it possible that the other files may contain two or more "_" as well ?

Another option might be to choose

DIRS=`ls -1rt \`find . -type d\` | tail -n 3`

Watchit 1: cascaded reverse quotes require \ sign
Watchit 2: ls -1rt -- sort by timestamp, NOT by name, so if the directories are created with name and date not matching, you might be trapped.
Consider to change to YYYYMMDD convention if possible, or better YYYYMMDD_MYDIR because *_MYDIR would give you a very distinct wildcard.

Volker
Volker Borowski
Honored Contributor

Re: Script help

Forgot:

ls -1drt


"d" to list only the directory name if it is a directory. Not the contents of the dir !

V.
Krishna Prasad
Trusted Contributor

Re: Script help

You could do a ls -ltr /prod/moe/db | tail -1 and give the lastest directory.

However, if there are files in that directory you will have to either move them some where or remove them before you can remove the directory.

Once you know the directory in /prod/moe/db you could try

x=`ls /prod/moe/db/$last_dir`

for file in $x
do
mv $file /new_dir
done

rmdir /prod/moe/db/$last_dir
Positive Results requires Positive Thinking
Patrick Wallek
Honored Contributor

Re: Script help

Here's a script to try on for size. I have also attached it for you.

It will go through the current directory and find all directories that start with Jan, Feb, Mar, etc. and count them. If the count is greater than 3, then it will find the oldest directories starting with Jan, Feb, Mar, etc. and remove them.

#!/sbin/sh

# Find number of dirs in current dir
NUM_DIR=`find . -type d -name "[Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec]*" | wc -l`

echo "$NUM_DIR directories here"

if [ $NUM_DIR -gt 3 ]
then

let NUM_TO_REMOVE=$NUM_DIR-3
echo "$NUM_TO_REMOVE to remove"
DIR=`find . -type d -name "[Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec]*" -print`
DIR_TO_RM=`ls -1drt $DIR | head -$NUM_TO_REMOVE`
echo "The following directories should be removed:"
echo "$DIR_TO_RM"
for i in `echo $DIR_TO_RM`
do
echo "Removing $i"
rm -r $i
done
else
echo "No directories to remove"
fi

SHABU KHAN
Trusted Contributor
Solution

Re: Script help


Hi Anthony,

I presume the directories Feb07_2002, Feb12_2002 etc.. are created by some rotatelogs script and the directories correspond to the actual date like this:
Mar 1 12:32 Mar01_2002/
Feb 18 12:30 Feb18_2002/
Feb 12 12:30 Feb12_2002/
Feb 7 12:30 Feb07_2002/

If that is the case and if there is some consistency in creating the directories then you could do something like this:
LOGDIR="/prod/moe/db"
cd "${LOGDIR}"
find . -type d -mtime +20 -exec rm -rf {} \;

rm -rf is a dangerous and powerful command ... I would test several scenarios before deploying it...

Hope this helps..

Thanks,
Shabu