Delete / Remove a Directory Linux Command

Posted on in Categories , , last updated July 17, 2013

I am a new Linux user. How do I delete or remove a directory using command line option?

You can use the following commands to delete the directory (also known as folder in the Macintosh OS X and Microsoft Windows operating system).

a] rmdir command – Deletes the specified empty directories.

b] rm command – Delete the file including sub-directories.

Syntax- rmdir command

The rmdir command remove the DIRECTORY(ies), if they are empty. The syntax is:

rmdir directory-name
rmdir [option] directory-name

Examples

Open a command-line terminal (select Applications > Accessories > Terminal), and then type the following command to remove a directory called /tmp/docs:

rmdir /tmp/docs

If a directory is not empty you will get an error:
$ rmdir letters
Output:

rmdir: letters: Directory not empty

You can cd to the directory to find out files:
$ cd letters
$ ls

In this example, remove data, foo and bar if bar were empty, foo only contained bar and data only contained foo directories:

cd /home/nixcraft
rmdir -p data/foo/bar

Where,

  1. -p – 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.

Linux remove entire directory including all files and sub-directories command

To remove all directories and subdirectories use rm command. For example remove *.doc files and all subdirectories and files inside letters directory, type the following command (warning all files including subdirectories will be deleted permanently):
$ rm -rf letters/
Where,

  1. -r : Attempt to remove the file hierarchy rooted in each file argument i.e. recursively remove subdirectories and files from the specified directory.
  2. -f : Attempt to remove the files without prompting for confirmation, regardless of the file’s permissions
See also
  • For more information read man pages: rmdir(1),rm(1)

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

102 comment

    1. I give it a 3 star. There has to be a more flexible way to do it. Removing files permanently is not a good option for linux this is a windows kind of rubbish thing

      1. It’s not exactly permanent in Windows either. In fact, the “undelete” command has been available for quite some time… the Recycle Bin/Trash makes it possible to restore in both operating systems (from the GUI of course). I’m not exactly glorifying M$ but they aren’t exactly the cesspool of operating systems… LOL they’re not the best either but if M$ Windows were FREE and/or Open it may change some opinions.

  1. Hello all
    i am having some trouble in deleting a directory in ubuntu. actually my directory’s ownership changes to ?? ?? also file size is also changed to ?? only name is there and all things changes to ?? like this,
    ls: cannot access pg_database: No such file or directory
    total 0
    -????????? ? ? ? ? ? pg_database
    I need your suggestions

    Thanks in advance !!!!!!!!!!!!!!

  2. In Linux correct command would be rm -rf /letters/
    The slash before the directory name is required or else it will not be seen as a directory and you will get an error. Also, consider using sudo such as:
    sudo rm -rf /letters/ when permissions conflict.

  3. Thanx seems to be very useful however I’m using Slax to access a damaged NTFS drive and am having trouble deleting files from that drive, i can read the files fine however i can’t change permissions for any of the files or folders and the drive is mounted with the ntfs -3g command so it should be editable. i’ve tried the chmod command without luck. any ideas?

    Thanks a ton in advance…

  4. rm -rf

    will remove it without any answer needed.

    rm -r without (-f for force) will request you a reply to remove each files.
    exemple:
    rm -r ntop-4.1.0
    rm: descend into directory `ntop-4.1.0′? yes
    rm: remove regular file `ntop-4.1.0/ntop_win32.h’?

  5. Is there a way to delete like every directory named a special name if so I have 19 directorys with the same name on my dedicated debian server and I want to delete all of them then it just searches for the names and deletes them?

  6. I have problem of Koha perl comiling…. when I put the command…. following …

    sudo perl Makefile.PL
    Can’t locate ZOOM.pm in @INC (@INC contains: /koha-latest/koha-3.08.04 /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at /koha-latest/koha-3.08.04/C4/Context.pm line 102.
    BEGIN failed–compilation aborted at /koha-latest/koha-3.08.04/C4/Context.pm line 102.
    Compilation failed in require at /koha-latest/koha-3.08.04/C4/Installer.pm line 24.
    BEGIN failed–compilation aborted at /koha-latest/koha-3.08.04/C4/Installer.pm line 24.
    Compilation failed in require at Makefile.PL line 31.
    BEGIN failed–compilation aborted at Makefile.PL line 31.

    Then showing this command

    What to do now… Pls give me …. msg

    Thanks.

    Syed Ahmed

  7. thank you so much I was about 30 seconds away from doing a hard restore on my iphone but luckily I found this forum first! I was desperate I could not find a way to remove my directory thanks to an archive that the program was unable to locate…the prgram was not installed but it was not complletely uninstalled and i could neither delete or load my cydia directory. Thanks so much!

    Just on a side note…anyone know what to do now that the hackulous team has disassembled??

  8. Thanks a lot!

    Another tip: you can use wildcards to remove many folders which have the same name at the start, E.G to remove all the folders “recup_dir.1” , “recup_dir.2”, “recup_dir.3″

    I would type ” sudo rm -rf recup_dir*** ” and all the folders disappear :) This was very useful as a file ‘undeleter’ left me with loads of these folders!

  9. thank u soooooo muchhhhhhhhh…:)its working…:) :) :)
    can anyone plzz help me with this..?? i need to change my directory and the dir name contains 3 words separated by spaces.. like “whole Project Mydhili”. I tried

    cd whole Project Mydhili

    but the result was :there is no such file or directory”. My path to the dir is correct and when i gave an ls command ,i can see this dir in the list…but am not able to make it as my pwd..!! i tried as super user (sudo) too..
    plzz if u could help me,i will b so thaankful to u.

  10. Hi,

    I have created a .trash hidden directory to get the removed files to use as recyclic bin by using the below command
    alias rm=’mv\!*~/.trash’
    Now I could not use rm -rf or rmdir command.

    Error message is coming as
    :bash= mv\!*~/.trash : directory or file not found.
    Can anyone please help me? in this. It will be very helpful to me.

  11. Hi,

    I’m trying to delete a directory Test2(e.g.: /home/apps/Test/Test1/Test2 by executing sh script.

    cd /home/apps/Test/Test1
    rm -rf Test2

    But if Test2 directory is already deleted then it throws error. I want to proceed further even if Test2 is not present. Can some one please help me out?

  12. Yesterday, I incorrectly extracted a soundfont to the following location : /usr/share/sounds/sf2/GeneralUser GS 1.44 FluidSynth.
    I would like to delete that last folder completely along with all contents and then install the soundfont correctly. I’ve tried almost every combination of the command line codes given above (rm -rf) but nothng seems to happen. I suspect I’m not typing in the command correctly. Any help would be appreciated.

    Regards Ramlee.

  13. Hi, I have a problem with this command :

    A file that contain “è” is recognised as “??” and when I try to delete it with the rm command (or rm -rf) I get the following error :

    rm: cannot remove ‘Wolf – La Belva ?? Fuori – 1994.avi’: No such file or directory

    Its not a directory but an avi file

    tried with the following :

    rm 'Wolf - La Belva ?? Fuori - 1994.avi'
    rm -r 'Wolf - La Belva ?? Fuori - 1994.avi'
    rm ./"Wolf - La Belva ?? Fuori - 1994.avi"
    rm -rf ./"Wolf - La Belva ?? Fuori - 1994.avi"
    

    nothing works :(

    Can someone please help me to find the correct command ? Thank you so much!

    Francesco

Leave a Comment