2

First, an example:

~ $ ls
~ $ mkdir foo; cd foo
foo $ pwd
/Users/person/foo
foo $ ls
foo $ touch file1 file2 file3
foo $ ls
file1    file2    file3
foo $ rm *
foo $ ls
foo $ rm -r ../foo
foo $ pwd
/Users/person/foo/
foo $ touch file4
touch: file4: No such file or directory
foo $

What happens when you delete the directory you are currently in? Why am I still in a directory that doesn't exist (I assume it doesn't exist solely because I cannot write into it)? I imagine it has something to do with a pointer of some sort.

Whymarrh
  • 13,139
  • 14
  • 57
  • 108
  • 1
    See http://stackoverflow.com/questions/2028874/what-happens-to-an-open-file-handler-on-linux-if-the-pointed-file-gets-moved-de -- does this answer your question? (Not much of a difference in how this is handled between files and directories.) – rodion Sep 10 '12 at 21:19
  • @rodion It helps, but this particular scenario I don't understand. – Whymarrh Sep 10 '12 at 21:21

1 Answers1

6

Your shell process has that directory as its current directory, which keeps its inodes on disk allocated in much the same way as an open file keeps its inodes allocated after unlinking, until all processes holding an open handle to that file close them.

lanzz
  • 42,060
  • 10
  • 89
  • 98
  • So if it is still on disk (even temporarily) why can I not write into it? – Whymarrh Sep 10 '12 at 21:27
  • 3
    @Whymarrh, it is not really *on disk* anymore. Like a zombie process, it only waits for its last descriptors to be closed (including the one you're holding through your shell's current directory), but it's already dead so you cannot do anything with it anymore, including creating new files. – Frédéric Hamidi Sep 10 '12 at 21:31
  • 1
    (previous comment deleted after some testing) Actually it seems that the kernel actually checks if the _current directory_ is deleted and disallows creation of files in it, to avoid having to delete them once the deleted directory is released for deallocation. Tested by creating _another_ directory with the same name as the deleted one, while still keeping the deleted as current — new files did not go to the new directory but were rejected. – lanzz Sep 11 '12 at 06:59