Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 06-24-2013, 10:18 PM   #1
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?

Old 06-25-2013, 12:35 AM   #2
Registered: Feb 2008
Distribution: Fedora,RHEL,Ubuntu
Posts: 661

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

Old 06-25-2013, 01:25 AM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,395

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
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
Old 06-25-2013, 10:02 AM   #4
LQ Newbie
Registered: Jun 2013
Posts: 10
Blog Entries: 2

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
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:


# Immediately exit upon shell error
set -e

# Set lock file details
scriptname=$(basename $0)

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

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

# Define the location of the applications

# Define the directory to check

# 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
    # Delete files until NumtoDel has been reached, or no more files exist
    for FileName in $DIR/*.jpg; do
      rm $FileName
      # Check if number of files to delete has been reached
      if [ $FileCount -ge $NumtoDel ]; then
    # Check if free space is less than the minimum specified
    FreeSpace=$($DF -m $DIR | $AWK '{print $4}' | sed "1d")
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.


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 > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:25 AM.

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