Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Recently I was very frustrated with xterm not behaving as expected. This is what I was doing:

  1. open xterm
  2. run the following commands:

    $ mkdir test_01
    $ cd test_01/
    $ echo 'a' > a
    $ cat a
    

    The output will be:

    a
    
  3. open another xterm (don't close the first one)

  4. run the following commands:

    $ mv test_01/ test_01_old
    $ mkdir test_01
    $ cd test_01
    $ echo 'b' > a
    $ cat a
    

    The output will be:

    b
    
  5. now in the original xterm run the command:

    $ cat a
    

    The output will be:

    a
    

Why does this happen? At this stage, both xterms report the same directory with the pwd command (which is /home/user_1/test_01).

share|improve this question
1  
This is probably not xterm-related. (An xterm can't tell if it is running a shell or nethack and has no idea what directory the shell it runs considers the cwd.) Btw, while pwd might be the same, ls -ld /proc/self/cwd of both Xterms probably differs. –  sr_ Aug 10 '12 at 8:32
    
sr_ Thanks for the edits! Looks much nicer) And you are right! It actually is different, the output shows that the directory in reality moved. –  Valera Rozuvan Aug 10 '12 at 8:53
    
cd . will "sanitize" the situation in the first xterm. After this pwd will show the new path. –  janos Aug 10 '12 at 9:17
add comment

1 Answer 1

up vote 12 down vote accepted

This has little to do with xterm. You could do the same thing with two shells without invoking xterm at all. For that matter, you could do it with one shell (see below).

Each process has a current working directory. This isn't tracked by name, but as a pointer (more or less) to the directory itself. (I'm not sure how it's represented internally; it may be something like the major and minor device numbers and the inode number for the directory.)

The shell running in your first xterm has test_01 as its current directory. That directory is then renamed (by another process) from test_01 to test_01_old -- but it's still the same directory, and the shell process still has it as its current directory.

The kernel doesn't remember the name of the current directory, but your shell does. It uses this information when you run the built-in pwd command. The shell under your first xterm didn't notice that directory was renamed, so when you type pwd it prints the cached path.

But /bin/pwd is an external command, and it doesn't have access to the shell's cached information. It works by starting from the current directory (whose name it doesn't immediately know), looking at its .. entry, and traversing up the directory hierarchy until it gets to the root (i.e., the directory whose .. entry points to itself); it then prints the path elements in reverse order delimited by / characters.

For example, I just did the following on my system (Ubuntu 12.04, bash 4.2.24):

$ pwd ; /bin/pwd
/home/kst
/home/kst
$ mkdir test_01
$ cd test_01
$ pwd ; /bin/pwd
/home/kst/test_01
/home/kst/test_01
$ mv /home/kst/test_01 /home/kst/test_01_old
$ pwd ; /bin/pwd
/home/kst/test_01
/home/kst/test_01_old
$ cd $(/bin/pwd)
$ pwd ; /bin/pwd
/home/kst/test_01_old
/home/kst/test_01_old
$ 

As you can see, pwd and /bin/pwd are consistent until I rename the current directory; then the shell's built-in pwd prints what it remembers the current directory to be. But when I do cd $(/bin/pwd), the two are in synch again.

share|improve this answer
    
Thanks for clarifying that! Also, I forgot that the shell might have built-in replacements for some commands. –  Valera Rozuvan Aug 10 '12 at 8:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.