Skip to content

Rm Is a Directory: Why the Error Happens and How to Fix It Safely

By HHB Admin Editorial Team · 6 October 2026

Rm Is a Directory: Why the Error Happens and How to Fix It Safely

Fix 'rm: cannot remove: Is a directory' on Linux or Mac. Learn why rm refuses folders, when to use rm -r or rmdir, and a safety checklist before deleting.

Last Updated:

rm Is a Directory: Why the Error Happens and How to Fix It Safely

You type rm old-project, press Enter, and the terminal replies rm: cannot remove 'old-project': Is a directory. The "rm is a directory" message is not a bug. The command is refusing a folder on purpose. This guide explains the rule behind the error, gives the correct command for Linux and macOS, and includes a pre-deletion checklist so a fix does not become a disaster.

Contents
  1. Quick answer: fixing rm is a directory
  2. Why rm refuses directories
  3. Commands to remove a directory
  4. Decision table: which command to use
  5. rm is a directory on Linux vs Mac
  6. Pre-deletion safety checklist
  7. Common mistakes and edge cases
  8. FAQ: rm is a directory

Quick answer: fixing rm is a directory

To fix the "rm is a directory" error, add the recursive flag: rm -r foldername deletes the folder and everything inside it. If the folder is already empty, rmdir foldername is the safer choice because it refuses to delete anything with contents. Check the path before you press Enter.

The 10-second fix:

  • Empty folder: rmdir foldername
  • Folder with contents, with a prompt for each item: rm -ri foldername
  • Folder with contents, no prompts: rm -r foldername
  • Folder containing write-protected files: rm -rf foldername (last resort only)

The Unix shell has no recycle bin for rm. Files removed this way skip the desktop Trash. The safe habit is to list the target first with ls -la foldername, then delete it.

Terminal window showing the rm cannot remove is a directory error
Photo: panumas nikhomkhai / Pexels

Why rm refuses directories

The rm command refuses directories because the POSIX standard requires it to. Under POSIX.1-2017 (IEEE Std 1003.1-2017), if an operand is a directory and neither -R nor -r is given, rm must write a diagnostic to standard error and do nothing more with that operand.

That diagnostic is the "rm cannot remove is a directory" line you see. The GNU implementation follows the same rule. Its Linux manual page states that rm removes each specified file but does not remove directories by default. The refusal is a guardrail. A single typo such as rm build instead of rm build.log cannot quietly destroy a whole project tree.

What does rm actually do to a file?

The rm command unlinks a name from the filesystem. It does not necessarily erase data straight away. The Wikipedia entry on rm explains that rm calls unlink(), and a file is only deleted when its last remaining link is removed. POSIX specifies that rm performs the equivalent of unlink() for files and rmdir() for directories. The rmdir() call only succeeds on an empty directory. That is why recursive mode must empty the folder from the bottom up first.

This mechanism has a practical effect. If a program still has a file open, deleting the file frees no disk space until that program closes it. Admins often see this when they clear huge log files and the disk stays full.

Where did the -r flag come from?

The -r flag is the long-standing Unix convention for recursive removal. The POSIX rationale calls -r historical practice on all known systems. It adds -R as a synonym for consistency with other utilities that descend recursively, such as cp -R. The two flags behave identically in rm.

Commands to remove a directory

There are four ways to remove a directory, from safest to most destructive: rmdir, rm -ri, rm -r, and rm -rf. Choose the least powerful command that does the job. Escalate only when the gentler command fails, and read its error message before you do.

How do you remove a directory in Linux?

To remove a directory in Linux, run rm -r directoryname for a folder with contents or rmdir directoryname for an empty one. The recursive flag tells rm to descend into the folder, delete every file and subfolder, then remove the folder itself. Add -i to confirm each deletion, or -v to print each removed path.

  1. Confirm your location: pwd
  2. Inspect the target: ls -la directoryname
  3. Check its size: du -sh directoryname
  4. Delete with feedback: rm -rv directoryname
  5. Confirm it is gone: ls directoryname, which should report "No such file or directory"

How do I delete an entire directory?

To delete an entire directory with all its contents, use rm -r path/to/directory. If rm asks about write-protected files, either answer each prompt or add -f to force the deletion. Deep trees are not a problem: POSIX requires rm to descend to arbitrary depths and not fail on path length, unless an operand you typed exceeds system limits.

How do I remove a directory in rm?

You remove a directory with rm by adding -r or -R, as in rm -r foldername. Without either flag, rm treats a directory as an operand it must not touch and prints the "Is a directory" error. GNU rm also accepts -d, which removes empty directories in the same way as rmdir.

Decision table: which command to use

The right command depends on two questions: is the directory empty, and do you need to see what gets deleted? Use this decision table as a quick reference when the "rm is a directory" error appears.

SituationCommandRisk levelWhy
Directory is emptyrmdir dirLowestFails if anything is inside
Unsure what is insiderm -ri dirLowPrompts for every item
Many files, need a recordrm -rv dirMediumPrints each deleted path
Known contents, no promptsrm -r dirMediumStill stops at protected files
Write-protected files, verified pathrm -rf dirHighNo prompts, no warnings
Only clear the contentsrm -r dir/*MediumKeeps the folder; misses hidden files

Our editorial rule of thumb at HHB Admin is to treat -f as an override, not a default. If a command needs -f, find out why before you use it. The cause is usually permissions, read-only mounts, or files owned by another user.

rm is a directory on Linux vs Mac

The "rm is a directory" error behaves the same on Linux and macOS because both follow POSIX, and the fix is the same: rm -r. The differences are in the error wording and the extra flags. Linux usually ships GNU coreutils. macOS ships a BSD-derived rm.

  • Linux (GNU): prints rm: cannot remove 'dir': Is a directory and supports long options such as --recursive, --interactive=once and --preserve-root.
  • macOS (BSD): prints rm: dir: is a directory, with no quotes and a lowercase "is". Long GNU options are generally unavailable, so scripts that use --recursive can fail on a Mac.
  • macOS Trash: Finder's Trash is separate from rm. Terminal deletions do not appear there.

For scripts that must run on both systems, use only short POSIX flags (-r, -f, -i). Those three behave the same everywhere. More command-line explainers are in our library of practical guides.

Side-by-side Linux and macOS terminals removing a directory with rm -r
Photo: Vadym Alyekseyenko / Pexels

Pre-deletion safety checklist

Before running any recursive delete, check five things: your current directory, the exact target, its contents, whether a backup exists, and whether any variable in the path could be empty. These checks take under a minute and prevent most accidental deletions.

HHB Admin Recursive-Delete Checklist

  1. Where am I? Run pwd. Relative paths depend on your current directory.
  2. What exactly? Press Tab to autocomplete the path rather than typing it.
  3. What is inside? Run ls -la, and ls -R | head for deep trees.
  4. Is it backed up? If not, archive it first: tar czf backup.tgz dir.
  5. Any variables? In scripts, write rm -r "${DIR:?}" so the script stops if DIR is empty.
  6. Dry run: Swap rm -r for ls -d with the same arguments to preview what matches.

POSIX includes some built-in protection. The rm specification requires rm to refuse, with a diagnostic, any operand whose final component is . or .., or that resolves to the root directory. It offers no protection for /home or a project folder you typed by mistake. The checklist covers that gap.

Common mistakes and edge cases

Most failed or dangerous deletions come from shell behaviour, not from rm itself. Unquoted spaces, glob patterns, hidden files and symbolic links each change what rm receives. Know these cases before you escalate to -rf.

  • Spaces in names: rm -r My Folder tries to delete two targets, "My" and "Folder". Quote the name: rm -r "My Folder". Do not quote a wildcard, because a * inside quotes is treated literally.
  • Hidden files survive dir/*: the glob skips dotfiles such as .git. Delete the folder itself instead.
  • Symlinks: rm link removes only the link. A trailing slash (rm -r link/) can follow the link into its target on some systems.
  • "Directory not empty" from rmdir: the folder is protecting you. Inspect it before switching to rm -r.
  • "Permission denied": you need write permission on the parent directory, not only the file. Check with ls -ld parent.
  • Aliases: some distributions alias rm to rm -i for root. Run type rm to see what actually executes.

Application-managed folders such as web app trash, cache or data directories add another risk. Deleting them from the shell can leave the application's database pointing at missing files. Use the application's own cleanup command where one exists. Browse our related troubleshooting articles for more fixes of this kind.

FAQ: rm is a directory

What does "rm: cannot remove: Is a directory" mean?

It means you asked rm to delete a folder without the recursive flag. POSIX requires rm to print this diagnostic and leave the directory untouched. Rerun the command as rm -r foldername, or use rmdir foldername if the folder is empty. Nothing was deleted when the error appeared.

Is rm -r the same as rm -R?

Yes. In rm, -r and -R are identical. The POSIX rationale describes -r as historical practice and says -R was added for consistency with other recursive utilities. Either flag lets rm descend into a directory and delete its contents before removing the directory itself.

Can I recover a directory deleted with rm -r?

Usually not through normal means. rm unlinks files and bypasses the desktop Trash, so restoring from a backup or snapshot is the dependable route. Stop writing to the disk at once. Filesystem recovery tools sometimes retrieve data that has not yet been overwritten, but results vary.

How do I fix rm is a directory on a Mac?

On a Mac, run rm -r foldername in Terminal. The macOS error reads rm: foldername: is a directory, but the cause and the fix are the same as on Linux. Avoid GNU-only long options such as --recursive, because the BSD-based rm on macOS may reject them.

When should I use rmdir instead of rm -r?

Use rmdir when you expect the folder to be empty. rmdir fails with "Directory not empty" if anything remains inside, which makes it a built-in safety check. Use rm -r only when you have inspected the contents and intend to delete all of them.

Is rm -rf dangerous?

Yes. rm -rf deletes recursively and suppresses every prompt and most errors, so a wrong path is destroyed silently. Reserve it for verified paths that contain write-protected files. In scripts, guard variables with "${VAR:?}" so an empty value cannot widen the target.

Review note: This guide was prepared by the HHB Admin Editorial Team using the POSIX.1-2017 rm specification and GNU rm documentation, checked on 6 October 2026. Flag behaviour can vary between operating system releases, so run man rm on your own system to confirm. We review this page when the standards or major implementations change. Find more explainers in our article archive.