Delete directory indirectly inside of itself

I have a directory inside itself. How do I delete it.

~/.local/share/Trash/files$ ls devices/
reg-dummy
~/.local/share/Trash/files$ ls devices/reg-dummy/
subsystem
~/.local/share/Trash/files$ ls devices/reg-dummy/subsystem/
devices

Also

~/.local/share/Trash/files$ find devices/ | head -n 20
devices/
devices/reg-dummy
devices/reg-dummy/subsystem
devices/reg-dummy/subsystem/devices
devices/reg-dummy/subsystem/devices/reg-dummy
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices
devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy/subsystem/devices/reg-dummy

Also, although my brain can't solve the halting problem, it appears that sudo rm -rf devices goes on forever without producing output.

~/.local/share/Trash/files$ sudo rm -rf devices
^C~/.local/share/Trash/files$

Same thing for perl -e 'use File::Path qw(remove_tree); remove_tree("$ENV{HOME}/.local/share/Trash/files/devices")'. Same thing for du -s devices/. Same thing for du -sch ~/.local/share/Trash/ Other commands

$ cd ~/.local/share/Trash/files/devices/reg-dummy/subsystem/devices/
$ ls -ldi 
8131921 drwxr-xr-x 3 theking theking 4096 Mar 17 19:43 .
$ cd reg-dummy/subsystem/devices/
$ ls -dli
8131926 drwxr-xr-x 3 theking theking 4096 Mar 17 19:43 .


$ find .local/share/Trash/files/ -maxdepth 1 -delete
find: cannot delete `.local/share/Trash/files/devices': Directory not empty
find: cannot delete `.local/share/Trash/files/': Directory not empty

I don't want it stuck in my trash forever!

Note: I was making a crude backup of a computer by simply using scp, and but I ran out of space and then this happened.


Solution 1:

The output of ls -ldi will show the inode number of the directory. If the directory within the directory really has the same inode number as its ancestor, rather than just the same name, then your filesystem is corrupt and you will need to boot into rescue mode and fsck it.

Solution 2:

I still think that rm -rf will work if you give it enough time but if not, one or both of these should:

perl -e 'use File::Path qw(remove_tree); 
        remove_tree("$ENV{HOME}/.local/share/Trash/files/devices")'`

 

find .local/share/Trash/files/ -delete

You can make sure that something is happening if you use rm -rfv ~/.local/share/Trash/files/devices at least that will let you know that files are being deleted.

Anyway, this can't be a hardlink problem (despite my very wrong comment) because directories can't be hardlinked under Linux. In general, when you have infinite recursion, as can happen with softlinks, you will get a message to that effect, that does not appear to happen.

The other possibility I can think of is that the Trash folder is some strange system of its own. I don't really know how it works, I never use it. However, you might have better luck deleting the top level files directly instead of targeting the problematic directory:

rm -rf ~/.local/share/Trash/* 

You should also try emptying the Trash folder from the GUI, just select Trash and click on "Empty Trash", see if that works.