automatic file deletion if DF shows more than 70% use

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

automatic file deletion if DF shows more than 70% use

Postby g8jvm » Fri May 17, 2013 11:57 am

Hi
Running motion and I'm using a lot of file space with.jpg and .avi files
I need to run ideally a daemon to delete the oldest files once the used space in a directory exceeds say 70 %.
Is there any application that will do this ?
or is it a case of write a script and use cron or something to invoke it say once an hour.
$this->bbcode_second_pass_code('', '[richard@localhost Live_MUF_V73]$ df --direct /dev/sda5
Filesystem 1K-blocks Used Available Use% File
- 1993968 0 1993968 0% /dev/sda5
')

using the above maybe using SED or AWK to get the third block, Used, and then sorta
If [SED/AWK ] > 70
then delete, and here's a hard bit, all order than 1 day

rm doesn't have switches to delete on age

the start of each file is date stamped in day-month-year

maybe using SED or AWK to get the greatest decimal day and the lowest decimal day and delete all less than the greatest decimal day.


I would really appreciate some help with this, as its not as simple as "just delete everything older than a day if too much disk space is used."

Thanks
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 karog » Fri May 17, 2013 1:50 pm

Try something like:
$this->bbcode_second_pass_code('', '
cd /whatever/dir
for x in $(ls -t | tail -n1); do echo "$x"; done')
where ls -t lists files in time order oldest last and tail -n1 picks off the last one.

Change echo to rm when you are comfortable.

You can put that in a while loop that keeps deleting a file until you are below threshold in df.

Run by cron.
karog
 
Posts: 305
Joined: Thu Jan 05, 2012 7:55 pm

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

Postby g8jvm » Fri May 17, 2013 3:44 pm

Thanks
That simplifies the file deletion, I'll play with that
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 » Fri May 17, 2013 5:43 pm

If I use somet6hing like :- $this->bbcode_second_pass_code('', 'S1=df --direct /dev/sda5 | awk '{print $5}'
if S2=[ $S1 GT 50]
then cd /Files/motion-pics
for x in $(ls -t | head -10); do rm -f "$x"
else
exit
fi
')

That I think will remove the first 10 oldest files, and looped until df returns 50 or less

One problem is the df line returns
Use%
20%
I could use SED to remove the percentage sign, but I'm not sure how to with the output from df on two lines
Thanks
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 karog » Fri May 17, 2013 6:20 pm

$this->bbcode_second_pass_code('', '
S1=df --direct /dev/sda5 | grep sda5 | awk '{print $5}'
S1=${S1%%%}
')
The first line adds grep so you only get the one desired line.

The second line removes the %.

Also why did you change tail to head? You are going to delete the newest 10 if you use head.

Use echo rather than rm until you see that you are deleting the correct files.
Last edited by karog on Fri May 17, 2013 9:30 pm, edited 1 time in total.
karog
 
Posts: 305
Joined: Thu Jan 05, 2012 7:55 pm

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

Postby g8jvm » Fri May 17, 2013 7:14 pm

Great :)
maybe getting a bit confused with head and tail,
ls -t lists files in date order, when you use it, it scrolls up, hence the confusion, its this getting old function.
sorry my fault as you say tail will give the oldest by modification date, may be ls -tr would work with head, but tail is simpler.

many thanks
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 pepedog » Fri May 17, 2013 7:28 pm

I don't know your file space limitation, but I would have gone for simple once-a-day delete files in backup folder, then move files to backup folder. Nothing can be over 24 hours old then.
pepedog
Developer
 
Posts: 2431
Joined: Mon Jun 07, 2010 3:30 pm
Location: London UK

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

Postby g8jvm » Fri May 17, 2013 8:37 pm

Hi
when I get the camera looking for movement indoors when the house is unoccupied that would be fine, but while testing its point out at the road and depositing a lot of pics and movies.

I'm getting so far with the script

$this->bbcode_second_pass_code('', '[root@Pogopig ~]# df | grep sda5 | awk '{print $5}'| sed s'/.$//'
17
')
that works OK on CLI
but the script
$this->bbcode_second_pass_code('', '#!/bin/bash
# S1=df | grep sda5 | awk '{print $5}'
s= `df | grep sda5 | awk '{print $5}'| sed s'/.$//'`;echo $s
# echo $S1
#S1=${S1=%%%} : it didn't like this, and not echoing $s or $S1

# now try to test use level
if [ $s -gt 10 ];then cd /Files/motion-pics/movies; for x in $(ls -t | tail -n10); do echo "$x"
else
exit 1
fi
')

I've also tried
$this->bbcode_second_pass_code('', '#!/bin/bash
# S1=df | grep sda5 | awk '{print $5}'
s= `df | grep sda5 | awk '{print $5}'| sed s'/.$//'`
echo $s
#S1=${S1=%%%} : it didn't like this, and not echoing $s

# now try to test use level
if [ $s -gt 10 ]
then cd /Files/motion-pics/movies; for x in $(ls -t | tail -n10); do echo "$x"
else
exit 1
fi
')
If I run it i get
$this->bbcode_second_pass_code('', './[root@Pogopig ~]# ./autodel
./autodel: line 3: 17: command not found

./autodel: line 10: syntax error near unexpected token `else'
./autodel: line 10: ` else'


')

I've tried "then cd/Files/....." on a new line tabbed back, but that made no difference

by the way its throwing a error on the else statement it looks like its not seeing if $s is greater than 10 ($s should be 17, so greater)

Something daft and I cant see it, sorry
TIA
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 karog » Fri May 17, 2013 9:32 pm

$this->bbcode_second_pass_code('', '#S1=${S1=%%%} : it didn't like this')
Oops. That was a typo I have now fixed above. Should be
$this->bbcode_second_pass_code('', '#S1=${S1%%%} ')

In your script you have a space after s= and before `df... You MUST remove it. That is the line 3 error.

The "else" error is that you left out the "; done" after the do echo "$x"

The entire if then else would be better written
$this->bbcode_second_pass_code('', '
if [ $s -le 10 ]; then exit 1; fi
cd /Files/motion-pics/movies
for x in $(ls -t | tail -n10); do echo "$x"; done
')
karog
 
Posts: 305
Joined: Thu Jan 05, 2012 7:55 pm

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

Postby g8jvm » Fri May 17, 2013 10:20 pm

Thanks that is much tidier.
so far , I found the space and removed it
and the first section is working, albeit messy$this->bbcode_second_pass_code('', '
#!/bin/bash

s=`df | grep sda5 | awk '{print $5}'| sed s'/.$//'`
echo "space used "$s"%"

LT=15
echo $LT
cd /Files/motion-pics/movies
# now try to test use level
if (( s > LT )); then
for x in $(ls -t | tail -n10); do echo "$x";done
fi
#### now loop until the usage is less than LT%
while (( s > LT )) ; do
for x in $(ls -t | tail -n10); do rm "$x"
let s=`df | grep sda5 | awk '{print $5}'| sed s'/.$//'`
echo $s
done
')
The else statement can be removed altogether , it seems to function OK without it.
the second section is not happy
$this->bbcode_second_pass_code('', '[root@Pogopig ~]# ./autodel
space used 17%
15
17-05-2013_12:42-05.avi
17-05-2013_12:38-05.avi
17-05-2013_12:38-04.avi
17-05-2013_12:32-04.avi
17-05-2013_12:32-03.avi
17-05-2013_12:30-03.avi
17-05-2013_12:29-02.avi
17-05-2013_12:22-02.avi
17-05-2013_12:22-01.avi
01-01-1970_01:00-01.avi
./autodel: line 27: syntax error: unexpected end of file
')
there isn't a line 27, so still a screwup somewhere
Do the same arithmetic rules apply to while do loops, or should I be using
while [ $s -gt $LT ] instead of (( s > LT ))

Thanks
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

Next

Return to User Questions

Who is online

Users browsing this forum: No registered users and 2 guests