4

Imaging the next simple file structure within the /home/user/ directory:

/home/user 
       |--dir0 
           |--dir1
               |--file1

My current directory is 'dir1' and I remove the directory from inside with the following command:

rm -r ../dir1

After that (and not getting any errors on the terminal), the working directory still is the same and when using the command pwd the output is:

user@ubuntu:~/dir0/dir1$ pwd
/home/user/dir0/dir1
user@ubuntu:~/dir0/dir1$

Why would the OS return that the working directory is 'dir1' if that was already removed from the filesystem?

jchanger
  • 401
  • 5
  • 14

4 Answers4

7

I think pwd you run was a bash shell built-in. It just printed out the path it held in memory without looking up the file system.

$ type pwd
pwd is a shell builtin
$ /bin/pwd
/bin/pwd: couldn't find directory entry in '..' with matching i-node
yaegashi
  • 12,108
  • 1
  • 36
  • 41
  • @Fabby I'm correct. See [the source of `pwd_builtin()`](http://sources.debian.net/src/bash/4.3-13/builtins/cd.def/#L466). It just prints the content of `the_current_working_directory`. – yaegashi Jul 31 '15 at 16:01
  • Looked at the source code and it seems you're right... What do you want me to do? Delete my answer? – Fabby Jul 31 '15 at 16:08
  • 2
    This answer (yaegashi’s answer, about`pwd` being a shelll builtin) is the correct answer to the question, but Fabby’s answer and unxnut’s answer (inode doesn’t get fully deleted until it’s no longer in use) also have some truth to them.  If you do `ls -lai` before the `rm` and `ls -ldi` after the `rm`, you’ll see that the current directory (the directory formerly known by the name `dir1`) still exists, with the same inode number, but with a link count of 0 and a size of 0 (and, accordingly, with no contents).  … (Cont’d) – G-Man Says 'Reinstate Monica' Jul 31 '15 at 17:36
  • 2
    (Cont’d) …  Also, if you do `df -i .` before and after the `rm`, you’ll see that `IUsed` (the used inode count) goes down by *n*, where *n* is the number of files in `dir1`.  After you `cd` out of the directory, `df -i` shows that `IUsed` has dropped by one more, since the directory inode has finally been deallocated. – G-Man Says 'Reinstate Monica' Jul 31 '15 at 17:37
  • **@G-Man**: as **yaegashi** is not responding: what's your opinion about this: Should I delete my answer so his moves to the top? (or edit his and include mine?) (or: *shudder* include his in mine?) – Fabby Jul 31 '15 at 19:36
  • What I do in cases like this is leave my answer, but add an edit pointing others to the correct answer below. – Dewi Morgan Jul 31 '15 at 23:51
7

Why can you continue viewing a movie although it's been deleted?

Because the file descriptor might be gone, but the inode is still there! And only when the inode gets deleted, is the file gone forever!

So in your case: when you cd to / and all files in that tree are closed will the inodes be recycled and will the directory be gone forever¹...

Remember: this is not Windows, this is a professional OS!

;-)

¹Unless it gets undeleted before it gets recycled.

Fabby
  • 5,836
  • 2
  • 22
  • 38
2

The OS doesn't return that the working directory is 'dir1', the shell does. The shell keeps track of the current working directory, and the 'pwd' command you're running is a command built into the shell. The shell is not aware of the fact that your 'rm' command removed the directory.

$ type pwd
pwd is a shell builtin

Try running /bin/pwd instead:

$ /bin/pwd
/bin/pwd: couldn't find directory in .. with matching i-node
Andy Dalton
  • 13,654
  • 1
  • 25
  • 45
2

The file (directory) is open. When you do rm it, the OS marks it as deleted without actually deleting it. In case you try to do cd into this directory from another shell instance, you will be denied the permission. After you cd out of this directory, the directory will actually get deleted.

unxnut
  • 5,908
  • 2
  • 19
  • 27