Host Your Own AI Agent with OpenClaw - Free 1-Click Setup!

Delete Directories in Linux: rm and rmdir Explained

Two commands do the heavy lifting when you need to remove a directory in Linux: rm and rmdir. They look similar at first glance, but they behave very differently. Getting the wrong one on the wrong directory can delete a lot more than you intended - and Linux doesn't have a recycle bin.

This guide covers both commands, every useful option, and the situations where each one belongs.

Linux rm and rmdir: Command Overview

Before running any deletion command, make sure you know where you are in the filesystem. The pwd command shows your current working directory; ls lists what's in it. Both are worth running before you delete anything.

Here's the quick reference for which command does what:

rm options at a glance:

  • rm -d - remove an empty directory
  • rm -r - recursively remove a directory and all its contents
  • rm -f - skip confirmation prompts when deleting write-protected files
  • rm -rf - recursive deletion, no prompts, write-protected or not
  • rm -i - prompt before every single deletion
  • rm -I - single prompt if more than 3 files are being deleted
  • rm * - wildcard matching multiple characters
  • rm ? - wildcard matching exactly one character

rmdir options:

  • rmdir -p - remove an empty subdirectory and its parent
  • rmdir -v - print confirmation that the directory was removed

The rule of thumb: use rmdir when you're sure the directory is empty and want protection against accidental deletion of files. Use rm -r when you need to delete directory contents as well.

Before running any deletion command in Linux, always have a current backup. These commands are permanent. There's no undo.

Delete Empty Directories with rmdir

rmdir is the safer choice for removing empty directories. It simply refuses to work on a directory that still contains anything - files, subdirectories, symlinks - and returns this error:

rmdir: failed to remove 'Directory': Directory not empty

That behavior is intentional. It prevents you from accidentally nuking a directory tree when you thought it was already clean.

Basic syntax:

rmdir DirectoryName

To remove multiple empty directories in one command, list them as separate arguments:

rmdir Directory_1 Directory_2 Directory_3

Remove a Subdirectory and Its Parent

The -p option removes a subdirectory and walks up the path, deleting each parent directory if it's also empty after the child is gone:

rmdir -p /Directory/SubDirectory

This removes SubDirectory first, then Directory - but only if Directory contains nothing else at that point.

Confirm Deletion with -v

The -v flag prints a message for each directory removed. Useful when removing multiple directories and you want to verify what actually got deleted:

rmdir -v Simple-Directory

rmdir: removing directory, 'Simple-Directory'

Remove Non-Empty Directories with rm -r

rm is a file deletion command, but combined with -r (recursive), it becomes the standard tool for deleting directory trees. The recursive flag tells it to descend into subdirectories and remove everything - files, folders, the lot.

Basic recursive removal:

rm -r Simple-Directory

Warning: This deletes the directory and everything inside it permanently. Recovery requires a backup.

Skip Write-Protection Prompts with -rf

When a directory or its contents are write-protected, rm will pause and ask for confirmation before each file. If you're confident about what you're deleting, -rf skips all prompts:

rm -rf Simple-Directory

Use this carefully. The combination of recursive and force is the most destructive variant of the command. Double-check the path before running it.

Delete Multiple Directories at Once

You can pass multiple directory names as arguments to remove them all in a single linux delete directory operation:

rm -r Directory_1 Directory_2 Directory_3

Delete Files in Linux Using rm Command

Sometimes you want to remove specific files from a directory without touching the directory structure itself. rm handles that too.

Remove a single file from your current working directory:

rm file.txt

Remove multiple files at once:

rm file1.txt file2.txt file3.txt

Remove a file in a different directory by specifying its path:

rm dir/subdir/file.txt

Safe Deletion with Confirmation Flags

The -i flag prompts before every deletion. Slower, but safe when you're removing files you haven't checked recently:

rm -i file1.txt file2.txt file3.txt

Terminal will ask for a Y/N on each file. If you're deleting a large batch and only want one confirmation, use -I instead:

rm -I file1.txt file2.txt file3.txt

To skip all confirmation, including for write-protected files, use -f:

rm -f file.txt

Using Wildcards to Delete Multiple Files

Wildcards let you match groups of files with a single command rather than listing them individually. The asterisk matches any number of characters; the question mark matches exactly one.

Delete all .txt files in the current directory:

rm *.txt

Delete all files beginning with the letter 'a':

rm a*

Delete files with single-character extensions:

rm *.?

Warning: Run ls first to see exactly which files a wildcard pattern matches before using it with rm. Wildcards don't care about file importance.

VPS File Management via SSH Terminal

On a VPS or dedicated server, you'll run these commands over SSH. Connect from a terminal application or SSH client - on Linux and macOS that's the built-in Terminal; on Windows, PuTTY or Windows Terminal work fine.

If you'd rather not manage files through the command line, most VPS providers let you install a control panel. CyberPanel and cPanel both offer a file manager with a graphical interface - useful for one-off tasks, though slower than the command line for anything repetitive.

For quick access without installing additional software, many providers now include a browser-based terminal directly in the VPS dashboard. Open it, authenticate, and you're running bash commands from any browser without a local SSH client.

Whatever access method you use, the linux rm command and rmdir behave identically. The only thing that changes is how you get to the prompt.

Quick Reference: rm vs rmdir

  • rmdir - empty directories only, safe by default, returns an error if directory has content
  • rm -d - rm's equivalent for empty directories
  • rm -r - removes non-empty directories recursively; cannot be undone
  • rm -rf - recursive, no prompts; use only when you're certain of what you're targeting
  • rm -i - interactive mode; confirmation before each deletion
  • rm * / rm ? - wildcards for batch deletion; verify with ls before running

The linux delete directory operation that gets admins in trouble is almost always rm -rf pointed at the wrong path. Slow down, verify the path with pwd and ls, then execute. That extra five seconds prevents a very bad afternoon.

Scroll to Top