c - 给定目录名,如何在 C 中找到它所在的文件系统?

标签 c linux filesystems directory mount

例如,示例 df 命令输出为

Filesystem    MB blocks      Free %Used    Iused %Iused Mounted on
/dev/hd4         512.00    322.96   37%     4842     7% /
/dev/hd2        4096.00    717.96   83%    68173    29% /usr
/dev/hd9var     1024.00    670.96   35%     6385     4% /var
/dev/hd3        5120.00      0.39  100%      158    10% /tmp

现在,如果我指定类似于 /tmp/dummy.txt 的内容,我应该能够获得 /dev/hd3 或只是 hd3

编辑:感谢 torek 的回答。但是探测 /proc 会变得非常乏味。谁能建议我一些可以在内部执行相同操作的系统调用?

最佳答案

df `pwd`

... super 简单,有效,还会告诉您有多少空间...

[stackuser@rhel62 ~]$ pwd
/home/stackuser
[stackuser@rhel62 ~]$ df `pwd`
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda7            250056240 196130640  41223408  83% /
[stackuser@rhel62 ~]$ cd isos
[stackuser@rhel62 isos]$ pwd
/home/stackuser/isos
[stackuser@rhel62 isos]$ df `pwd`
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda5            103216920  90417960  11750704  89% /mnt/sda5
[stackuser@rhel62 isos]$ df $(pwd)
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda5            103216920  90417960  11750704  89% /mnt/sda5

...这可能首先是挂载点查询的原因。

请注意,这些是反引号,另一种(现代)方法可进一步控制斜杠和扩展,是 df $(pwd)。在 bashdashbusyboxzsh 上测试并正确遍历符号链接(symbolic link)。请注意,tcsh 不喜欢 $(...),因此请坚持使用 csh-variants 中旧的反引号样式。

pwddf 中还有额外的开关供您进一步享受。

关于c - 给定目录名,如何在 C 中找到它所在的文件系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9616578/

相关文章:

linux - 如何加快Linux上一组固定小文件的读取速度?

c# - Filesystemwatcher 和 GAC

c - 将节点添加到二叉搜索树

c++ - 是否允许将指向模板函数的指针传递给 C 库? (作为回调)

regex - 对于带有连字符的域,正则表达式不起作用

c - 使用 epoll 时 timerfd 将无法读取

c++ - 检查存在性后,unlink() 失败并显示 "No such file or directory"

c - 使用 getchar() 读取整数

c - 如何在 C 中读取由 { , , } 格式分隔的数字的整个文件?

PHP:从 cron 运行脚本时路径不正确