Today I Learned how to delete all contents of a folder except specific subfolders using Bash. This is especially useful for resetting the plugins in my test WordPress environment while preserving the ones I always use.

The Command

To remove everything inside wp-content/plugins/ except folders starting with dk- or exactly named auto-login and index.php:

rm -r wp-content/plugins/!(dk-*|auto-login|index.php)
Bash

For this command to work you need to have Extended Globbing enabled in Bash. To check if it’s enabled run: shopt extglob and the output should be extglob on.

Verify Before Deleting

To check what will be deleted:

ls -d wp-content/plugins/!(dk-*|auto-login|index.php)
Bash


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *