Linux Delete Folder Using rmdir and rm Command

last updated in Categories , , , , , , , , ,

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

Linux Delete Folder Commands

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 To Delete Folder in Linux
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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

3 comment

  1. Look out Vivek,

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

    Philippe

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

    Still, have a question? Get help on our forum!