Delete a non-empty directory when you get directory not empty error on Linux

Posted on in Categories , , , , , last updated May 24, 2017

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
OR
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
OR
sudo rm -rfv $HOME/movies
OR
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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

1 comment

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