8

I am running a program that creates a bunch of files in a certain directory, and I want to watch the files get created.

I open two terminal windows and cd one of them (call it terminal A) to the directory of the program (so I can run it) and the other (terminal B) to the directory where the output files get written (this output directory starts out empty). When I touch a file in the output directory from terminal A then ls in terminal B, the new file appears -- all this behaves normally.

However, after I run the program in terminal A, none of the new files show up when I do ls in terminal B. Strangely enough, if I do cd . then ls in terminal B, the new files now get listed.

What is causing this behavior, and can I get around it?

Edit: Information about what is writing the files.

  • Some are being written by calls to cv2.imwrite(...) in Python 2, using OpenCV.
  • Some are being written by an ofstream in C++.
9
  • Could you include a minimal reproducible example (your code)? We can only guess without having the hard facts. Commented Jul 12, 2016 at 18:28
  • Unfortunately, I'm not able reproduce this bug with anything else but this program I'm running, which is a large and confidential collection of code.
    – vijrox
    Commented Jul 12, 2016 at 18:30
  • 1
    Then we won't be able to reproduce this bug, which is a reason for closure. Can you at least list the pertinent commands used in the script? Commented Jul 12, 2016 at 18:31
  • Thanks -- I'll find what I can and edit it into the question
    – vijrox
    Commented Jul 12, 2016 at 18:34
  • 1
    Could it be a duplicate of this: superuser.com/questions/702402/….
    – Simon
    Commented Jul 12, 2016 at 18:38

2 Answers 2

15

This sequence of events seems to reproduce the issue.

image

Your program in terminal A probably deletes terminal B's current directory and then recreates it with the same name, so ls doesn't work since that particular directory that was originally cd'd to by terminal B doesn't exist anymore. However, cd . brings you to the (now) re-created directory, at which point ls works again.

1
  • Looks like that's exactly what's happening -- thanks!
    – vijrox
    Commented Jul 12, 2016 at 18:42
4

This will happen if your second directory gets deleted and recreated. Even if directory is deleted, but some process has it as current directory, file descriptor for it will remain open, and ls will show old content. Executing cd . will force to close descriptor for now non-existent directory and reopen it again, now showing new content.

1
  • 1
    Very helpful tidbit re: file descriptors, thanks for sharing this. Commented Apr 2 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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