Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a IP cam which save its recordings in a specific directory named Camera1 in my Ubuntu Server 12.04.

I would like to limit the size of this folder to 5 gigs, by deleting -say once a day- the oldest files.

I first checked the quota program but it doesn't seem to allow the creation of new files and deleting of the old ones.

So I think the best workaround would be to run a bash script ? But I have no idea how to write it...

Thank you guys !

share|improve this question

5 Answers 5

up vote 3 down vote accepted

The find command can be used to find files and delete them. The following command, for example, will delete files created more than seven days ago:

find /path/Camera1 -ctime +7 -delete 

Use crontab if you want to schedule it; there is more information here.

share|improve this answer
Thank you both for your answers. It doesn't fit exactly my needs but it's light and will do the trick 99% anyway! –  Sulliwane Aug 17 '12 at 15:03

You could write a little bash script that just has a find command like

find /Camera1 -atime +7 -exec rm {} \;  

This would remove all files that haven't been accessed in over 7 days from the folder /Camera1.
Obviously this isn't quite what you wanted, but I hope it helps.

share|improve this answer

I started thinking about how hard it would be to only keep a certain amount of files. I turned to awk, which I have not used for a while, and came up with the following one liner.

cd /path/to/Camera1 && ls -ltc | awk '{ if (!system("test -f " $9)) { size += $5; if (size > 5*2^30 ) system("rm " $9) } }'

  1. change to directory in question
  2. list files, newest first
  3. Run awk on output, check that it is a regular file, add file size to counter, remove file if cumulative size over 5 gigs

You can change "rm " to "ls " to have it list the files it would remove. It would be insane not to carefully test a script, suggested by an unknown on the net, which removes files!

The script will probably break and/or not do what you excpect if you have funny characters (space for example) in the filenames.

share|improve this answer

Excellent solution with awk!

I would however take an additional step to stop your trashcan overflowing.

In my cron..

wipes old cam shots if over 500mb

*/2 * * * * cd /home/me/Desktop/Dropbox/a_security_cam && ls -ltc | awk '{ if (!system("test -f " $9)) { size += $0.5; if (size > 0.5*2^30 ) system("rm " $9) } }'

empties trash oldest first over 2gb

*/10 * * * * autotrash –min-free 2048

See http://www.logfish.net/pr/autotrash/

share|improve this answer

I was in a very similar situation. I have a big LVM partition holding 2 directories:

  • Videos (live streams recorded) can use 88% of the partition

  • Pictures (pictures sent by the cameras when detecting motion) can use 7% of the partition (as pictures are much lighter, I can still keep older pictures than videos)

The remaining 5% is a security margin so that the partition never gets full. Note I am talking in percentages instead of fixed gigabytes because the size of my LVM partition changes if I replace or add a hard drive.

So here is the script (I added many comments so it's easily understandable):

#Usage = sh limit-directory-size.sh /media/computer/mypartition 88 120 ("/media/computer/mypartition" = the directory to be limited / "90"=the percentage of the total partition this directory is allowed to use / "120"=the number of files to be deleted every time the script loops (while $Directory_Percentage > $Max_Directory_Percentage)

#Directory to limit
echo "Directory to limit="$Watched_Directory

#Percentage of partition this directory is allowed to use
echo "Percentage of partition this directory is allowed to use="$Max_Directory_Percentage

#Current size of this directory
Directory_Size=$( du -sk "$Watched_Directory" | cut -f1 )
echo "Current size of this directory="$Directory_Size

#Total space of the partition = Used+Available
Disk_Size=$(( $(df $Watched_Directory | tail -n 1 | awk '{print $3}')+$(df $Watched_Directory | tail -n 1 | awk '{print $4}') ))       
echo "Total space of the partition="$Disk_Size

#Curent percentage used by the directory
Directory_Percentage=$(echo "scale=2;100*$Directory_Size/$Disk_Size+0.5" | bc | awk '{printf("%d\n",$1 + 0.5)}')
echo "Curent percentage used by the directory="$Directory_Percentage

#number of files to be deleted every time the script loops (can be set to "1" if you want to be very accurate but the script is slower)
echo "number of files to be deleted every time the script loops="$Number_Files_Deleted_Each_Loop

#While the current percentage is higher than allowed percentage, we delete the oldest files
while [ $Directory_Percentage -gt $Max_Directory_Percentage ] ; do
    #we delete the files
    find $Watched_Directory -type f -printf "%T@ %p\n" | sort -nr | tail -$Number_Files_Deleted_Each_Loop | cut -d' ' -f 2- | xargs rm
    #we delete the empty directories
    find $Watched_Directory -type d -empty -delete

Then you call it every 15 minutes with a crontab entry

*/15 * * * * sh /home/computer/limit-directory-size.sh /media/computer/mypartition/videos 88 120

Hope it helps!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.