Delete / Remove a Directory Linux Command

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

Everything is a file in Linux including the directory. A directory is nothing but a group of files. You can use the following commands to delete the directory in Linux. Directory is also known as a folder in the Macintosh OS X/macOS and Microsoft Windows operating system.

ADVERTISEMENTS

Commands to remove a directory in Linux

There are two command to delete a folder in Linux:

  1. rmdir command – Deletes the specified empty directories and folders in Linux.
  2. rm command – Delete the file including sub-directories. You can delete non-empty directories with rm command in Linux.

Let us see some examples and usage in details delete the directories.

rmdir command syntax to delete directory in Linux

The rmdir command remove the DIRECTORY(ies), if they are empty. The syntax is:
rmdir directory-name
rmdir [option] directory-name

Open the terminal application and run command to delete given directory. For example, delete a folder named dir1:
rmdir dir1

Delete directory Linux Command

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 message that read as follows:
rmdir letters
Sample outputs:

rmdir: letters: Directory not empty

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

Delete those files or directories. In this next 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,

  • -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.

How to see a diagnostic message for every directory processed

Pass the -v option to the rmdir command:
$ rmdir -v dir1
Sample outputs:

rmdir: removing directory, 'dir1'

Removing directories with rmdir and wildcards

We can use wildcards such as ‘*’ and ‘?’ to match and delete multiple directories. For example:
$ ls -l dir*
We have three dirs named dir1, dir2, and dir3. To delete all directories starting with ‘dir’ in the current, you would use the following command:
rmdir -v dir*
How to Remove (Delete) Directory in Linux

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

To remove all directories and subdirectories use the rm command. For example, remove *.doc files and all sub-directories and files inside letters directory, type the following command:

Warning: All files including subdirectories will be deleted permanently when executed the following commands.

$ rm -rf letters/
Sample session:
Delete directory Linux command
Where,

  • -r : Attempt to remove the file hierarchy rooted in each file argument i.e. recursively remove subdirectories and files from the specified directory.
  • -f : Attempt to remove the files without prompting for confirmation, regardless of the file’s permissions

Are you getting permission denied error message while removing directories?

Only owners can delete their directories. However, a sysadmin can delete any directories created by anyone on the system. The syntax is:
sudo rmdir /path/to/dir/
sudo rm -rf dir2

When prompted, you need to provide root user or sudo user password.

Use find command to delete unwanted directories

Say you want to find out all directories named ‘session’ and delete them in the current directory, run:
find . -type d -iname 'session' -delete

How to find and remove all empty directories

Run:
find . -type d -iname 'session' -empty -delete
Where,

  • -type d : Only search for directories and ignore all other files.
  • -iname 'session' : Search directory named ‘session’. You can use wildcards here too. For example, -iname 'dir*'.
  • -empty : Only match empty directories
  • -delete : Deletes all found empty directories only

To delete all ‘.DS_store’ directories stored in /var/www/html, run:
sudo find /var/www/html/ -type d -name .DS_Store -exec rm {} \;
OR
sudo find /var/www/html/ -type d -name .DS_Store -exec rm {} +
The -exec option to the find command run an external command named rm to delete all files. The “rm {} +/” is a better option as it uses one rm command to delete all .DS_Store directories.

Conclusion

This page showed how to delete a directory when it is empty. Further, it showed, how to remove folders using the rm and rmdir commands. See rm help page page for more info:

  • For more information read man pages: rm(1)
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
103 comments… add one
  • yogi Apr 28, 2008 @ 3:24

    very good
    i give five star

    • Sourav Aug 2, 2012 @ 7:36

      Getting error as : Permission denied

      • Chris Lentz Aug 10, 2012 @ 13:53

        Try running with ‘sudo’ command in front of it.

  • vishwa May 20, 2008 @ 10:25

    i tried it , very good can delete a large directory also.

    i give 5 stars

    • Kshitiz Singh Apr 14, 2012 @ 4:36

      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

      • Mark Oct 7, 2012 @ 18:03

        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.

  • Yury Oct 31, 2008 @ 11:05

    thank you

  • Arif Nov 6, 2008 @ 5:06

    Very effective

  • suveetha Nov 28, 2008 @ 11:45

    THANKS:)

  • manjunath Dec 8, 2008 @ 17:10

    thanks a lot

  • Ed Dec 10, 2008 @ 9:27

    rm: cannot remove `unixODBC//exe/.libs/odbcinst’: Permission denied

    I got loads of these messages, must I go through each file and change their permissions?

  • 🐧 nixCraft Dec 10, 2008 @ 9:34

    Run rm as root. Otherwise use chmod -R to change permission.

  • Ed Dec 10, 2008 @ 9:47

    thanks for the reply Vivek

  • krishna Feb 7, 2009 @ 10:32

    thanks yaar really works

  • raja Feb 10, 2009 @ 12:33

    Thanks for remove with sub directory cmd is rm -rf

  • ralph Feb 12, 2009 @ 16:42

    Awesome! great!

  • ghan Mar 4, 2009 @ 1:26

    Save my time. Very useful. Thanks a lot!

  • nguyen ngoc thanh Mar 19, 2009 @ 2:18

    very good!

  • hari Mar 25, 2009 @ 15:33

    Thanks ! its very useful.

  • phani Mar 30, 2009 @ 9:13

    thaks..a lot. i know the differnce between rmdir and rm -rf.

  • Nandlal Sahu Apr 1, 2009 @ 10:21

    Excellent
    i give 5 star

  • ganesh Apr 13, 2009 @ 5:19

    its very effective

  • Timon Apr 14, 2009 @ 19:49

    Thank you is very usefull post :)

  • Jordan May 8, 2009 @ 4:15

    Thank u very much it works properly

  • Sukhdeep Singh Jul 14, 2009 @ 12:51

    Thank You
    Very very useful post sir

  • anad Jul 15, 2009 @ 16:40

    worked for me

  • pravin Aug 7, 2009 @ 6:08

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

  • Max Aug 12, 2009 @ 12:19

    very useful info ! Thanks

  • Draco Aug 14, 2009 @ 23:11

    Thanks a lot! I needed it for work and didn’t want to take chance to mess sth up.

  • Juan Guillermo Aug 31, 2009 @ 5:11

    -r Stands for Recursive and -f Stands for Force

    Take care with the effect when using ‘-rf’ flags

  • amit Sep 1, 2009 @ 17:39

    thanks……….

  • traverlawre Oct 19, 2009 @ 2:52

    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.

  • Sujan Nov 24, 2009 @ 5:08

    Thanks traverlawre,
    ‘sudo’ did it. Without ‘sudo’ I had permission denied for many files that I wanted to remove.

  • Shady Dec 3, 2009 @ 13:43

    excellent article

  • siropio Dec 7, 2009 @ 14:28

    worked! thanks

  • d134 Dec 12, 2009 @ 20:45

    thanx a lot

  • lalish Feb 3, 2010 @ 5:56

    its niceeeeee

  • Rajive Mar 10, 2010 @ 8:01

    Thanks a lot bro

    Helped me a lot.

  • Tarun Apr 15, 2010 @ 9:35

    how to remove the directory with spaces.
    rm -rf “file name”
    it dosnt works.

    • akhil Sep 19, 2016 @ 2:34

      it’s worked to me.

  • kaptenvu Jun 23, 2010 @ 6:44

    try this :
    rm -rf file\ name\ with\ spaces

  • Shabbir Ahmed Jun 24, 2010 @ 9:53

    Hello!
    I am installing oracle10 database on linux (rh4em) plz can i have the easiest steps taht first time i can understand.

  • Guru Jul 29, 2010 @ 13:00

    Is any linux/unix command to delete one folder among 5 folders inside directory?
    Thanks with anticipation

  • Balaraju/Aniruddh Divy Tej Aug 21, 2010 @ 9:59

    information is most use full thanks for your help
    I appreciate your great help

  • Esonz Aug 23, 2010 @ 5:27

    Great! taynk yow!

  • Frederik Aug 26, 2010 @ 12:04

    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…

  • random Sep 25, 2010 @ 16:51

    Thanks a lot !!! very effective

  • dana Oct 30, 2010 @ 1:48

    You have been bailing me out countless of times. Thanks!
    :D

  • Miguel Costa Nov 9, 2010 @ 10:56

    :) tks a lot
    You really helped me ;)

    • sandeep choudhary Nov 10, 2010 @ 16:33

      i m unsetisfide

  • lara Dec 2, 2010 @ 3:47

    thanks 4 the nice idea about that!.. hehe

  • nagesh Dec 19, 2010 @ 8:42

    cool command…. simply works great,thanks

  • Rakesh Jan 4, 2011 @ 5:51

    Thank you

  • Pankaj Jan 27, 2011 @ 5:36

    Thanks…! it helps

  • Sivasangkari Feb 9, 2011 @ 8:27

    Thanks a lot…it’s very helpful…

  • Nisha Mar 31, 2011 @ 4:14

    Thanks a lot..

  • Duncan Murimi Apr 26, 2011 @ 12:17

    Very useful. for permission denied folders add sudo in front.

  • Marco May 18, 2011 @ 10:26

    thanks! save me some time as im new to linux!

  • Terryble Sep 8, 2011 @ 13:25

    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’?

  • Rahinur Rahaman Sep 14, 2011 @ 3:20

    Thanks!

    It works for me

  • Nitin Oct 26, 2011 @ 2:04

    Thanks :)

  • Raghavendra Nov 16, 2011 @ 7:34

    Please guide me If i have mutipal folders in same dir.
    like
    ls -x
    a
    b
    c
    d
    f
    Now I want delete ..a c d dir.How can I delete in singal command

    • syamkumar Dec 21, 2011 @ 6:03

      Thanks its very useful.

  • harga laptop Jan 23, 2012 @ 10:21

    What’s the command to find all files with extension tmp (ex: *.tmp) and then delete them automatically? Thanks

  • n0de Feb 16, 2012 @ 21:36

    very nice, worked like a charm keep up the good work ;)

  • Tirumal Feb 18, 2012 @ 12:24

    Thanks for info…! It was really helpful

  • Shashank Apr 3, 2012 @ 15:19

    thank a lot it’s working excellently

  • khalid Apr 26, 2012 @ 22:19

    thanks a lot save my time

  • Yousefzad May 16, 2012 @ 4:39

    Thank you very Much for the information, i found it so useful.

    thanks,

  • Jacob Jul 3, 2012 @ 13:29

    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?

  • Alireza Jul 13, 2012 @ 12:35

    Thank you.

  • Onneile Aug 27, 2012 @ 17:55

    Thanks, it worked for me

  • Syed Ahmed Sep 4, 2012 @ 4:24

    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

  • Mekbib Sep 10, 2012 @ 7:17

    Thanks i found it helpful.

  • Anoop Sep 11, 2012 @ 5:27

    It was really useful. Thank you

  • Varsha Katre Sep 15, 2012 @ 10:43

    Thank you very much!!! It worked!! :) :) :)

  • Amiya Sep 28, 2012 @ 15:43

    nice one..very help full info..

    i will give *****

  • Hamid Oct 15, 2012 @ 7:20

    دمت گرم اينم تو اونت
    Thank you very much. It’s very good!

  • Julio Nov 1, 2012 @ 19:26

    Thanks a lot, this was really helpful!

  • Martial Tchuente Nov 27, 2012 @ 8:52

    This definitely solved my problem.
    I’m still a Linux newbie.
    Thanks!

  • Nick Jan 3, 2013 @ 15:42

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

  • Tom G Feb 6, 2013 @ 23:32

    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!

  • Elrafie Mar 16, 2013 @ 20:38

    sudo rm -r /home/elrafie/recup_dir.1
    sudo rm -r /home/elrafie/recup_dir.2

  • xyz Jul 17, 2013 @ 7:13

    it worked…:)

  • mydhili warrier Jul 25, 2013 @ 7:42

    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.

  • Uncle Dec 7, 2013 @ 14:04

    Thanks Helpful Stuff

  • chatu Jan 29, 2014 @ 5:31

    to remove all the content in a dir

    commad :: rm -rf *

  • chatu Jan 29, 2014 @ 5:31

    to remove all the content in a dir

    commad :: rm -rf *

  • Jai Prakash Yadav Mar 3, 2014 @ 12:40

    I want to remove folder , pls suggest which command i can use..

  • Nik Mar 19, 2014 @ 15:43

    Thank you !!

  • Charlie May 15, 2014 @ 23:03

    here is another:
    mv folder /tmp
    reboot

    • felisbag Jan 22, 2015 @ 9:34

      Yes, it works. Thanks.

  • Venkat Jul 13, 2014 @ 21: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.

  • Ranjeet Jul 14, 2014 @ 15:26

    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?

  • Ramlee Aug 6, 2014 @ 1: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.

    • Christo Sep 12, 2017 @ 16:56

      Put the file name in single ‘q u-o.tes’.

  • shivaji Sep 11, 2014 @ 8:32

    Thank you for the guidance.

  • juvy Oct 2, 2014 @ 11:55

    what is the command on how to create and delete a folder in cent os 6 ? pls ans. thanks :)

  • Mozah Chisanger Nov 26, 2014 @ 6:34

    This so nice. Thank you!

  • Francesco Feb 27, 2015 @ 12:50

    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

  • Bhimashankar Mar 3, 2016 @ 3:21

    thanku sir

  • Dler Oct 15, 2016 @ 17:35

    Thank you, this worked!

  • andrii Sep 21, 2020 @ 13:29

    mistake in article
    rm command – Delete the file including sub-directories. You can delete non-empty directories with **rmdir** command in Linux.

    **rmdir** rmdir is not correct

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.