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.

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*

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:

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)
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 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 VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
103 comments… add one
  • 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. Required fields are marked *

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