How to remove directory in Linux using rmdir and rm command

last updated in Categories , , , , ,

I am a new Linux user. How do I remove directory in Linux using the command line or terminal applications?

Introduction: Everything is a file in Linux including the directory. A directory is nothing but a group of files (or data). One can group files as per needs. For example, store documents in ~/Documents/ directory. You can use any one of the following commands to remove the directory in Linux (also known as a folder in the Macintosh OS X/macOS and Microsoft Windows operating system):

  1. rmdir command – Remove the specified empty directories
  2. rm command – Remove directories even if it is not empty

Let us see how to remove directories in Linux using the command line.

How to remove directory in Linux

Say you want to delete a directory named /tmp/foo, run:
rmdir /tmp/foo/
rmdir -v /tmp/foo/

Verify directory deleted from the system with help of ls command:
ls /tmp/
ls /tmp/data/

Please note that when attempting to remove a directory using the rmdir command, the directory must be empty. Otherwise, you might see an error message that read as follows on screen when execute rmdir -v /tmp/delta/:

rmdir: failed to remove '/tmp/delta/': Directory not empty

How do I remove a full directory in Linux?

As I said earlier rmdir command remove the DIRECTORY(ies) if they are empty. But, how do you delete a full directory that has many files and sub-directories? The solutions is to pass the -rf option to the rm command. The syntax is:
rm -r /path/to/dir/
rm -rf dir1
rm -rfv /path/to/dir
rm -rfv /tmp/delta/

Remove directory in Linux using rmdir and rm command
Where,

  • -r – Remove directories and their contents recursively
  • -f – Forceful option i.e. ignore nonexistent files and arguments, never prompt for anything
  • -v – Be verbose. Show what rmdir or rm command doing with given directory
  • -i – Prompt before every removal of file/dir
  • -I – Prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes. Useful when working on a large number of files on Linux

Conclusion

This page demonstrated how to delete both empty and non-empty directories along with all files/sub-directories using rm and rmdir command.

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

2 comment

  1. I typed “rmdir staticjs”. Linux cried and be unable to delete the directory:

    rmdir: 'staticjs': Directory not empty

    Maybe you can add?
    rm -r staticjs

  2. Hi,

    I got another error when I run rm -r foo

    rm cannot remove directory not empty

    Turn out I do not have permission to access that dir. The solution was to run it as

    sudo rm -r foo

    Maybe this will help someone.

    Cheers,

    Prajakta

    Have a question? Post it on our forum!