0

Say that under document root I have 3 apps; One is WordPress, second is Drupal, and third is Magento.

I wonder what's a good way to delete all cache directories of all CMSs under document root.

I could run a script with shell extensions for all (WP-CLI, Drush and Magento CLI), but what would be a good Bash-global way?

I thought of this to delete all cached files (${drt} = document root):

find ${drt}/*/cache/* -type f -exec rm -rf {} \;
  • I don't see anything wrong with your code but I recommend creating a copy of your document root (if it's not alread backed up) and trying it there first before you put it into production. – Nasir Riley Feb 4 '18 at 21:07
2

I would use a slightly different find command, myself:

find "${drt}" -path "*/cache/*" -type f -exec rm -f {} \;

... so that find does the work of recursing down the directory tree; your example has the bash command-line expanding all of the *'s, giving find multiple starting paths. Also note that you don't need to -r recursively remove files (you're explicitly asking for files with -type f). This generalized version of the find command will also "catch" files that are in directories such as ${drt}/wordpress/subdir/cache/directory/filename, where the cache directory is not one level underneath the document root. Whether that is something that you want or not depends on exactly where the cache directories are in your environment.

If you have GNU find (on e.g. Linux), you could simplify the removal with:

find "${drt}" -path "*/cache/*" -type f -delete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.