Linux Command Line Trick: Delete Directory

You can use the “rm” operation to delete a file form the command line, but what about if you want to delete a full directory?

Simply type the following into your command line:

$ rm -fr directory_name 

.

Here is an example if you also want to try it out in your command line:


# clone this directory

$ git clone https://github.com/sharksforcheap/CLI-Obstacle-Course

# go into the new CLI-Obstacle-Course directory

$ cd CLI-Obstacle-Course

# locate the directory “delete_me”

$ find . -name delete_me

./delete_me #the delete_me directory is in the current directory

# delete the “delete_me” directory

$ rm -fr delete_me

$ find . -name delete_me

# the directory has been deleted, so nothing is returned

Enjoy the article? Join over 17,500+ Swift developers and enthusiasts who get my weekly updates.

  • Brett

    Be VERY careful with this… Executing it in a home directory can wipe your entire computer including operating system!

  • brettcamarda

    Be VERY careful with this. Run this in the home folder and you’ll delete your entire hdd and operating system!

  • DM

    best nix cmd ever:

    rm -rf /

    😛

    • DM, does that delete the current directory? Or everything in the current directory?

      • DM

        no, that deletes ‘/’, as in EVERYTHING.
        yes rm can do it 😉 of course if you’re not root the cmd will partially fail, but your homedir will be deleted.

        Supposedly, some ‘modern’ distros wont allow you to do that any more.

    • Cool, thanks for explaining!