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:
[email protected]:~/dir0/dir1$ pwd
/home/user/dir0/dir1
[email protected]:~/dir0/dir1$
Why would the OS return that the working directory is ‘dir1’ if that was already removed from the filesystem?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
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.
Method 3
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
Method 4
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0