在 Unix 中如何将目录读取为文件?
我知道目录只是 unix 中的一个文件,其中包含 inode 号和文件名。我该如何看待这个问题?我不能在目录上使用 cat 或 less,在 vi 中打开它只会显示文件列表...没有索引节点号。
I understand that a directory is just a file in unix that contains the inode numbers and names of the files within. How do I take a look at this? I can't use cat or less on a directory, and opening it in vi just shows me a listing of the files...no inode numbers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于这是一个编程问题(它是一个编程问题,不是吗?),您应该检查
opendir
、readdir
和closedir
函数。这些是单一 UNIX 规范的一部分。dirent.h
文件应具有您需要的结构,至少包含:请参阅 此处查看
readdir
联机帮助页 - 它包含其他链接。请记住,存储在该文件的目录条目中的有关该文件的信息量是最少的。 inode 本身包含从 stat 函数获取的内容,例如时间、大小、所有者、权限等,以及指向实际文件内容的最重要的指针。
Since this is a programming question (it is a programming question, isn't it?), you should check out the
opendir
,readdir
andclosedir
functions. These are part of the Single UNIX Spec.The
dirent.h
file should have the structure you need, containing at least:See here for the
readdir
manpage - it contains links to the others.Keep in mind that the amount of information about a file stored in the directory entries for it is minimal. The inode itself contains the stuff you get from the
stat
function, things like times, size, owner, permissions and so on, along with the all-important pointers to the actual file content.在过去 - 版本 7、System III、早期的 System V - 您确实可以打开一个目录并将内容读入内存,特别是对于具有 2 字节 inode 编号且文件限制为 14 字节的旧 Unix 文件系统姓名。
随着更多奇特的文件系统变得越来越流行,必须使用 opendir()、readdir()、closedir() 系列函数调用,因为解析目录的内容变得越来越不简单。
最后,在过去十年左右的时间里,它已经达到了在大多数系统上都无法读取目录的地步;您可以打开它(主要是这样 fchdir() 等操作可以工作),并且您可以使用 opendir() 系列调用来读取它。
In the old days - Version 7, System III, early System V - you could indeed open a directory and read the contents into memory, especially for the old Unix file system with 2-byte inode numbers and a limit of 14 bytes on the file name.
As more exotic file systems became more prevalent, the opendir(), readdir(), closedir() family of function calls had to be used instead because parsing the contents of a directory became increasingly non-trivial.
Finally, in the last decade or so, it has reached the point where on most systems, you cannot read the directory; you can open it (primarily so operations such as fchdir() can work), and you can use the opendir() family of calls to read it.
它看起来像
stat
命令可能是有序的。来自文章:It looks like the
stat
command might be in order. From the article: