0% found this document useful (0 votes)
13 views

Command Line - How To Delete A Non-Empty Directory in Terminal - Ask Ubuntu

This document discusses different commands that can be used to delete a non-empty directory from the terminal in Linux/Unix systems. The rmdir command will not work for non-empty directories, but the rm command with recursive and force options like rm -rf can delete a non-empty directory and all its contents. It is best to use rm -r rather than rm -rf to avoid accidentally deleting files without prompts.

Uploaded by

irrosennen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Command Line - How To Delete A Non-Empty Directory in Terminal - Ask Ubuntu

This document discusses different commands that can be used to delete a non-empty directory from the terminal in Linux/Unix systems. The rmdir command will not work for non-empty directories, but the rm command with recursive and force options like rm -rf can delete a non-empty directory and all its contents. It is best to use rm -r rather than rm -rf to avoid accidentally deleting files without prompts.

Uploaded by

irrosennen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4/28/24, 5:00 PM command line - How to delete a non-empty directory in Terminal?

- Ask Ubuntu

Anybody can ask a question


Ask Ubuntu is a question and answer site for
Ubuntu users and developers. It only takes a
minute to sign up. Anybody can answer

Sign up to join this community


The best answers are voted up and
rise to the top

How to delete a non-empty directory in Terminal?


Asked 11 years, 5 months ago Modified 6 years, 3 months ago Viewed 4.1m times

How do I delete the following directory?

I typed:
785
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?

command-line

Share edited Aug 4, 2014 at 17:37 asked Nov 16, 2012 at 4:37

Improve this question Peter Mortensen naveen


884 2 11 17 8,077 4 15 14
Follow

https://askubuntu.com/questions/217893/how-to-delete-a-non-empty-directory-in-terminal 1/5
4/28/24, 5:00 PM command line - How to delete a non-empty directory in Terminal? - Ask Ubuntu

1 I'm unable to remove a directory like "New Folder" using all the above detailed commands. It's
double worded. But I want to remove that directory. Any suggestions will be welcomed.
T.Divakara, Bengaluru, India – user307933 Jul 22, 2014 at 7:33

Its the blank space in the file name, try using 'quotes' > rmdir 'New Folder' < then the folder
disapers, or use escape characters for non-vissible characters. – Ken Mollerup Feb 5, 2015 at
13:25

5 Just doing rm -r lampp will do. – John Strood Jul 1, 2016 at 19:39

4 Answers Sorted by: Highest score (default)

Use the below command :

1112 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

https://askubuntu.com/questions/217893/how-to-delete-a-non-empty-directory-in-terminal 2/5
4/28/24, 5:00 PM command line - How to delete a non-empty directory in Terminal? - Ask Ubuntu

-v = to explain what is being done

Share edited Jun 12, 2020 at 14:37 answered Nov 16, 2012 at 5:19
Improve this answer Community Bot Shantanu Banerjee
1 11.3k 1 13 6
Follow

73 In my humble opinion, it's a good practice never to add the "f" on first attempt. Its purpose is
to ignore certain warning prompts that may be important, especially if you've accidentally
done it on/from the wrong directory. In my opinion it's good to try without the "f" first, then
only if you are encountering a lot of warning prompts and you're sure it's OK to ignore them
all, Ctrl+C out of it and repeat the command with the "f". – thomasrutter Aug 27, 2013 at 2:52

1 @BKSpurgeon not unless you accidentally type --no-preserve-root too. – muru Oct 2,
2015 at 7:22

@thomasrutter ... Agree. A file "xxx" owner: root and group: root can BE deleted with the -f
switch; and without sudo. This is the message without -f: "rm: remove write-protected regular
file ‘/home/william/.cache/netbeans/v08.01/tmp/xxx’? _". _Tread gently. – will Dec 2, 2015 at
14:06

rm -R lampp

106 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

https://askubuntu.com/questions/217893/how-to-delete-a-non-empty-directory-in-terminal 3/5
4/28/24, 5:00 PM command line - How to delete a non-empty directory in Terminal? - Ask Ubuntu

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.

Share Improve this answer edited Jan 16, 2018 at 22:45 answered Nov 16, 2012 at 4:56
Follow thomasrutter
36.9k 10 88 108

6 Note: lowercase -r can be used too and has the same effect here. I only tend to use
uppercase -R because it's consistent with other commands I use such as grep and chmod ,
some of which only support the uppercase form. – thomasrutter Sep 20, 2013 at 4:04

This one worked for me using adb (android debug bridge) on Android. Was trying rm -rf but it
didn't work. Had to use rm -R. Thanks. – raddevus Mar 13, 2016 at 2:15

1 This is a good solution, especially if inexperienced with using the Bash terminal. The command
can be shortened to rm -ir FOLDER . – AlainD Aug 30, 2019 at 9:50

There are lots of ways to delete a directory through CLI mode. It depends on which
way you are comfortable with.
30
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.

Share Improve this answer edited Jan 20, 2015 at 19:25 answered Nov 16, 2012 at 5:38
Follow muru papseddy
198k 55 488 744 506 3 7

https://askubuntu.com/questions/217893/how-to-delete-a-non-empty-directory-in-terminal 4/5
4/28/24, 5:00 PM command line - How to delete a non-empty directory in Terminal? - Ask Ubuntu

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
-1
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

Share Improve this answer edited Jan 20, 2015 at 18:23 answered Sep 28, 2014 at 23:00

Follow Kenny Thomas


103 2 11

4 "SUDO" is not a command - "sudo" is. Nevertheless, you do not use sudo to delete a non-
empty directory, you use it to delete a file that is not owned by you. Doing this as a matter of
course is dangerous and not very smart, as you should not be blindly deleting files you do not
own. – Marty Fried Sep 28, 2014 at 23:53

Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer
this question. The reputation requirement helps protect this question from spam and non-answer
activity.

https://askubuntu.com/questions/217893/how-to-delete-a-non-empty-directory-in-terminal 5/5

You might also like