Ubuntu Linux: Delete Directory Command in Terminal

How do I delete a directory in terminal using Ubuntu Linux operating systems?

You need to use the rmdir utility / command. The rmdir utility removes the directory entry specified by each directory argument, provided it is empty. Arguments are processed in the order given. In order to remove both a parent directory and a subdirectory of that parent, the subdirectory must be specified first so the parent directory is empty when rmdir tries to remove it.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time 1m
[/donotprint]Please note directory often referred to as a folder in the Apple Mac OS X and Microsoft Windows operating systems.


The syntax is:

rmdir dirName


rmdir [option] dirName

WARNING! These examples may crash your computer or may result into data loss, if executed without proper care.

Ubuntu delete directory called /tmp/foo

Open the terminal. Type the following command:
$ rmdir /tmp/foo
To remove foo and bar empty directories, type:
$ rmdir foo bar

Recursive directory removal on Ubuntu

Remove all files and directories including all sub-directories i.e. recursive removal:
$ rm -rf /path/to/directory
$ rm -rf /tmp/foo

Please note that you can also pass -p option to the rmdir command. Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last most component:
$ rmdir -p /tmp/x/y/z

Deleting directories as a superuser on ubuntu

If directory is owned by root or any other user or if you are getting “access denied/permission denied” message, try:

### Warning: careful with sudo and rm/rmdir command. ### 
### Check twice before you hit [enter] key ###
sudo rmdir /path/to/dir
sudo rm -rf /path/to/dir

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 33 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
33 comments… add one
  • David Jan 25, 2009 @ 0:01

    Thanks for this. However, it doesn’t always work. I found that Thunderbird stores a hidden file in a /home directory and I kept getting a message saying that the folder was not empty. In the end, I used “gksudo nautilus”, renamed the hidden file and then deleted it.

  • PCMaind Jul 11, 2009 @ 8:16

    tnx for this

  • Raffy \n Apurillo Aug 13, 2009 @ 5:49

    It really works thanks

  • LCC Feb 5, 2010 @ 19:10

    I really doesn’t work. I get the error message saying rmdir: invalid option — ‘r’ when i put in the following:
    rmdir -r /sametime-connect-8.0.2/opt/ibm/lotus/sametime/eclipse/plugins/org.apache.lecene_1.4.103.v20060601/META-INF

  • Amjad Apr 1, 2010 @ 11:50

    @ LCC:
    Go where you have your directory. But dont enter into Directory.Use
    $> rm -rf Directory/*
    This shall remove all the files contained in “Directory”.
    Then do
    $> rmdir Directory
    This shall remove the Directory.
    Hope it works

    • D0tty Jan 10, 2012 @ 1:56

      You are a god. Simple and clear instructions that worked when apparently I was doing something wrong with the instructions given above.


  • nikhil Jun 25, 2010 @ 4:59

    thanx 4 dis help…

  • Ahsan Nov 10, 2010 @ 10:05

    thanx it really helped me

  • Techie Nov 19, 2010 @ 9:27

    I tried everything but still no luck. See what happened :

    [rockie01@mxrplnux01 home]$ sudo rmdir -rf /home/jzhou01
    rmdir: invalid option — r
    Try `rmdir –help’ for more information.
    [rockie01@mxrplnuxt01 home]$ sudo rmdir -rf /home/jzhou01/*
    rmdir: invalid option — r
    Try `rmdir –help’ for more information.

    I also tried to delete the user with userdel -r but it says, the user does not exist then when I check the home directory, the user name and subdirectories are still there. Someone who deleted the user account before me did not do it correctly because the user name and it’s sub-directories are still in the home directory. How do I remove this user name directory?

    • Amjad Nov 19, 2010 @ 13:13

      rmdir -r is nothing.
      rm -rf will remove the files in a certain driectory.
      when the directory is empty, do rmdir .
      $>rm -rf /home/jzhou01/*
      $> rmdir /home/jzhou01/
      Hope it works.
      and by the way I dont get why u want it with sudo.

      • Techie Nov 19, 2010 @ 16:04

        Actually I did those already. I tried :
        $ sudo rm -rf /home/jzhou01/* and $sudo rmdir /home/jzhou01 but it did not work.

        With the -rf the reply was :
        rmdir: invalid option β€” r
        Try `rmdir –help’ for more information

        Without the -rf and just $sudo rmdir /home/jzhou01, the reply was :

        Directory is not empty.

        I found out later that the only files left in the directory were started with a . , sorta like .profile or some other invisible files that will not show with: ls /home/namedir or ls -la /home/namedir

        When I tried this syntax :
        $sudo rm /home/jzhou01 it worked. I checked the /home directory and the username of jzhou01 and it’s subdirectories and files were deleted or removed.

        We use sudo because our management is very strict and only three senior systems admins in Unix are the ones with root access besides the manager of that dept. Otherwise all others in the Intermediate Systems Admin category will be in the sudoer’s file and will just have to use “sudo” for any administrative command. Very strict training so that when we reach the Senior level, we are able to fine tune and troubleshoot problems easily.


        • Kenneth Wahl Jr. Apr 22, 2011 @ 0:53

          using “sudo” will confirm all files of specified directory will be removed but there’s no need unless the permissions are set against your needs. Try sudo chmod 770 “file name”(or 777, shouldn’t matter really. There is a small formula to determine the permissions with chmod, look it up) Make sure you’re in the directory with the file or type in the full path i.e. sudo chmod 770 BunkGames if you’re in the directory and sudo chmod 770 /home/user/Games/BunkGames if your anywhere else. Hope this helped. Also make sure you use the “man pages”. man=manual and if you type “man” before nearly any command you will get to see nearly all of that commands capabilities and how to use them.

          • Roelof Roos Mar 16, 2012 @ 1:28

            The chmod formula is binary, the first number indicates the user, the 2nd group the “group” and the third one the world (=everyone).
            The numbers are ordered Read, Write Execute. Where read is 1, write is 2 and execute is 4. So for a read + execute rank the number would be 1+4 = 5.

            I know this is late, but I thought you might be interested.

  • stullko Dec 19, 2010 @ 11:08

    Thanks for this! it really helped me!

  • goosy Jan 28, 2011 @ 15:13

    thx for this handy help

  • black skull Feb 15, 2011 @ 19:06

    thx for help :)

  • wildcat Feb 20, 2011 @ 4:32

    I am having real trouble with Linux, i need some help.. I can’t figure out how to remove a directory i made, i was trying to install openoffice 3.3, then following the steps posted in some website, something like ( sudo -vxzf filename) done that and other stuff, the outcome was a file directory, that i couldn’t remove anymore “and no openoffice at all”. I have tried ( $rmdir dirpath), when i exe. that command all that says is ( dirpath is a directory).. that’s all i checked the dirpath, it’s still there. I am new to Linux..appreciate any help.
    I am running Ubuntu 10.10.

  • Techie Feb 20, 2011 @ 17:09

    Use sudo rmdir -rf /dir/dir/*file* <–(the complete directory path) then try rmdir /dir/dir/*

  • matthew sellers Mar 6, 2011 @ 2:18

    i know this doesnt necessarily go with the topic but i am really getting frusterated.. ok so i cant seem to get the terminal to let me type in my sudo password.. i put in the coding for the compiz fusion but when it brings up the statement to type my sudo password i try and nothing is coming up no letters what so ever… it works all the way up until that point.. so i hit enter to try it again and it keeps doin the same thing but only with my pass word can some one please help me figure this out cuz it is really hindering the work i am trying to do..

    • Techie Mar 6, 2011 @ 14:05

      This is a secured login. Of course it will not show what you type in. It is invisible. When it prompts you for your password, you type your password in but you do not see ******* as you type in your password in other terminals. This is a security measure. Just type your password (yes, it is there even if you cannot see it. It’s invisible) then hit enter. This will log you into the system. Then you can just do your thing. Unless you are in the sudoers file. In order to be able to access a particular machine, first of all, you should have an account set up in the box in order for you to log in. Second, if you do not have straight root access (usually done for first level support as having root access might be too dangerous for inexperienced admins), you will be added to a sudoers file. If you are on the sudoers file then depending on what type of group access you have, your sudo config file only limits you to what you are allowed to do. Ask you Systems Admin (Unix / Limux) to see what the real issue is. Usually, they are just trying to give someone a hard time or you might need a manager’s approval before you are given access by the Unix Systems Admin.

  • Amjad Mar 6, 2011 @ 9:26

    @matthew: This might tell you.

    In fact so far I don’t know any option to show the password characters.

  • JD Mar 25, 2012 @ 0:14

    God Bless u.. it’s solve my problem…

  • paracha Jun 27, 2012 @ 6:03

    sir when i remove direcotry i find this error ……read only permission.we change permission to 755 but not deleted use all rmdir and rm command but still ..?

  • Amjad Jun 27, 2012 @ 9:38

    check who has the permissions to xrw the directory .. !
    If it is on your personal pc, you may have to do sudo,
    If it is on some server, then only server admin (root) can delete it.

  • Amninder Aug 7, 2012 @ 21:05

    whenever I try to delete the folder, this comes up:
    rmdir: failed to remove folder ‘Plans’: Device or resource busy.

    I had used encryptfs-utils to encrypt it earlier, however the encryption didn’t really work. Now I can’t seem to delete the file.

  • Humayun A. Daud Sep 4, 2012 @ 9:04

    great article!
    thnk you

  • Chad Sep 4, 2012 @ 11:12

    Very bad command if used improperly!!!!!!! I really messed things up using that command by accidentally putting a “/” in there.

    rm -rf / <— then I went to hit the right shift key and hit enter instead

    This will wipe everything from the root directory!!! Do not perform as root or you end up like I did, very very very upset :) (I am really glad that it was a blank playground on a vps with an instant reinstall dashboard.)

  • Indranil Feb 19, 2013 @ 3:48

    To sorcefully remove you may try
    $ sudo rm -rf /Path

  • Indranil Feb 19, 2013 @ 4:42

    To forcefully remove you may try
    $ sudo rm -rf /Path

  • Tom Feb 20, 2013 @ 23:53

    Thanks! Was having a hell of a time trying to remove a directory I had set up as a domain which was uneditable the way I had initially set it up.

  • m.j Aug 27, 2013 @ 6:34

    rmdir: failed to remove `.kde’: Directory not empty
    what should i do now?

    • Amjad Aug 29, 2013 @ 13:19

      Do rm -rf ‘.kde’

  • Ramlee Aug 6, 2014 @ 7:14

    Hi everyone,
    I’m running the latest Ubuntu Studio on my laptop and I’m trying to delete a folder (with a few subdirectories and single files) that I incorrectly extracted from a compressed file yesterday. The path to the folder is;

    /usr/share/sounds/sf2/GeneralUser GS 1.44 FluidSynth

    That last folder (GeneralUser GS etc.) is the one I want to delete with all of its contents but every combination of the rm -r command I try in a terminal fails. It’s all very perplexing. I have a feeling I’m not getting the command exactly right or perhaps I don’t have the required permissions to delete the directory. All advice welcome.

    Regards Ramlee.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum