为什么目录列表包含当前(。)和父(..)目录? [英] Why do directory listings contain the current (.) and parent (..) directory?
问题描述
更新:谢谢大家的回答。我想我一直认为像./和..这样的东西只不过是可以通过搜索和替换来处理的惯例。我觉得有点让人惊讶,虽然可能更高效和透明,让它们成为文件系统本身的一部分。
但是,有一个问题仍然存在:since。和..是这些链接的任意名称,是否有使用不同的文件系统的文件系统?
最好的办法是只要 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 normal links in the file system and therefore indistinguishable from actual files, but I always have to filter them out because they are not actual objects in the directory I am listing. Is there a good reason for functions like readdir to include them? Do some operating systems or file systems contain more or different virtual file names? Is there a better way to filter them out other than by doing string comparison with "." and ".."? Update: thank you all for answering. I suppose I always thought that things like ./ and ../ were mere conventions that could be handled by searching and replacing. I find it a bit surprising, though probably more efficient and transparent, to have them be part of the file system itself. One question remains, though: since . and .. are arbitrary names for these links, are there file systems that use different ones? Best way is to just 这篇关于为什么目录列表包含当前(。)和父(..)目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 。
和 ..
实际上是文件系统中的硬链接。他们是需要的,所以你可以指定相对路径,基于一些引用路径(考虑../sibling/file.txt
)。由于这些硬链接实际上存在于文件系统中,所以 readdir
可以告诉你这些链接。 (实际上,术语 hard link
只是表示一些与实际目录无法区分的名称:它们都指向相同的 inode $ c
strcmp
,并忽略它们,要列出他们。.
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).strcmp
and ignore them, if you don't want to list them.