Linux Delete Folder Using rmdir and rm Command

How do I delete folders under Linux operating systems?

A folder is nothing but a group of files. The folder is also known as a directory in Linux. Everything is a file in Linux. You can delete the folder or directory in Linux, if they are empty using the following rmdir command. Otherwise you need to use the rm command.

Linux delete folder using rmdir

The syntax is

rmdir foldername
rmdir dirname
rmdir /path/to/folder/
rmdir /path/to/directory/

Let us see some examples that show how to remove a folder in Linux.

Linux remove folder

In this example, delete the directory called /tmp/letters/
rmdir /tmp/letters
For example, the following command would remove two empty folders named alpha and delta in the current directory:
rmdir alpha delta

How to get additional information about what is happening when running rmdir

Pass the -v (verbose) option as follows:
rmdir -v dir1
rmdir -v foo bar

Remove Folder and Its Ancestors

The -p option can delete directory and its subdirectories/sub-folders:
rmdir -p dir1/dir2/dir3
Where rmdir command command options are as follows:

  • -p : Linux remove folder i.e. remove the parent folders of the specified directory
  • -v : Output a diagnostic for every directory processed
  • --ignore-fail-on-non-empty : Ignore each failure that is solely because a folder is non-empty.

Delete All Files and Folders Including Subdirectories

Use the following syntax:
rm -rf /path/to/dir
For example, delete /home/vivek/docs and all its subdirectories including files, enter:

rm -rf /home/vivek/docs/
rm -rfv /home/vivek/dir2/
ls -l /home/vivek/docs/

Where rm command options are as follows,

  • -r : Recursively delete a directory by first deleting all of its contents
  • -f : Linux delete folder forcefully
  • -v : Verbose output

GUI File Manager

The Nautilus file manager (GNOME desktop) provides a simple and integrated way to manage your files and applications. Just open it from Places menu and select folder and hit delete key.

Fig.01: Gnome File Browser

Conclusion

In this tutorial, you learned how to delete folders in the Linux using rmdir and rm command-line options. The rmdir command is used to remove empty folders in Linux. The rm command used to delete both files and folders even if they are not empty. See rmdir help page here for more info.

🐧 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
3 comments… add one
  • Philippe Petrinko Nov 16, 2010 @ 16:15

    Look out Vivek,

    You have just invented 2 new commands : “mdir” and “m”
    (instead of “mkdir” (3 times) and “rm” (2 times).

    Philippe

    • 🐧 nixCraft Nov 16, 2010 @ 17:12

      Philippe,

      There is a bug in WP plugin that displays bash script and commands; it eats ‘r’ character when enclosed in formatting tags as follows:

      <pre lang="bash">rmdir foldername</pre>

      Which renders it as

      mdir foldername

      To avoid this I need to add an extra newline to command:

      <pre lang="bash">
      rmdir foldername
      </pre>

      This one renders correctly:

      rmdir foldername

      The plugin author stopped updating plugin and I’m not able to find out the bug on my own :( so some time we get this kind of weird problems. I’d like to thank you for pointing out this and while fixing this I found another bug because of your comment too. It was related to image display. I’ll buy you beer and pizza!

  • faaris Jul 17, 2015 @ 13:04

    How i can delete a folder in website.com/wp-content/plugins/pluginname

    hope to help

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.