Script to delete old files

I recently did something like a private file sharing, and to not control what users are uploading, added a script in cron that files that were deleted automatically for more than 3 days.

An example of a command to delete files with more than N days:

find /var/www/share/ -type f -mtime +N -exec rm {} \;

If you need to delete the directories and all that is in them, then this command:

find /var/www/share/ -type d -mtime +N -exec rm -rfv {} \;

Instead of deleting, you can check what data is deleted by the command:

find /var/www/share/ -type d -mtime +N -print

Write a command to a file and put it in the desired directory, for example /home/user/share.sh
We add a link to this file in /etc/crontab. The scheduler will execute it every day at 4 am on behalf of the user “www-data” and thus will delete files that are stored longer N days:

0 4 * * * www-data /home/user/share.sh > /dev/null 2>&1
Подписаться на IT Blog (RU) по Email
Subscribe to IT Blog (EN) by Email

Did my article help you? How about buying me a cup of coffee as an encouragement? Buy me a coffe.

Leave a Reply