How to delete a non-empty directory in Terminal?

How do I delete the following directory?

I typed:

rmdir lampp

This error comes up:

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

Is there a command to delete all the files in the directory and delete the directory folder?

Asked By: naveen

rm -R lampp

However, you need to be careful with a recursive command like this, as it’s easy to accidentally delete a lot more than you intended.

It is a good idea to always double-check which directory you’re in, and whether you typed the command correctly, before pressing Enter.

Safer version

rm -R -i lampp

Adding -i makes it a little safer, because it will prompt you on every deletion. However, if you are deleting many files this is not going to be very practical. Still, you can try this first.

Note about -f option:

Many people suggest using -f (combining it into -Rf or -rf), claiming that it gets rid of annoying prompts. However, in normal cases you don’t need it, and using it suppresses some problems that you probably do want to know about. When you use it, you won’t be warned if your arguments supply a non-existing directory or file(s): rm will just silently fail to delete anything. As a general rule, try first without the -f: if there are problems with your arguments, then you’ll notice. If you start getting too many prompts about files without write access, then you can try it with -f. Alternatively, run the command from a user (or the superuser using sudo) that has full permissions to the files and directories you’re deleting to prevent these prompts in the first place.

Answered By: thomasrutter

Use the below command :

rm -rf lampp

It deletes all files and folders contained in the lampp directory.

In case user doesn’t have the permission to delete the folder:

Add sudo at the beginning of the command :

sudo rm -rf folderName

Otherwise, without sudo you will be returned permission denied. And it’s a good practice to try not to use -f while deleting a directory:

sudo rm -r folderName

Note: this is assuming you are already on the same level of the folder you want to delete in terminal, if not:

sudo rm -r /path/to/folderName

FYI: you can use letters -f, -r, -v:

  • -f = to ignore non-existent files, never prompt
  • -r = to remove directories and their contents recursively
  • -v = to explain what is being done
Answered By: Shantanu Banerjee

There are lots of ways to delete a directory through CLI mode. It depends on which way you are comfortable with.

rm -rvf /path/to/directory  
  • -r = remove directories and their contents recursively
  • -v = explain what is being done
  • -f = ignore nonexistent files, never prompt

If you are new in Linux, use the man pages of commands (man rm) for more option and more accuracy.

Answered By: papseddy

I was having some trouble with that today, but I overcame it with sudo.

Caveat: Be very certain you want to delete the entire thing before using the command below.

$ sudo rm -R [Directory name]

I successfully did this today, and removed many non-empty directories that I confirmed I didn’t want/need.

I’m using 14.04 LTS

Answered By: Thomas
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.