Why do directory listings contain the current (.) and parent (..) directory?

后端 未结 8 1164
面向向阳花
面向向阳花 2020-12-11 03:11

Whenever I list the contents of a directory with a function like readdir, the returned file names also include \".\" and \"..\". I have the suspicion that these are just nor

相关标签:
8条回答
  • 2020-12-11 03:20

    Because on Unix-like operating systems, the directory-listing commands include those, and you use them to move up and down in the filesystem hierarchy.

    Something like grep { not /^.{1,2}\z/ } readdir HANDLE should work for you.

    0 讨论(0)
  • 2020-12-11 03:20

    there is no good reason a directory scan should return these filenames.

    0 讨论(0)
  • 2020-12-11 03:26

    I have the suspicion that these are just normal links in the file system and therefore indistinguishable from actual files

    They are. While you may perceive the file system as a hierarchy of "folders" "containing" folders, it is actually a doubly linked tree1, with directories being nodes and files being leafs. So, . and .. are needed links for accessing the leaves of the current node and for traversing the tree, and they are the same thing as all the other links.

    When you call readdir, you get all the places you can directly go to from the current node. If you do not want to list places that you perceive as "up", you have to sort them out yourself. You should write a little function for that, perhaps called readdir_down. I do not know in which order readdir lists the directories, but perhaps you can just throw away the first two entries.

    1) this is a first approximation, there are also "hard links" possible that make the tree actually a net.

    0 讨论(0)
  • 2020-12-11 03:28

    One reason is that without them there is no way to get to the parent directory. Or get a handle to the current directory.

    Without them, we cannot do such things as:

    ./run_this
    

    Indeed, we couldn't add '.' to the $PATH, meaning we couldn't ever execute files that weren't already in the path.

    0 讨论(0)
  • 2020-12-11 03:29

    . and .. are actually hard links in filesystems. They are needed so that you can specify relative paths, based on some reference path (consider "../sibling/file.txt"). Since these hard links are actually existing in the filesystem, it makes sense for readdir to tell you about them. (actually the term hard link just means some name that is indistinguishable from the actual directory referred to: they both point to the same inode in the filesystem).

    Best way is to just strcmp and ignore them, if you don't want to list them.

    0 讨论(0)
  • 2020-12-11 03:35

    Originally they were hard links, and the number of special cases in the filesystem code for . and .. were minimal. That's not true for all modern filesystems, however.

    But the conventions have been established so that even filesystems where these two directory entries don't actually exist still report their existence through APIs like readdir. Changing this would now would break a lot of code.

    0 讨论(0)
提交回复
热议问题