Aboutmatt.net

Blog, projects and et cetera..

Removing Multiple Folders... Except One.

Reading time: about 1 minute

While doing a little work on my site this morning I needed to remove a bunch of folders from my Wordpress instance (I don’t like having all those themes kicking around needing updating).

I SSHed into my site and proceed to figure out how to go about it.

I landed a blogger site called whileonefork which had a handy little command:

1
rm -rf $(echo target/*/ | tr " " "\n" | grep -v keepme | tr "\n" " ")

Warning, never run a random command line command on your computer without understand it and what it is doing first! I recommend trying out commands in safe isolated environments… such as a VM, but at the very minimum in a test folder which approximates the one you want to mess with. I did this by creating a test folder on my PC, and create a batch of folders with the following command:

1
mkdir folder{1..3}

Pretty handy eh? On with the post.

I tweaked it to my needs:

1
rm -rf $(ls -d */ | tr " " "\n" | grep -v keepme | tr "\n" " ")

For more information on how this works, head on over to the blog post.