Operating System - Linux
1753295 Members
6520 Online
108792 Solutions
New Discussion юеВ

Re: "delete" shell script

 
SOLVED
Go to solution
Jano_1
Advisor

"delete" shell script

Hi,

I need a shell script to monitor a directory's size. When there is no more space left on the disk I want to delete some subdirectories. The subdirectories are in the format:
backup_of_20041011
backup_of_20041012
where 20041011 is the date.

The last 10 (newest) subdirectories should not be deleted, only the oldest ones.

Can I easily find such a script, and if so where is a good place to look?

If not, what are the main functions needed to accomplish these tasks? I am new to Bash programming so any comments are welcome.

Regards.
9 REPLIES 9
Steven E. Protter
Exalted Contributor

Re: "delete" shell script

Attaching an example script that selects files based on age. the +mtime option is the critical thing to look at.

This script uses external libraries, but you don't need that.

My script cleans up pdf files but is adaptable.

Better than seeing if the filesystem is full, its a good idea to pick a certain number of days retention and be done with it. Makes the process less complex.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Jano_1
Advisor

Re: "delete" shell script

Hi Steven,

It is an impressive script that you attached. However I am looking for something quite simple. I decided to retain 10 directories.

ls -1 list my directories.

How do I put this output in an array so I can delete a directory per element reference? eg.
list{1)
list{2}
...
list{y}

And y-10=x, which means I should delete
list{1) to list{x}

Regards.
Rick Garland
Honored Contributor
Solution

Re: "delete" shell script

A 1-liner way of doing this. I'm not sure how you determine which 10 directories to retain so I am going in order - forward and backwards.

from the directory you are working with;
ls -l | grep '^d' | head -10 - this will produce a list of the first 10 directories.

ls -l | grep '^d' | tail -10 - this will producr a list of the last 10 directories.

rm -rf `ls -l | grep '^d' | head -10` - This will force remove the first 10 directories. You can add additional conditions to this command, such as grep -v. (Notice the backticks with the rm command.)
Jano_1
Advisor

Re: "delete" shell script

Hi Rick,

Cool answer- I will try it out.

Regards.
Jano_1
Advisor

Re: "delete" shell script

Hi,

Does anyone know what is wrong with this line in my script:
rm -rf `ls -1 $DIR | head -$TODELETE` &
The line proceeding this one prints the directories to be deleted perfectly, but this line does not remove anything, and also gives no error.

Regards.
Jano


#!/bin/sh

DIR="/mnt/nas/"
# amount of directories to keep
TOKEEP=10

TOTAL=0
# count total directories
TOTAL=`ls -l $DIR | grep '^d' | wc -l`

echo Total $TOTAL
echo Tokeep $TOKEEP

#test if TODELETE > 0
TODELETE=$[TOTAL-TOKEEP]
echo Todelete $TODELETE

if [ "$TODELETE" -gt 0 ] ; then
# something to do so let's continue
# ls -1 $DIR | head -$TODELETE
rm -rf `ls -1 $DIR | head -$TODELETE` &
else
# nothing to do so let's stop
echo nothing to delete
fi
James A. Donovan
Honored Contributor

Re: "delete" shell script

Add "set -xv" as the second line in your script and re-run. That will let you know what the values of the variables in your script are at run-time.
Remember, wherever you go, there you are...
Jano_1
Advisor

Re: "delete" shell script

Hi,

From the last line of the debug I can see that the script is trying to delete the right directory (
+ rm -rf backup_of_20041018)
but it looks as though the path (DIR=/mnt/nas/) got lost somehow.

Here is the output as requested:

DIR="/mnt/nas/"
+ DIR=/mnt/nas/
# amount of directories to keep
TOKEEP=10
+ TOKEEP=10

TOTAL=0
+ TOTAL=0
# count total directories
TOTAL=`ls -l $DIR | grep '^d' | wc -l`
ls -l $DIR | grep '^d' | wc -l
++ ls -l /mnt/nas/
++ grep '^d'
++ wc -l
+ TOTAL= 11

echo Total $TOTAL
+ echo Total 11
Total 11
echo Tokeep $TOKEEP
+ echo Tokeep 10
Tokeep 10

#test if TODELETE > 0
TODELETE=$[TOTAL-TOKEEP]
+ TODELETE=1
echo Todelete $TODELETE
+ echo Todelete 1
Todelete 1

if [ "$TODELETE" -gt 0 ] ; then
# something to do so let's continue
# ls -1 $DIR | head -$TODELETE
rm -rf `ls -1 $DIR | head -$TODELETE` &
else
# nothing to do so let's stop
echo nothing to delete
fi
+ '[' 1 -gt 0 ']'

exit 0
+ exit 0
[root@server]# ++ head -1
+ rm -rf backup_of_20041018

Re: "delete" shell script

Jano,

The directory part given as argument to the ls command is not included in the output.

If you change this line:
rm -rf `ls -1 $DIR | head -$TODELETE` &

To:
ls $DIR | head -$TODELETE |
while read TARGET
do
rm -rf $DIR/$TARGET
done

But test it first by replacing the "rm -rf" part with "echo" to see that the correct dirs are displayed.
shanker_3
New Member

Re: "delete" shell script

Hi Steven E. Protter

Saw your script, but my need to get the list of the versions in the partition having more disk usage., if we come across the no space left on the device we come across the struggle to increase space.

could you help me to give me some what helpful to me and all..

my requirement: to list versions occupy for a particular package, and how many times a same space contained versions are distributed.

thanks in advance in helping me..
Regards,
Shanker G.