automatic file deletion if DF shows more than 70% use

Ask questions about Arch Linux ARM. Please search before making a new topic.

Re: automatic file deletion if DF shows more than 70% use

Postby karog » Fri May 17, 2013 10:30 pm

Once again you left out the "; done" for the "do" in the for loop within the while loop.
karog
 
Posts: 305
Joined: Thu Jan 05, 2012 7:55 pm

Re: automatic file deletion if DF shows more than 70% use

Postby g8jvm » Sat May 18, 2013 8:35 am

Thanks
I wasn't sure as there is done on the next line

thats why I left it out. back in it goes :)

Works nicely, with the echoing of the percentage used , you can see that decrease until it meets the required level.
The end statement isn't like at the moment , but I'll sort that.
There is a flaw in my method as motion is feeding .jpg files in to one directory and .avi movie files in to another.

Should the level of .jpgs get too high , it will force over deletion of the movie files to recover space.
It will be a good learning exercise to modify it to stop that happening.

many thanks for the help
Best wishes
Richard
richard@g8jvm.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ham Call: G8JVM , QRA IO82SP38 interests weak signal propagation
VHF to microwave, http://www.g8jvm.com
g8jvm
 
Posts: 93
Joined: Mon Apr 22, 2013 4:40 pm
Location: United Kingdom

Re: automatic file deletion if DF shows more than 70% use

Postby g8jvm » Sat May 18, 2013 12:50 pm

still a little stuck, lots of reading bash scripts and googling.
I'm attempting to limit file deletion if less than three days old,
once working all the echo statements will go they are only there while I'm blundering through it.

$this->bbcode_second_pass_code('', '#!/bin/bash

s=`df | grep sda5 | awk '{print $5}'| sed s'/.$//'`
echo "space used "$s"%"
Day=` date|awk '{print $3}'`
echo "todays day is " $Day
let E=$[ $Day -3 ]
echo "three days ago it was "$E
LT=12
echo "usage limit is" $LT"%"
echo " and we have " $s"%"
cd /Files/motion-pics/
# now try to test use level
if (( s > LT )); then
for x in $(ls -t | tail -n10) AND if [ ${x:0:2} -lt $E ]; then echo "$x"; done
fi
')
each file is date stamped dd-mm-yyyy;rest of file name.jpg or .avi
the ${x:0:2} grabs the first to digits , dd on the date stamp.
The wanted function is to logically AND the output of tail with the test on the first two digits of the file date stamp and echo the o/p, when working to be replaced by rm
Sorta get the 10 oldest files and delete only if older than so many days old
when run
$this->bbcode_second_pass_code('', '[root@Pogopig ~]# ./autodel
space used 18%
todays day is 18
three days ago it was 15
useage limit is 12%
and we have 18%
./autodel: line 15: syntax error near unexpected token `then'
./autodel: line 15: ` for x in $(ls -t | tail -n10) AND if [ ${x:0:2} -lt $E ]; then echo "$x"; done'
')

I haven't found any examples online of doing what I'm attempting, I'm trying to find how to in the advanced bash-scripting guide
It will probably be of use to others with similar application use.

I can get the function I want , but not a one liner
$this->bbcode_second_pass_code('', ' if (( s > LT )); then
for x in $(ls -t | tail -n10)
do
let y=${x:0:2};echo "$y" "$E"
if (( E > y )); then rm "$x"
echo "$y" "$x" "$E"
fi
done
')
The echo statements are for debug only
Best wishes
Richard
richard@g8jvm.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ham Call: G8JVM , QRA IO82SP38 interests weak signal propagation
VHF to microwave, http://www.g8jvm.com
g8jvm
 
Posts: 93
Joined: Mon Apr 22, 2013 4:40 pm
Location: United Kingdom

Re: automatic file deletion if DF shows more than 70% use

Postby g8jvm » Sun May 19, 2013 9:37 am

If anyone's interested here's the finished version which is running in cron.daily.
The date stamp on each file is dd-mm--yyy, I store pictures from motion in /Files/motion-pics and movies from motion in
/Files/motion-pics/movies. LT and MLT are the threshold for disk usage.. I set E , the oldest date to delete, to 3 days back from current date.
$this->bbcode_second_pass_code('', '#!/bin/bash

s=`df | grep sda5 | awk '{print $5}'| sed s'/.$//'`
echo "space used "$s"%"
dd=` date|awk '{print $3}'`
E=$[ $dd -3 ]
LT=45
MLT=43
echo "usage limit is" $LT"%"
#### first removes some .jpg files
cd /Files/motion-pics/
# now try to test use level
while (( s > LT )); do
for x in $(ls -t | tail -n10)
do
let y=${x:0:2}
if (( E > y )); then rm "$x"
s=`df | grep sda5 | awk '{print $5}'| sed s'/.$//'`
else
echo " changing to movies"
cd movies/
fi
done
done
### now loop until the usage is less than MLT%
while (( s > MLT )) ; do
for x in $(ls -t | tail -n10)
do
let y=${x:0:2}
if (( E > y )); then rm "$x"
s=`df | grep sda5 | awk '{print $5}'| sed s'/.$//'`
else
echo " Finished"
fi
done
done
exit

')
Thanks for the help getting it functional
Best wishes
Richard
richard@g8jvm.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ham Call: G8JVM , QRA IO82SP38 interests weak signal propagation
VHF to microwave, http://www.g8jvm.com
g8jvm
 
Posts: 93
Joined: Mon Apr 22, 2013 4:40 pm
Location: United Kingdom

Previous

Return to User Questions

Who is online

Users browsing this forum: No registered users and 6 guests