Question

I have a general question as to why this occurs, and a misconception about 'pwd'.

You start with directory /test and in it you have /test/folder1.

Folder 1 has: file1.txt

In 2 separate terminals we "cd /test", and do an "ls" and discover folder1 as the output for both of these terminals.

We now "cd folder1" on terminal one. Terminal two remains in /test.

If we then "mv folder1 folder2" on terminal two and run an "ls" we get folder2 as the output. Clearly indicating our mv was successful.

However, within terminal 1 (which was in /test/folder1) if we run a "pwd" the output remains /test/folder1. Ie: it does NOT reflect that we have since moved the folder to /test/folder2.

Why is this the case? I can understand why if we were to edit the file1.txt it is just a pointer within the file system that should be pointing to the same file. Indeed it is as you can modify the file in each terminal and see the edits in the other. However, why is it the case that the 'pwd' command no longer reflects the actual path to that directory?

Thanks!

Was it helpful?

Solution

Assuming you're using bash, pwd is showing you the value of the PWD environment variable, which is updated when you change directory with cd. The folder1 directory changing name does not cause bash to update PWD. However you can find evidence that the directory has changed name:

  • pwd -P will show the new name of the directory.
  • ls -l /proc/self/cwd will link to the new name.

OTHER TIPS

I think it is just the case that the first terminal has no reason to re-evaluate where it is. If you do the following command in the first terminal

cd .

you will see your current working directory has indeed changed per the rename (mv).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top