Deleting a directory is a task with a couple of important variations. Most importantly, we may want to delete a directory only if it is empty, so that the operation should fail (without deleting anything) if the specified directory contains files or subdirectories; alternatively, we may want to delete a directory along with all of its contents, even if those contents are themselves directories that contain files or subdirectories. When deleting a non-empty directory, we may want the shell to provide a confirmation prompt as a final check before deletion; or, if we’re sure we know what we’re doing, or if we’re deleting a directory from a batch or script file, we generally don’t want a confirmation prompt.

The syntax descriptions and examples below focus on the above variations; the Bash shell provides a few more, but we won’t go into those here.

Commands

Windows Command Prompt

Syntax

rmdir [/S] [/Q] {path}
rd [/S] [/Q] {path}

Removes the directory specified by path on the specified drive. path is interpreted in accordance with “Specifying file and directory paths”.

Options

/S
Remove the directory and all of its contents, including subdirectories. If this option is not specified, and the directory is not empty, the operation will fail.
/Q
Quiet mode—i.e. no confirmation prompt will be presented to the user.

Examples

rmdir output

Removes the output subdirectory of the current working directory, as long as output is empty.

rmdir /S /Q ..\bin

Removes the bin subdirectory of the parent of the current working directory, along with any files or subdirectories in bin, without displaying any confirmation prompts.

Bash shell (including OS X & Ubuntu Terminal, Git Bash, etc.)

Syntax for removing an empty directory

rmdir [options] {path …}

Removes one or more specified directories, but only if those directories are empty. However, the --parents (or -p) option can be used to remove a simple hierarchy of directories, as long as each of the directories has no file contents, and no additional subdirectories other than the subdirectory specified in the command. path is interpreted in accordance with “Specifying file and directory paths”.

Syntax for removing a directory, whether empty or not

rm [options] -R {path …}

Removes one or more specified directories (without the -R or --recursive option, this command is used to delete files), recursively removing any subdirectories and files contained in those directories. The --force (or -f) option can be specified to prevent any confirmation prompts from being displayed. path is interpreted in accordance with “Specifying file and directory paths”.

Examples

rmdir ~/Downloads/work

Removes the work subdirectory of the Downloads subdirectory of the current user’s home directory, if work is empty.

rmdir -p temp/src/java

Removes the temp/src/java directory, relative to the current working directory, as long as that directory is empty. Then, if the temp/src directory is empty, it is removed. Finally, if temp is empty, it is removed.

rm -fR temp

Removes the temp subdirectory of the current working directory, along with any files or subdirectories it contains, without displaying any confirmation prompts.

Tasks

  1. Use the appropriate command to remove the test1 subdirectory that you created previously in the projects subdirectory of the bootcamp directory.

  2. With a single command, remove the test2/test3 and test2 subdirectories that you created previously in the projects subdirectory of the bootcamp directory.