LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-24-2013, 10:18 PM   #1
rdibley
LQ Newbie
 
Registered: Feb 2006
Posts: 4

Rep: Reputation: 0
Automatic deletion of old files when disk is full


I am using the "motion" software to capture still images to a volume of limited size. The images are all roughly the same file size, and are in one directory. Since they will be triggered by movement in the camera view, the creation of the files will be random and sporadic.

I would like to set it up so that, when the volume is full, the oldest files will be deleted to make room for the new files. I was thinking of writing a script that would monitor the directory on a regular basis. When the amount of free space drops below 100MB, it would delete enough files to clear up 200MB of free space. (or something along those lines)

I figure that somebody has had a need to do something like this before, but I havenít been able to find anything as of yet. Any suggestions?

Thanks.
 
Old 06-25-2013, 12:35 AM   #2
vishesh
Member
 
Registered: Feb 2008
Distribution: Fedora,RHEL,Ubuntu
Posts: 658

Rep: Reputation: 66
You can set script for this.

Thanks
 
Old 06-25-2013, 01:25 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,247

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
You could use the logrotate tool, which can be used to manage files generally, or set a cron job to use the find cmd, after using eg df to check disk space.
These will be useful
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 06-25-2013, 10:02 AM   #4
nsingh63
LQ Newbie
 
Registered: Jun 2013
Posts: 8

Rep: Reputation: 0
Better u need to use the script to delet the old file by the find command with option mtime or atime and schedule it by cronjob to run as per frequency of the file .
 
Old 06-27-2013, 12:15 AM   #5
rdibley
LQ Newbie
 
Registered: Feb 2006
Posts: 4

Original Poster
Rep: Reputation: 0
I figured somebody would have done this before, but I guess not. So, I wrote a script to do the trick:

Code:
#!/bin/bash

# Immediately exit upon shell error
set -e

# Set lock file details
scriptname=$(basename $0)
lock="/var/run/${scriptname}"

# Lock the process, or exit if already running
exec 200>$lock
flock -n 200 || exit 1

# Write the PID to the lock file
pid=$$
echo $pid 1>&200

# Define the location of the applications
DF="/bin/df"
AWK="/usr/bin/awk"
SED="/bin/sed"
FIND="/usr/bin/find"

# Define the directory to check
DIR="/motion"

# Define constants
Minimum=100   # Minimum space below which files will be deleted (MB)
DeleteTo=200  # Delete files until this value is reached (MB)
NumtoDel=100  # Number of files to delete between each free disk space check

# Set shell such that empty file listing will return null
shopt -s nullglob

# Check if free space is less than the minimum specified
FreeSpace=$($DF -m $DIR | $AWK '{print $4}' | $SED "1d")
if [ $FreeSpace -lt $Minimum ]; then
  # Free space has dropped below minimum value, delete files until DeleteTo space is free
  while [ $FreeSpace -lt $DeleteTo ]; do
    # Check if any pictures remain in the directory, otherwise break out of the delete loop
    if test -z "$($FIND $DIR -maxdepth 1 -name '*.jpg' -print -quit)"; then
      break
    fi
    # Delete files until NumtoDel has been reached, or no more files exist
    FileCount=0;
    for FileName in $DIR/*.jpg; do
      rm $FileName
      FileCount=$((FileCount+1))
      # Check if number of files to delete has been reached
      if [ $FileCount -ge $NumtoDel ]; then
        break
      fi
    done
    # Check if free space is less than the minimum specified
    FreeSpace=$($DF -m $DIR | $AWK '{print $4}' | sed "1d")
  done
fi
Basically, you set the minimum free space available to the constant Minimum in the script. When less than this value is free, it will delete files in groups of NumtoDel until DeleteTo MB are free.

I set it to run every 5 minutes. Seems to work fine so far.

Thanks for the help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
no files on the disk but 'df -h' show it's full junust Linux - General 7 04-22-2010 05:16 AM
disk stats not updating after deletion of file.. disk full joecool118 Linux - General 2 04-10-2006 12:23 PM
Automatic file deletion Azhrarn Linux - Newbie 5 08-30-2005 05:01 AM
Another question on automatic file deletion Azhrarn Linux - Newbie 1 08-29-2005 11:18 AM
Automatic deletion of spam folder? RKris Linux - General 1 05-27-2003 08:21 AM


All times are GMT -5. The time now is 02:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration