rmdir command in linux

Learn via video courses
Topics Covered

Overview

The rmdir command in Linux is used to remove directories in Linux. It is an easy command that, if necessary, can remove directories sequentially.

It should be used with caution, though, because it can erase entire folders and their contents.

Introduction

The rmdir command in Linux is the command used to remove or delete empty directories. It is a simple and easy-to-understand command that only removes empty directories and does not work on directories containing files or other directories. The command is useful for managing directory structures and cleaning up the file system by removing unnecessary or unused directories.

It is often used in shell scripts and other automated tasks to clean up directories and ensure efficient use of system resources. Overall, the rmdir command is an essential tool for managing directory structures in Linux systems.

Linux rmdir Command Syntax

The basic syntax of the rmdir command in Linux is as follows:

In this syntax, OPTION is an optional parameter that can be used to specify additional functionality for the command. DIRECTORY is the directory that you want to remove.

rmdir Options

There are several options available with the rmdir command in Linux. Here are a few of the most commonly used options:

-p: This option removes the DIRECTORY and any empty parent directories.

-v: This option displays a message for each removed directory.

--ignore-fail-on-non-empty: This option suppresses error messages if a directory is not empty.

--help: This option displays the help message for the rmdir command.

functionality of the option help in the Terminal

How to Use rmdir Command in Linux

Here are some examples of how to use the rmdir command in Linux:

First, delete the child directory and then the parent directory:

To remove a child directory and then remove the parent directory, use the following command:

directory in the Terminal

This command will remove the child directory first and then remove the parent directory if it is empty.

Remove the directories if they are empty:

To remove a directory if it is empty, use the following command:

deleting empty Directory in the Terminal

This command will remove the directory if it is empty.

Use the Verbose Option to Display a Message for Each Directory that is Removed:

To use the verbose option to display a message for each directory that is removed, use the following command:

the option of verbose option in the Terminal This command will display a message for each directory that is removed.

Conclusion

  • The rmdir command is used to remove directories in Linux.

  • It can be used to remove directories that are empty or recursively` remove directories that are not empty.

  • The syntax of the rmdir command is simple to understand and includes an optional options parameter.

  • The most commonly used options with the rmdir command are -p, -v, -ignore-fail-on-non-empty, and --help.

  • The rmdir command can be used to remove child and parent directories, remove empty directories, and display a message for each directory that is removed.

  • Since it may erase directories and all of their contents, use it cautiously.

MCQs

A: What is the rmdir command used for in Linux?

i) To remove files

ii) To remove empty directories

iii) To copy directories

iv) To move directories

Answer: ii) To remove empty directories

B: Which option is used with the rmdir command to remove the directory and any empty parent directories?

i) -v

ii) --help

iii) -p

iv) --ignore-fail-on-non-empty

Answer: iii) -p

C: What happens when the rmdir command is used on a non-empty directory in Linux?

i) The directory and all its contents are deleted

ii) An error message is displayed

iii) The directory is deleted but its contents are not

iv) The user is prompted to confirm the deletion

Answer: ii) An error message is displayed.