How to remove directory in Linux using rmdir and rm command

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):

ADVERTISEMENTS

  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.

🐧 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 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
2 comments… add one
  • Fevi Mar 11, 2004 @ 13:34

    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

  • Prajakta May 13, 2011 @ 16:42

    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

Leave a Reply

Your email address will not be published.

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