Ubuntu – Limit the size of a directory by deleting old files


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 method would be to run a bash script…

Best Answer

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.

Related Question