Knowing how to use the command line is essential for anyone working with Linux systems management. Managing directories effectively is one of the core abilities that any Linux user should have. Managing directories efficiently requires an awareness of the subtleties of the ‘rm’ and ‘rmdir’ commands, which are commonly used to delete directories. The major way to save time is by adding a directory path in Linux that helps to run executables without establishing complete paths.

We will explore the nuances of deleting directories in Linux with these commands in this tutorial. After completing this course, you should be able to confidently explore directories and delete them, which will make Linux administration easier. If you use the GUI, removing a directory in Linux is a rather easy job. On the other hand, terminal commands can also be used to remove directories if you are not able to use the GUI.

Required Conditions

  • A device with a Linux distribution installed.
  • An account that has sudo access.
  • Ability to use the command line or terminal window.

In Linux, How can I Delete a Directory?

To delete a directory from the terminal window or command line, use one of two Linux commands:

  • Files and subdirectories are deleted in full from directories with the rm command.
  • Empty directories are removed with the rmdir command.

It is significant to remember that directories are permanently deleted by the rm and rmdir commands; they are not moved to the Trash directory. This means that these commands will not allow you to restore a deleted directory.

1. Use the rm Command in Linux to Remove a Directory

Linux’s rm command removes directories and files.

The syntax it uses is as follows:

rm [options] [file or directory name]

Among the various rm command options are:

  • -f: Makes all files and folders disappear by force.
  • -i: Requests confirmation before eliminating.
  • -I: Asks once before deleting recursively or more than three files.
  • -r: Eliminates folders and their contents in a recursive manner.
  • -d: Gets rid of pointless folders.
  • -v: Produces output that is verbose.
  • –help: Shows the text for assistance.
  • –version: Shows the version of the command.

An error message appears when you try to remove a directory using the rm command without any options:

displays error message when removing directory

To eliminate an empty directory, modify the rm command by adding the -d flag:

rm -d Example

The following example demonstrates how to remove the Example directory using the rm command and the -d flag:

removing emprty directory using rm command

To remove a directory that has files and subdirectories, use the -r flag.

The Example directory’s tree hierarchy, which includes the Dir1 and Dir2 subdirectories with several text files each, is depicted in the image below:

example of directory and file hierarchy

The -v flag produces an output listing each stage of the process, but the -r flag deletes the entire directory, including files and subdirectories:

rm -r -v Example

removing multiple directories,files

When using the -i option, a prompt asking to confirm the removal of a directory appears. Enter to confirm by pressing Y.

rm -d -i Example

displaying when removing directory

When deleting a write-protected directory, human involvement is required. Make a directory of this kind using:

sudo mkdir Example

Use this to delete the directory:

rm -d Example

write protected directory

Press Enter after typing Y to confirm deletion. Use the -f flag or increase the command privileges to sudo to bypass the confirmation:

rm -d -f Example
sudo rm -d Example

Use the following command if there are other files and directories in the write-protected directory:

rm -rf <directory name>

2. Use the rmdir Command to Remove a Directory in Linux

Only empty directories are deleted by the Linux rmdir command. The syntax used by the command is as follows:

rmdir [options] [directory name]

The following arguments are available with the rmdir command:

  • –ignore-fail-on-non-empty: When attempting to remove a non-empty directory, this option suppresses the error message.
  • -p: Removes the directory from the hierarchy together with its parent.
  • -v: Produces output that is verbose.
  • –help: Shows the help screen.
  • –version: Shows the version of the command.

An error occurs when the rmdir command is used on a directory that isn’t empty:

rmdir command on non empty directory

In this instance, the Test subfolder is located in the Example directory:

showing example of hierarchy directory

Add these directories in the opposite hierarchy order to remove them with the rmdir command. Each stage of the process is listed in the output when the -v option is used:

rmdir -v Example/Test Example

removing multiple directories using rmdir command

Using the -p option and the name of the subfolder is a more straightforward way to accomplish this. By doing this, the subfolder and its parent directory are eliminated:

rmdir -p -v Example/Test

removing subdirectory and its parent using rmdir command

Using wildcards, the rmdir command lets you remove several folders with similar names. To remove the directories Example1, Example2, and Example3, for example:

rmdir -v Example*

rmdir command allows to remove multiple directories

In Summary of Remove a Directory in Linux

Ultimately, the commands ‘rm’ and ‘rmdir’ are essential resources for Linux directory management. These commands provide the flexibility and efficiency needed for efficient system administration, whether you need to remove a directory or its contents all at once. To prevent inadvertent data loss, it is imperative to use caution while utilizing the ‘rm’ command, particularly when the ‘-r’ flag is included.

You’ll be well-equipped to traverse and manage directories in your Linux system with ease if you learn these commands and comprehend their subtleties, which will promote a more effective and ordered computing environment. You should be able to remove folders in Linux using the terminal window or command line after reading this article.