Are the "." and ".." entries in a dir listing always the same?

13

2

When would the "." and ".." entries in a dir listing differ? (I understand they represent two different directories, but they always list identically with the same date and time in a default dir command. Do they ever differ?)

Witness Protection ID 44583292

Posted 2011-09-13T18:50:05.840

Reputation: 339

1Can you give an example (perhaps with a screen shot) – ChrisF – 2011-09-13T20:05:43.157

Answers

26

As to the actual listing:

27/08/2011  11:23    <DIR>          .
27/08/2011  11:23    <DIR>          ..

They're using the date of the current directory for both. If you start in C:\Users\Chris (say) you might get the above. But when you go up to C:\Users you get:

26/07/2011  21:20    <DIR>          .
26/07/2011  21:20    <DIR>          ..

So the date of .. in the first case is not the same as the date of . in the second. Which is apparently wrong as they are (or should be) the same directory. However, . and .. are references to the current and parent directory and are both created at the same time - when the directory was created - so it's actually correct (thanks to Synetech inc for pointing this out)

The only time the timestamps would be different is if one or other of . or .. were recreated.


The original answer:

. means the current directory.

.. means the parent directory.

So under normal circumstances they are always different.

The only time they yield the same result is when you are at the root of the disk. So at C:\> dir . and dir .. produce the same output.

ChrisF

Posted 2011-09-13T18:50:05.840

Reputation: 39 650

answer to the point, great. – ppuschmann – 2011-09-13T19:11:30.503

Edited to emphasize the question is regarding their appearance in a dir listing. – Witness Protection ID 44583292 – 2011-09-13T20:05:09.580

@mike - tried to answer the revised question to justify the votes ;) – ChrisF – 2011-09-13T20:15:08.697

4That’s wrong, because .. not given the timestamp of the parent directory, it is given the timestamp of the current directory. This is because . and .. are both created when the directory is created. – Synetech – 2011-09-13T22:06:05.367

@Synetech - Ah! The penny drops – ChrisF – 2011-09-13T22:09:53.207

I suspect they might also be different if a clock tick happened to take place between the creation of the . entry and the .. entry. We'd have to look at the code to be sure, though. – Harry Johnston – 2011-09-15T03:01:59.353

14

No, they will always be the same. Because these are directories, not files, they are handled slightly differently (in fact, they are not even normal directories, they are pointers as eL01 said, so they are handled even more differently than normal directories).

When you create a directory, two entries are automatically created:

  • . points to the newly created directory
  • .. points to the the new directory’s parent

Obviously . will have the current date and time that the directory is created, and while it may seem logical for the timestamp of the parent directory to be copied to .., that is not how it works. When you create a new directory, both of the pointers receive the current date and time. This is the case from DOS through Windows 7 on both FAT* and NTFS.

Synetech

Posted 2011-09-13T18:50:05.840

Reputation: 63 242

1+1 since it seems you were the one coming up with the answer ChrisF is now presenting ;-) – Jonas Heidelberg – 2011-09-13T22:40:29.367

@Jonas, actually, ChrisF’s answer makes the same mistaken assumption that most people would: that .. would have the timestamp of the parent. (I would not be surprised if some day, a new file-system—WinFS?—were to finally do it that way). – Synetech – 2011-09-13T23:01:56.167

Starting with his 4th revision I'd say he doesn't make that assumption any more.

– Jonas Heidelberg – 2011-09-13T23:15:55.957

@Jonas, ah okay. I didn’t see the edits. Oh well, he’s getting new up-votes anyway; I guess that’s the point to the edit function. – Synetech – 2011-09-14T02:23:32.433

I should have credited you in that latest revision - I'll do so now. – ChrisF – 2011-09-14T08:04:22.577

For the record, I just checked on my Linux box and you do get different timestamps. – UncleZeiv – 2011-09-14T09:02:02.543

Thanks guys. In any case, the point is that they are the same because they are special cases and handled differently. (I just checked and apparently WinFS was formally cancelled! I was looking forward to it to; it really would have helped with file organization.) :-(

– Synetech – 2011-09-14T18:36:57.977

4

I actually can't give you proof, but I think: Every directory has a list of directories and files it contains. To make it possible to use relative paths every directory needs those two pointers - one to itself . and the other directory one level above .. - those two pointers are just created on the time the directory itself is created.

So the timestamp of .. is actually not the timestamp of the directory above but the timestamp of the pointer to the directory above.

eL01

Posted 2011-09-13T18:50:05.840

Reputation: 141

That is correct. The .. entry is given the timestamp of the current date and time when the current directory is created. – Synetech – 2011-09-13T22:06:26.600