Skip to content

Linux basicsFor beginnersDuration around 5 min.

How to remove a directory in Linux with rm and rmdir

In Linux, you delete an empty folder with rmdir and a folder including its contents with rm -r. This tutorial shows both commands with their messages, the variants with and without prompting, and what to check before running rm -rf — because there is no recycle bin in the terminal.

Tested on with Ubuntu 24.04 LTS and Ubuntu 26.04 LTS

At a glance

  • rmdir only deletes empty folders and otherwise reports “Directory not empty” — the safe first attempt.
  • rm -r deletes a folder with everything in it; -i prompts before each step, -f never prompts and stays silent for missing files.
  • What rm deletes is gone: there is no recycle bin. Check the path beforehand with ls.

Delete an empty folder: rmdir

rmdir only removes a folder if it is empty. If there is still something in it, it aborts and deletes nothing — which is why it is the safe first attempt.

Delete an empty folder
rmdir my-folder
Message when the folder is not empty
rmdir: failed to remove 'my-folder': Directory not empty

rm -d my-folder is equivalent: it also only deletes empty folders.

Delete a folder and its contents: rm -r

-r stands for recursive: rm goes into the folder, deletes every file and subfolder, and finally the folder itself. Without -r, rm refuses to touch a folder.

Delete a folder with contents
rm -r my-folder
Message without -r
rm: cannot remove 'my-folder': Is a directory

With prompting or logging

-i prompts before each step, first asking whether rm should even enter the folder. If you answer with n, everything remains as it is.

Delete with prompting
rm -ri my-folder

-v lists every deleted file — useful for seeing what is gone afterwards:

With output of every file
rm -rv my-folder
Output of rm -rv
removed 'my-folder/sub/b.txt'
removed directory 'my-folder/sub'
removed 'my-folder/a.txt'
removed directory 'my-folder'

Without any prompting: rm -rf

-f suppresses all prompts, even for read-only files, and reports no error if the folder does not exist at all. That is why it is used in scripts — and why it is dangerous.

Delete without prompting
rm -rf my-folder

Before every rm -rf

Look at what you are deleting and how large it is. If the path is incorrect, ls shows an error message instead of your data — and you have lost nothing.
Check beforehand
ls -la my-folder
du -sh my-folder

rm protects the root folder itself: an rm -rf / aborts unless someone explicitly specifies --no-preserve-root.

Message for the root folder
rm: it is dangerous to operate recursively on '/'
rm: use --no-preserve-root to override this failsafe

Delete folders of other users

If a folder belongs to another user or root, rm reports “Permission denied”. With sudo, you delete it as root — and without the protection of file permissions. Double-check the path here.

Delete with administrator privileges
sudo rm -r /srv/old-project

The chmod calculator shows what permissions a folder has and what the numbers mean.

Names with spaces or hyphens

A name with spaces belongs in quotation marks, otherwise rm deletes two wrong targets. If a name begins with a hyphen, rm considers it an option; two hyphens before it terminate the options.

Special cases
rm -r "My Folder"
rm -r -- -old

Delete all empty subfolders at once

find searches for empty folders; view them first, then delete. With -delete, find works from the inside out, so that a folder containing only empty folders disappears right along with them.

Find and delete empty folders
find /srv/data -type d -empty -print
find /srv/data -type d -empty -delete

Typical errors

  • “Directory not empty”: rmdir only deletes empty folders. Use rm -r if the contents really need to go.
  • “Is a directory”: rm needs the -r option for folders.
  • “No such file or directory”: The path is incorrect, or a space in the name is not enclosed in quotation marks.
  • “Permission denied”: The folder belongs to another user. Delete it with sudo after checking the path.
  • “Device or resource busy”: A process or mount is still using the folder. End the process or unmount the drive first.

We tested all commands in this guide on 2 December 2026 — with Ubuntu 24.04 LTS and Ubuntu 26.04 LTS. If a step changes at the manufacturer, we update it here along with the date.

Official documentation

Frequently asked questions about deleting in Linux

What is the difference between rmdir and rm -r?

rmdir only deletes empty folders and aborts otherwise. rm -r deletes a folder with all files and subfolders inside it.

Can I recover files deleted with rm?

Not with built-in tools. rm removes files without a recycle bin; they can only be recovered from a backup or a snapshot of the drive.

Snapshots and backup for VPS

What does rm -rf mean?

-r deletes recursively, i.e. with everything inside, -f without any prompts and without errors for missing files. The combination is fast and therefore dangerous if the path is incorrect.

How do I delete a folder safely?

First view the path with ls, then delete with rm -ri, which asks before every step. In scripts, it helps to check the path for an empty value before deleting.

How do I delete only the empty subfolders?

With find and the options -type d -empty -delete. First use -print to see which folders are affected, then delete.

Further reading

More tutorials

  • The scp command with examples: copy files over SSH

    Linux basicsFor beginnersabout 5 min

    scp copies files over an SSH connection — encrypted, with the same credentials as your login, and without an additional service on the server. This guide shows the commands for common use cases, each with an example to copy.

  • How to check your Ubuntu version, codename and kernel

    Linux basicsFor beginnersabout 2 min

    A single command in the terminal shows which Ubuntu version is running on a server. Here are the common ways with their output — for the version with point release, the codename, the kernel and the architecture.

  • How to update Ubuntu and upgrade to the next LTS release

    Linux basicsFor beginnersabout 15 min

    A server only stays secure if it receives its updates. This guide shows the three stages: applying updates manually, running security updates automatically and switching to the next LTS release.

Snapshots before deleting

For VPS, you create snapshots as needed in the customer area — a way back in case an rm ever hits the wrong target.