How to Delete a Directory in Linux Using the rm Command

When you are organizing files on a Linux server via the terminal, creating directories is easy. However, deleting them can be surprisingly stubborn. If you try to use the standard rmdir (remove directory) command on a folder that contains even a single hidden file, Linux will refuse to delete it, throwing a “Directory not empty” error to protect your data. To force Linux to delete a folder and everything inside it, you must use the powerful rm command with specific recursive flags.

The Recursive Delete Command

The standard rm command is designed to delete individual files, not folders. To make it delete a folder, you must add the -r (recursive) flag. This tells Linux to dive into the folder, delete all the files inside it, delete any sub-folders inside it, and finally, delete the main folder itself.

To delete a folder named “old_backups”, open your terminal and type:

rm -r old_backups

Press Enter. If you own all the files inside, the folder will vanish instantly without any confirmation prompt.

Dealing with Stubborn and Write-Protected Files

Sometimes, if a folder contains write-protected files or files owned by another process, the rm -r command will pause and ask you to confirm the deletion of every single file individually. If the folder contains thousands of files, this is impossible.

To bypass all confirmation prompts and force the deletion, you must combine the recursive flag (-r) with the force flag (-f). This creates the infamous and highly dangerous rm -rf command.

rm -rf old_backups

CRITICAL WARNING: The rm -rf command is the most dangerous command in Linux. It does exactly what it is told, instantly, with zero warnings, and it does not use a Recycle Bin. Once you press Enter, the data is permanently destroyed and cannot be recovered. If you accidentally type a space in the wrong place (e.g., rm -rf / old_backups instead of rm -rf /old_backups), you will instruct Linux to recursively delete the entire root hard drive (/), instantly destroying the entire server.

Always double-check your spelling and path before pressing Enter when using the -f flag.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.