Why can't I remove the '.' directory?
Removing the current directory does not affect the file system integrity or its logical organization. Preventing
. removal is done to follow the POSIX standard which states in the
rmdir(2) manual page:
If the path argument refers to a path whose final component is either dot or dot-dot, rmdir() shall fail.
One rationale can be found in the
rm manual page:
The rm utility is forbidden to remove the names dot and dot-dot in order to avoid the consequences of inadvertently doing something like:
rm -r .*
On the other hand, explicitly removing the current directory (i.e. by stating its full or relative path) is an allowed operation under Unix, at least since SVR3 as it was forbidden with Unix version 7 until SVR2. This is very similar to what happens when you remove a file that is actively being read or written to. Processes accessing the delete file continue their read and write operations just like if nothing happened. After you have removed a process current directory, this directory is no more accessible though its path but its inode stay present on the file system until the process dies or change its own directory.
Note that the process won't be able to use a path relative to its current directory to change its cwd (e.g.
cd ..) because there is no more a
.. entry in its current directory.
When someone type
rmdir ., they likely expect the current directory entry to be removed but when a directory is removed (using its path), three directory entries are actually removed,
.., and the directory itself.
. and not this directory's directory entry would create a non compliant directory but as already stated, it is forbidden by the standard.
As @Emmanuel rightly pointed out, there is a second reason why removing
. is not allowed. There is at least one POSIX compliant OS (Mac OS X with HFS+) that, with strong restrictions, supports creating hardlinks to existing directories. In such case, there is no clear way from inside the directory to know which hardlink is the one expected to be removed.
It's done like that for integrity since you are currently inside that directory and the
. is only a self-reference.
You need to either go in its parent or call
rmdir with its path, which can be done with:
If you often need that, you can set an alias to it like:
alias rmc='rmdir `pwd`'
.. which could be called as
rmc alone to remove current directory.