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
add comment

4 Answers 4

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
add comment

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
add comment

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
add comment

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
add comment

Your Answer

 
discard

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.