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 {} +
BashDry 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
BashConfirm 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 {} \;
BashMove 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 {} +;
BashmacOS
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.
Leave a Reply