On Linux, when you a create folder, it automatically creates two hard links to the corresponding inode.
One which is the folder you asked to create, the other being the .
special folder this folder.
Example:
$ mkdir folder
$ ls -li
total 0
124596048 drwxr-xr-x 2 fantattitude staff 68 18 oct 16:52 folder
$ ls -lai folder
total 0
124596048 drwxr-xr-x 2 fantattitude staff 68 18 oct 16:52 .
124593716 drwxr-xr-x 3 fantattitude staff 102 18 oct 16:52 ..
As you can see, both folder
and .
's inside folder
have the same inode number (shown with -i
option).
Is there anyway to delete this special .
hardlink?
It's only for experimentation and curiosity.
Also I guess the answer could apply to ..
special file as well.
I tried to look into rm
man but couldn't find any way to do it. When I try to remove .
all I get is:
rm: "." and ".." may not be removed
I'm really curious about the whole way these things work so don't refrain from being very verbose on the subject.
EDIT: Maybe I wasn't clear with my post, but I want to understand the underlying mechanism which is responsible for .
files and the reasons why they can't be deleted.
I know the POSIX standard disallows a folder with less than 2 hardlinks, but don't really get why. I want to know if it could be possible to do it anyway.
rmdir somefolder/.
. I'm not on the branch so I don't see a problem. The answer by Stephen Kitt is super interesting in this way :) – Fantattitude Oct 18 '16 at 19:11rmdir .
refuses to do anything, as doesrmdir somefolder/.
, andrmdir ..
will always fail because the directory isn't empty. – Stephen Kitt Oct 18 '16 at 19:12..
, and at least EXT4 is still usable to some extent without.
(EXT2 possibly even more). – Stephen Kitt Oct 19 '16 at 05:32readdir
syscall returns, so it can decide to not return those special files. Of course, almost all FUSE filesystems do, because a) it's conventional and b) most FUSE filesystems (but not all) derive their readdir results from a readdir call to an actual filesystem which does return them. You can easily cobble together a Python script that creates a FUSE filesystem that returns any list of files you want when youls
a folder in the filesystem :) – Thomas Oct 19 '16 at 11:20