Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two windows open on the same machine (Ubuntu 9, ia32, server). I'll call these windows W1 and W2.


$ cd ~/test  
$ ls  

In W2 I run "make" from a parent directory that recreates file test/sample:

$ make project
$ cd test
$ ls 

Now, returning to W1:

$ ls
$ cd ../test
$ ls 

In other words, after I build from another window and the file test/sample is replaced, ls shows the file as missing in the 2nd window until I cd ../test back into the directory whereupon it reappears.

I can give more details if required, but just wondering if this is a well-known behavior.

share|improve this question

migrated from Feb 15 '11 at 22:22

This question came from our site for professional and enthusiast programmers.

up vote 2 down vote accepted

Do this experiment, and you'll see what's going on:

W1: cd /tmp
W1: mkdir foo
W1: cd foo
W1: rmdir ../foo
W1: ls
W1: touch ./bar

W2: cd /tmp
W2: mkdir foo
W2: cd foo
W2: touch bar
W2: ls

W1: ls
W1: touch bar

Basically, when the working directory a process is in is removed the system can't just randomly change its working directory for it. That would be a huge security hole. So it gives it sort of a fake working directory that can be read (giving 0 bytes) and various other operations, but cannot largely be used. It will happen to have the same name as a new directory created in the same place, but that's superficial.

If you do lsof | fgrep $$ after the directory is removed you will see an entry like this:

bash   2924   a_user   cwd   DIR   253,17   48   1327431   /tmp/joe (deleted)
share|improve this answer
Thanks for this excellent response; indeed I thought only the file was removed but you must be right that the directory was removed and recreated by make leaving my shell stranded. And the lsof illustrates your point beautifully. – Fixee Feb 15 '11 at 22:37
One last comment: I understand that we wouldn't want a randomly-reassigned cwd as a result of a dir being deleted out from under us. But I think an error msg like current directory invalid would be better than this "fake directory" solution. ALthough I guess at this point, behavior like this would break 40 yrs of backward compatibility. – Fixee Feb 16 '11 at 17:37
@Fixee - Well, I don't completely agree. This fake directory situation is roughly analogous to what happens when a file is deleted while some process has it open. It's not actually deleted until the process closes it. The 'current working directory' is just like a file that's still open, except that you can't create new entries in it, whereas with a regular open and deleted file you can still modify it and even append to it. – Omnifarious Feb 20 '11 at 1:45
you can put the shell back to where it should be via cd "$PWD" – Dan D. Mar 8 '11 at 11:07
or you can use cd $(pwd) – Brad Parks Jan 25 '13 at 20:28

You have (resp. your shell has) a file descriptor open to a directory whose nlink count has been reduced to zero (probably — you left out the most important trace of all: from make) — but of course the object lives on as long as a reference is held:

/dev/shm$ md z
/dev/shm$ cd z
/dev/shm/z$ rd ../z
/dev/shm/z$ ls -al
total 0
/dev/shm/z$ ls -dli .
9347030 drwxr-xr-x 0 me users 40 Feb 15 21:59 .

Recreating /dev/shm/z creates a new entity.

/dev/shm/z$ md /dev/shm/z
/dev/shm/z$ ls -dli .
9347030 drwxr-xr-x 0 me users 40 Feb 15 21:59 .
/dev/shm/z$ ls -dli /dev/shm/z
9350877 drwxr-xr-x 2 me users 40 Feb 15 22:03 /dev/shm/z

All well-defined behavior.

share|improve this answer
Well, not exactly a file descriptor. Only sort of. The current working directory is sort of a descriptor-like resource a process can own. – Omnifarious Feb 15 '11 at 21:09

Your Answer


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.