2.11.9. Automatic directory cleaning

To automatically clean up the directory, you need create cron task and specify the command to delete files.

Important points:

  • Warning! Do not under any circumstances indicate the path without ~/ at the beginning or in the form /*, since absolutely all files of all sites in one hosting account will be subject to deletion.
  • To disable recursiveness in the utility rm do not specify the key -r, leave only -f.
  • To disable recursiveness when using the utility find immediately after specifying the path, specify the key -maxdepth 1.
  • For removing all files and subdirectories in a specific directory, use the command:
    /bin/rm -rf ~/example.com/www/tmp/* 

    Instead example.com/www/tmp/ specify the full path to the directory whose contents you want to delete.

  • For removing all files with a specific extension in the directory and all its subdirectories use the command:
     /bin/rm -rf ~/example.com/www/tmp/*.tmp

    Instead .tmp specify the desired file extension to be removed.

  • For removing all files older than a certain number of days in a specific directory, use the command:
    /bin/find ~/example.com/www/tmp/ -type f -mtime +30 -exec rm -rf {} \;

    Instead 30 specify the number of days that the file should be kept.

  • For removing all files except one, use the command:
     /bin/find ~/example.com/www/tmp/ -type f ! -name 'index.php' -delete 

    To delete all files except files with a specific extension, instead of index.php indicate *.php... In this case, all files in the directory will be deleted, except for files with the extension .php.