I am trying to remove a directory named data in my $HOME. However, I am getting an error that read as ‘rmdir: data/: Directory not empty‘. How do I delete a non-empty directory in bash terminal running on Linux operating system?

Yes, you can use any one of the following command to delete all the files in the directory and remove the directory itself including all files on Linux operating system.

Open the Terminal app and type the following command.

How to delete a non-empty directory in bash shell?

The syntax is (be careful all files will be deleted forever):
rm -rf {dirName}
rm -rf {/path/to/dirName}

For example, the following will deletes all files and sub-directories contained in the movies directory your home directory. Run rm command:
rm -rfv movies
rm -rfv $HOME/movies

If you get an error that read as ‘Permission denied’, try:

In case if you do not have the permission to delete the directory run command as root user using the sudo command:
sudo rm -rfv movies
sudo rm -rfv $HOME/movies
sudo rm -rfv /path/to/data/dir/
Sample session:

Fig.01: How to delete a non-empty directory in Linux bash shell terminal?

Fig.01: How to delete a non-empty directory in Linux bash shell terminal?

Understanding rm command options

  1. -r – Attempt to remove the file hierarchy rooted in each file argument i.e. recursively delete all files and sub-directories.
  2. -f – Force file delete operation.
  3. -v – Be verbose when deleting files, showing them as they are removed.

The rmdir command delete directory (folder) provided it is empty. Use rm command to delete the non-empty directory on Linux.

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

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
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
1 comment… add one
  • Jouni "rautamiekka" Jrvinen May 26, 2017 @ 2:16

    Be damn sure you don’t have a Space after the leading slash in a sudo rm -r ! Bash and probly others should be blocking you from deleting the filesystem root like that, but it’s nothing guaranteed, it’s quite inconsistent in Bash.

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