How do I remove a directory in Unix?

I am a new macOS Unix user. How do I remove a directory in Unix using the terminal applications?

Introduction: Everything is a file in Unix including the directory. A directory is nothing but a group of files (or data). One can group files as per our needs and setup. For example, I can store all of my videos in ~/Videos/ directory. You can use any one of the following commands to remove the directory in Unix (also known as a folder in Microsoft Windows operating system):
  1. rmdir command – Remove the specified empty directories on Unix
  2. rm command – Remove directories even if it is not empty in Unix

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

How do I remove a directory in Unix using the CLI?

The syntax is
rmdir dirNameHere
First, open the terminal Application. Say you want to remove a directory named banking-data in the home directory, run:
rmdir banking-data
rmdir $HOME/banking-data
Verify directory deleted from the system with help of ls command:
ls -l banking-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 'banking-data': Directory not empty

How to delete a directory in Unix even when it is not empty

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 -r option to the rm command. The rm command syntax is as follows to delete a directory in Unix:
rm -r /path/to/dir/
rm -rf myDirName
rm -rfv /path/to/dir
rm -rfv ~/foo/
rm -ri myDirName


  • -r – Remove directories and their contents recursively on Unix
  • -f – Forceful option i.e. attempt to remove the files without prompting for confirmation, regardless of the file’s permissions.
  • -v – Be verbose. Show what rm command doing with given directory/file names on Unix
  • -i – Request confirmation before attempting to remove each file, regardless of the file’s permissions, or whether or not the standard input device is a terminal.


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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig 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
2 comments… add one
  • Tom Nov 12, 2013 @ 11:12

    Yes, this solved my problem. My directory was full. rmdir said can’t delete but rm -r dir1 did the job.

  • Mukundan Alayil Oct 12, 2020 @ 3:46

    Thanks. This helped.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum