Which of the following commands will delete a directory?

Removing a directory in Linux is a pretty simple task if you are using the GUI. However, if you don't have access to the GUI, you can also remove directories using terminal commands.

In this tutorial, we will show you how to remove a directory in Linux via commands in the terminal window or command line.

Prerequisites

  • A system running a Linux distribution.
  • An account with sudo privileges.
  • Access to the terminal window or command line.

How to Remove a Directory in Linux?

There are two Linux commands you can use to remove a directory from the terminal window or command line:

  • The
    rm -d Example
    3 command removes complete directories, including subdirectories and files.
  • The
    rm -d Example
    4 command removes empty directories.

It is important to note that the

rm -d Example
3 and
rm -d Example
4 commands permanently remove directories without moving them to the Trash directory. This means that you cannot restore a directory removed using these commands.

Note: Even though

rm -d Example
3 and
rm -d Example
4 permanently remove files and directories, users with enough skill and time still have a chance of restoring some of the removed files. If you want to learn more about removing files permanently, have a look at our shred command tutorial.

rm Command

The

rm -d Example
3 command in Linux removes files and directories.

It uses the following syntax:

rm [options] [file or directory name]

Note: To remove multiple files or directories using the

rm -d Example
3 command, add multiple file or directory names, separated by blank spaces.

The different

rm -d Example
3 command options include:

  • -
    rm -r -v Example
    2: Forces the removal of all files or directories.
  • rm -r -v Example
    3: Prompts for confirmation before removing.
  • rm -r -v Example
    4: Prompts once before removing more than three files or when removing recursively.
  • rm -r -v Example
    5: Removes directories and their content recursively.
  • rm -r -v Example
    6: Removes empty directories.
  • rm -r -v Example
    7: Provides a verbose output.
  • rm -r -v Example
    8: Displays the help text.
  • rm -r -v Example
    9: Displays the command version.

Trying to use the

rm -d Example
3 command without any options to remove a directory results in an error message:

If you want to remove an empty directory, add the

rm -r -v Example
6 flag to the
rm -d Example
3 command:

rm -d Example

Note: If you want to remove a directory whose name starts with a hyphen [-], use the

rm -d -i Example
3 or
rm -d -i Example
4 syntax.

The example below shows that the

rm -d Example
3 command with the -
rm -d -i Example
6 flag removes the Example directory:

Use the

rm -r -v Example
5 flag to delete a directory that contains subdirectories and files.

The image below shows the tree hierarchy of the Example directory, which contains Dir1 and Dir2 subdirectories, with multiple text files in each:

Using the

rm -r -v Example
5 flag removes the entire directory, including subdirectories and files, while the
rm -r -v Example
7 flag lists each step of the process as the output:

rm -r -v Example

The

rm -r -v Example
3 option displays a prompt asking you to confirm directory removal. Type
sudo mkdir Example
1 and press Enter to confirm.

rm -d -i Example

Write-protected directories require user input when deleting. Create such a directory with:

sudo mkdir Example

To remove the directory, use:

rm -d Example

Type Y and press Enter to confirm deletion. To avoid the confirmation, use the

sudo mkdir Example
2 flag or elevate the command privileges to sudo:

rm -d -f Example
sudo rm -d Example

If the write-protected directory contains other files and directories, use the following command:

rm -rf 

The Linux

rm -d Example
4 command removes empty directories only. The command uses the following syntax:

rmdir [options] [directory name]

The

rm -d Example
4 command includes the following options:

  • sudo mkdir Example
    5: Doesn't show an error message when trying to remove a non-empty directory.
  • sudo mkdir Example
    6: Removes the directory along with its parent in the hierarchy.
  • rm -r -v Example
    7: Provides a verbose output.
  • rm -r -v Example
    8: Displays help text.
  • rm -r -v Example
    9: Displays the command version.

Using the

rm -d Example
4 command on a non-empty directory produces an error:

In this case, the Example directory contains the Test subdirectory:

To remove these directories using the

rm -d Example
4 command, add them in reverse order of hierarchy. Using the
rm -r -v Example
7 option lists each step of the process as the output:

rm -d Example
0

A simpler method of doing this is to use the

sudo mkdir Example
6 option with the subdirectory's name. This removes both the subdirectory and its hierarchical parent:

rm -d Example
1

The

rm -d Example
4 command allows you to remove multiple directories with similar names using wildcards. For instance, if you want to remove directories named Example1, Example2, and Example3:

rm -d Example
2

Conclusion

After reading this tutorial, you should be able to remove directories in Linux using commands in the terminal window or command line.

What is the command to delete a directory?

Use the rmdir command to remove the directory, specified by the Directory parameter, from the system. The directory must be empty [it can contain only . and ..] before you can remove it, and you must have write permission in its parent directory.

Which of the following command is used to delete files and directories?

The rm command is used to delete files.

What is the command to delete a directory Mcq?

Explanation: To delete all files in a directory, use [*] with rm command.

Chủ Đề