Login

How to Delete a Non-Empty Directory Using the rm Command in Linux

If you are managing a Linux system, you may sometimes need to delete folders that contain files and subdirectories. The rm command is the simplest and most powerful way to remove both empty and non-empty directories from the command line.

This guide explains how to safely delete non-empty directories using the rm command and its different options.

Basic Syntax of the rm Command

The general syntax of the rm command is:

rm [option] FileOrFolderName

By default, the rm command is used to delete files. However, with the right options, you can also remove directories and their contents.

Delete a Non-Empty Directory (Recursive Deletion)

To delete a directory along with all its files and subdirectories, use the -r (recursive) option:

rm -r Simple-Directory

This command removes the directory and everything inside it.

⚠️ Caution:
The rm -r command permanently deletes all files and subdirectories within the specified directory. Ensure you have a backup if you need to restore data later.

Delete a Non-Empty Directory Without Confirmation

When you try to delete a write-protected directory, Linux will ask for confirmation before removing each file.
To skip these prompts and force delete the directory, use the -rf option:

rm -rf Simple-Directory

Here:

  • -r removes directories and their contents recursively.
  • -f forces deletion without any confirmation.

This command is powerful, so use it with care—especially when logged in as the root user.

Delete an Empty Directory Using rm

You can also delete empty directories using the -d option:

rm -d Empty-Directory

This works similarly to the rmdir command, but gives you more flexibility since rm can handle both empty and non-empty directories.

Delete Multiple Directories at Once

The rm command can remove multiple directories in a single command.
Simply list all the directory names separated by spaces:

rm -r Directory_1 Directory_2 Directory_3

This will recursively delete all the listed directories and their contents.

Quick Summary

Action Command Description
Delete non-empty directory rm -r directory_name Removes directory and all contents
Force delete without confirmation rm -rf directory_name Deletes everything without prompts
Delete empty directory rm -d directory_name Removes only empty folders
Delete multiple directories rm -r dir1 dir2 dir3 Deletes several folders at once

Pro Tip

Always double-check your directory path before running the rm -rf command. A small typo can lead to loss of important files. If you want to test before deleting, use the ls command to list the contents first.

Need Help Managing Your Linux Server?

If you are managing Linux servers and want a secure and reliable hosting environment, Host.co.in offers Linux VPS Hosting and Dedicated Servers with 24/7 expert technical support, 99.95% uptime, and Tier IV Indian data centres.

Explore Linux VPS Hosting Plans

Sarang Khedkar

How to Delete a Non-Empty Directory Using the rm Command in Linux
Table of Contents
    ×