在 Unix 中如何将目录读取为文件?

发布于 2024-08-09 04:09:11 字数 104 浏览 13 评论 0原文

我知道目录只是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花心好男孩 2024-08-16 04:09:11

由于这是一个编程问题(它一个编程问题,不是吗?),您应该检查 opendirreaddirclosedir 函数。这些是单一 UNIX 规范的一部分。

#include <sys/types.h>
#include <dirent.h>

DIR *opendir (const char *dirname);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);

dirent.h 文件应具有您需要的结构,至少包含:

char   d_name[]    name of entry
ino_t  d_ino       file serial number

请参阅 此处查看readdir联机帮助页 - 它包含其他链接。

请记住,存储在该文件的目录条目中的有关该文件的信息量是最少的。 inode 本身包含从 stat 函数获取的内容,例如时间、大小、所有者、权限等,以及指向实际文件内容的最重要的指针。

Since this is a programming question (it is a programming question, isn't it?), you should check out the opendir, readdir and closedir functions. These are part of the Single UNIX Spec.

#include <sys/types.h>
#include <dirent.h>

DIR *opendir (const char *dirname);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);

The dirent.h file should have the structure you need, containing at least:

char   d_name[]    name of entry
ino_t  d_ino       file serial number

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.

燃情 2024-08-16 04:09:11

在过去 - 版本 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.

感受沵的脚步 2024-08-16 04:09:11

它看起来像 stat命令可能是有序的。来自文章:

stat /etc/passwd

    File: `/etc/passwd'
    Size: 2911                Blocks: 8 IO    Block: 4096 regular file
    Device: fd00h/64768d      Inode: 324438   Links: 1
    Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
    Access: 2008-08-11 05:24:17.000000000 -0400
    Modify: 2008-08-03 05:11:05.000000000 -0400
    Change: 2008-08-03 05:11:05.000000000 -0400

It looks like the stat command might be in order. From the article:

stat /etc/passwd

    File: `/etc/passwd'
    Size: 2911                Blocks: 8 IO    Block: 4096 regular file
    Device: fd00h/64768d      Inode: 324438   Links: 1
    Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
    Access: 2008-08-11 05:24:17.000000000 -0400
    Modify: 2008-08-03 05:11:05.000000000 -0400
    Change: 2008-08-03 05:11:05.000000000 -0400
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文