I’ve shared how to remove all files in a directory except specific subfolders in Bash, but unfortunately, this command doesn’t work for Zsh. After some tweaking, I was able to reproduce the same behavior, which is to remove everything inside wp-content/plugins/, except folders starting with dk- or exactly named auto-login, and index.php:

find wp-content/plugins/ -mindepth 1 -maxdepth 1 -type d ! -name 'dk-*' ! -name 'auto-login' ! -name 'index.php' -exec rm -rf {} +
Bash

Dry run

Here’s the dry run version that will only list what would be deleted:

find wp-content/plugins/ -mindepth 1 -maxdepth 1 -type d ! -name 'dk-*' ! -name 'auto-login' ! -name 'index.php' -print
Bash

Confirm Before Deleting

You can also manually confirm the deletion of each item by adding the -ok parameter:

find wp-content/plugins/ -mindepth 1 -maxdepth 1 -type d ! -name 'dk-*' ! -name 'auto-login' ! -name 'index.php' -ok rm -rf {} \;
Bash

Move to Trash

A safer approach would be to move the files to the trash folder instead of deleting them permanently. Here is how to do this.

Linux

I’m using trash-cli tool for that.

find wp-content/plugins/ -mindepth 1 -maxdepth 1 -type d ! -name 'dk-*' ! -name 'auto-login' ! -name 'index.php' -exec trash-put {} +;
Bash

macOS

find wp-content/plugins/ -mindepth 1 -maxdepth 1 -type d ! -name 'dk-*' ! -name 'auto-login' ! -name 'index.php' -exec trash {} +;
Bash

🤖 Artificial Intelligence Usage Disclosure 🤖

  • 📝 AI used for: text correction;
  • 🧠 Model: ChatGPT 4.1-nano;
  • 🧐 Human Reviewed: Yes.


Comments

Leave a Reply

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