• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Solution / Command Line Delete All Directories Except One

Command Line Delete All Directories Except One

Last updated on June 18, 2018 by Sal Ferrarello

On the Mac OS X bash command line, you can delete all directories, with the exception of one, in the following way.

If our current directory has sub-directories and we want to delete all of them except, ./really-important-do-not-delete, we can run

$ shopt -s extglob
$ rm -rf !(really-important-do-not-delete)
$ ls
really-important-do-not-delete
$

original source

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Computing, Dev Tips, Draft, Solution Tagged With: command line

Reader Interactions

Leave a Reply Cancel reply

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

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in