Deleting Folders

Deleting folders differs from deleting files because there may be any number of files inside the directory. This video shows how to delete folders whether or not they contain other folders and files.

Deleting Empty Folders with rmdir

Deleting directories is also a little different from the way you delete files. a command like "rm " won't work. To demonstrate, I'll try to delete the "AlsoImportant" directory with the "rm" command.

The command I'll try is this: "rm AlsoImportant/"

When I press Enter, I get the message "rm: AlsoImportant/: is a directory". This doesn't actually tell you that it failed, but if we type "ls" we'll see that the directory is still there.

There is a command "rmdir" (for remove directory) that is designed to remove (or delete) directories. This however, will only work if the directory is empty. So, if I try to delete the "AlsoImportant" directory with "rmdir" I'll get an error.

The command I'll try is this: "rmdir AlsoImportant/"

This time, the message we get is "rmdir: AlsoImportant/: Directory not empty" which at least tells us that the directory is not empty.

The "TextFiles" directory is empty, so if I try to delete it with the "rmdir" command it will work.

The command I'll use is this: "rmdir TextFiles/"

This time, when I press Enter, the directory is deleted, and we can verify that it's gone with the "ls" command.

Okay, so you can delete directories with the rmdir command, but only if they're empty. That could get real tedious if you have multiple levels of directories with multiple files in each. Luckily, there's another option.

Deleting a Folder and all its contents with rm -rf

We saw that the "rm" command didn't work on directories. In its basic form, that's true, but we can add a couple of options to the "rm" command to make it work the way we'd like. This is how I personally delete directories, because it will delete them whether or not they are empty.

The way we can make the "rm" command work on directories, is to add the "-r" option, which stands for "Recursive", or "this directory and everything inside it as well." I'll use it to delete the "AlsoImportant" directory.

The command I'll use is this: "rm -r AlsoImportant/"

Now, if we use "ls" the "AlsoImportant" directory is gone. I should note that some installations default to using the "interactive" option when using the "rm -r" command. To show you what that is like, I'll add the "-i" (for interactive) option to "rm -r"

First, I'll recreate the AlsoImportant directory with the command we used in an earlier section: "cp -r VeryImportant/ AlsoImportant". Now, I'll delete it with the "-i" option.

The command I'll use is this: "rm -ri AlsoImportant/"

This time, we are given some prompts. The first asks us if we want to examine (or include) the contents of the "AlsoImportant" directory. I'll type "y" and press Enter to answer yes. Then I'm asked if I want to remove the ".DS_Store" file, (you'll be asked this for each file in the directory) I'll type "y" again. Then we're asked if we want to include the contents of the "OtherThings" directory that's inside the "AlsoImportant" directory. Again, I'll type "y".

Since there are no files in that directory, I'm now asked if I want to delete the folder. I'll type "y", and now that the "AlsoImportant" directory is empty, I'm asked if I want to delete it. I'll answer "y" one more time.

Okay, now everything should be gone, and using "ls" confirms that the "AlsoImportant" directory is in fact deleted.

This can be not quite, but almost, as tedious as deleting each file individually before using the "rm" command alone. So, if your installation does default to deleting files this way, or you just want to make sure everything is deleted every time, you can add the "-f" option (which stands for force) to force delete everything without prompts.

Again, I'll recreate the "AlsoImortant" directory with the command: "cp -r VeryImportant/ AlsoImportant". (Side tip: you can go back through your previous commands by pressing the "up" arrow) Now, I'll delete it, and all its contents, with the "-f" option.

The command I'll use is this: "rm -rf AlsoImportant/"

This time, when I press "Enter", there are no prompts, and when we type "ls" we'll see that the directory has been deleted.

_This has been a basic overview of the "rm" command. For more information, see this article on computerhope.com