2.12.9. Automatic catalog cleanup

Important points:

  • Warning! Do not specify a path without ~/ at the beginning or in the form of /*, as this will delete absolutely all files of all sites on the hosting account.
  • To disable recursion in the rm utility, do not specify the -r key; leave only -f.
  • To disable recursion when using the find utility, specify the -maxdepth 1 key directly after specifying the path.

To automatically clean up the directory, you can create a cron job with commands to delete files:

  • Delete all files and subdirectories in a specific directory:
    /bin/rm -rf ~/example.com/www/tmp/*

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

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

    Instead of .tmp, specify the desired file extension to be deleted.

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

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

  • Delete all files except one:
    /bin/find ~/example.com/www/tmp/ -type f ! -name 'index.php' -delete

    To delete all files except those with a specific extension, replace index.php with *.php. In this case, all files except those with the extension .php will be deleted from the directory.

Content