Why Do I Get "Directory Not Empty" When I Try to Remove an Empty Directory?

I have this empty directory, but I keep getting the following error message:

Cannot remove test: Directory not empty

I know this question has been asked plenty of times but none of them helped.

I tried ls -la to make sure there were no hidden files, and there does not seem to be:

I tried sudo rmdir test as well as sudo rm -rf test, and I just do not know what is wrong.

I read it might be a problem with the file system, but I have no idea how I would go about fixing that.

7

16 Answers

I had the same problem on a external hard disk, I tried so many ways using command line, but I failed every time. That's what worked for me:

  1. Right click on folder
  2. Move to trash
  3. Empty trash

Yes, it's silly but it worked for me (I don't really know how and why, but the damned folder no longer exist)

9

I have win 10 + ubuntu dual system installed. And both systems share the windows parititions.

Recently, i also ran into unable to delete empty folders in those partitions under ubuntu. I can't find out solution to solve it under linux.

However, after i switch to windows, and run

chkdsk

via cmd for the target disk. Some errors checked out. and then i run

chkdsk /F

to fix disk error.

After it finish, i am able to delete those folders now.

2

You could delete it by typing sudo rm -rf {dir_name}. The directory might have been set to read-only permission. I hope the given command can delete the folder.

4

I had the same issue not able to remove directory as it is not empty.

This sequence of operations worked for me.

  1. From command line first

    sudo rmdir  --ignore-fail-on-non-empty folder-name-to-be-deleted
    

The above command helps ubuntu ignore directory is not empty.

  1. Then just go to the folder and Shift + Del. That is all.

If the directory is part of a filesystem mounted with CIFS (aka samba), and it contains a file that is a broken symbolic link, then ls fails to mention that file. (I observe this bug on a CIFS client running 14.04.2 LTS, and a server running 12.04.5 LTS.)

So the directory is not empty, but (over CIFS) you have no way to see that. The file can only be seen, and thus can only be deleted, by a command running on the fileserver hosting that filesystem.

2

If you are using btrfs, it is possibly an empty directory with a non-zero i_size. You can check whether this is the case with:

stat -c %s test

The i_size of an empty folder in btrfs should be zero. In my case, I got 6160 with ~/.config/chromium/Default.

The suggested solution is to unmount the filesystem, run btrfs check to confirm the issue and check for other problematic directories, and finally run btrfs check --repair to fix. This operation is risky, though, so it's a good idea to backup files first.

Source: Btrfs Problem FAQ

GUI solution

  1. Move or cut & paste the folder to trash folder
  2. empty the trash

it is done.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.