Software & OS

How to Delete a Directory in Linux (rm -r and rmdir)

To delete a directory in Linux, run rm -r directory_name, which removes the directory and everything inside it. If the directory is empty, rmdir directory_name does the same job and refuses to touch a folder that still has files in it.

Both commands are part of GNU coreutils and are installed on practically every Linux distribution. Deleting is permanent: neither command sends anything to a trash folder.

Delete an empty directory with rmdir

rmdir old-project

The rmdir man page describes its job in one line: "Remove the DIRECTORY(ies), if they are empty." If the directory contains anything, rmdir fails and leaves it alone. That makes it the safe choice when you expect a folder to be empty, because a surprise file stops the command instead of being deleted.

Useful options:

  • -p (--parents) removes the directory and its ancestors. The man page gives rmdir -p a/b as similar to rmdir a/b a.
  • -v (--verbose) prints a diagnostic for every directory processed.
  • --ignore-fail-on-non-empty ignores each failure to remove a non-empty directory, which is handy in scripts.

Delete a directory and its contents with rm -r

rm -r old-project

By default, rm does not remove directories. The -r option (also written -R or --recursive) tells it to "remove directories and their contents recursively". Every file and subdirectory below the one you name is deleted too.

To see what is happening as it runs, add -v (--verbose):

rm -rv old-project

Example output (yours will list your own files):

removed 'old-project/notes.txt'
removed 'old-project/src/main.c'
removed directory 'old-project/src'
removed directory 'old-project'

Look before you delete

Because the deletion is permanent, check the target first:

ls -la old-project
du -sh old-project

ls -la lists the contents including hidden files, and du -sh prints the total size in a human-readable unit. If the output surprises you, stop. Also check where you are with pwd when you use a relative path, since the same name in a different folder is a different directory.

Ask for confirmation: -i and -I

Two options add a prompt:

OptionBehaviour (from the man page)
-iPrompt before every removal.
-IPrompt once before removing more than three files, or when removing recursively. The man page calls it less intrusive than -i, while still giving protection against most mistakes.
rm -rI old-project

With -I you answer one question for the whole directory instead of one per file, which makes it practical for large folders.

What -f does, and why to be careful with it

-f (--force) means "ignore nonexistent files and arguments, never prompt". Without it, the GNU manual says that if a file is unwritable, standard input is a terminal, and -f is not given, rm asks whether to remove the file. With -rf that question disappears, and so does your last chance to notice a wrong path. Use -f when you have already checked the target, for example in a script, not as a habit.

Delete several directories at once

List them all in one command:

rm -r build cache tmp-output

Shell wildcards expand before rm runs, so rm -r logs-2025-* deletes every match. Preview the expansion with ls -d logs-2025-* first. A stray space can also change the meaning: rm -r build / names two targets, and the second is the root directory. rm refuses that by default (see below), but the same slip with a different path would not be caught.

Permission denied

You need write and execute permission on the directory that contains the one you are deleting, plus permission on anything inside it. If rm reports permission problems, look at the owner and mode with ls -ld old-project. The details are in Linux file permissions and chmod explained. If the directory belongs to another user or to the system, you need sudo:

sudo rm -r old-project

Treat sudo rm -r with extra care. It overrides the permissions that would otherwise stop you from damaging system files.

Directories with odd names

A name that begins with - is read as an option. The coreutils manual uses rm -- -f as its example: -- marks all following arguments as non-options. The prefix form also works:

rm -r -- -olddir
rm -r ./-olddir

Names with spaces need quotes: rm -r "My Old Folder".

Built-in protection for the root directory

GNU rm has --preserve-root, which is the default. When used with --recursive it fails on any attempt to remove /. The opposite option, --no-preserve-root, tells rm not to treat / specially. Do not use it. Also note that this protection covers only / itself. Deleting your home directory, or /etc, is not blocked.

Two related options: --preserve-root=all rejects any command-line argument that sits on a separate device from its parent, and --one-file-system makes a recursive delete skip any directory on a different file system from the argument. The second is useful when a folder contains a mount point that you do not want to empty.

rm -d: a middle option

rm -d (--dir) removes empty directories, so rm -d old-project behaves like rmdir. Use it when you already have rm in your command and want the empty-only safety.

Quick reference

GoalCommand
Remove an empty directoryrmdir dir
Remove a directory and its contentsrm -r dir
Same, with one confirmation promptrm -rI dir
Same, listing every item removedrm -rv dir
Remove an empty directory with rmrm -d dir
Remove a directory and its empty parentsrmdir -p a/b
Name starts with a dashrm -r -- -dir

If it still fails

If rmdir fails, the directory is not empty: run ls -A dir to see what is left, including hidden files, and use rm -r if you want it gone. If rm -r fails partway, read the message for the path it names. The usual causes are missing permissions on a subdirectory, or a file system mounted read-only. A directory that is the current working directory of a shell, or a mount point in use, may also resist removal; leave it with cd .. and try again. Whatever the error, avoid reaching for -f or sudo until you know which path is failing and why.

Frequently asked questions

How do I delete a directory that is not empty in Linux?
Use rm -r directory_name. The -r (recursive) option removes the directory and everything inside it. rmdir cannot do this, because it only removes empty directories.
Can I recover a directory after rm -r?
Not reliably. rm does not move anything to a trash folder; it removes the directory entries. Recovery depends on backups or file-system snapshots, so check what you are deleting before you run the command.
What is the difference between rm -r and rm -rf?
rm -r asks for confirmation in some cases, for example when a file is write-protected and input comes from a terminal. Adding -f (force) ignores nonexistent files and never prompts, so a mistyped path or the wrong target is deleted without any warning.
How do I delete a directory whose name starts with a dash?
Put -- before the name, as in rm -r -- -olddir, or give the path with a prefix, as in rm -r ./-olddir. Both stop rm from reading the name as an option.
Why does rm say it cannot remove a directory?
By default rm does not remove directories. Add -r to remove a directory with its contents, or -d to remove an empty one.

Sources