【问题标题】:C program: how to get parent directory's inode number?C程序:如何获取父目录的inode号?
【发布时间】:2011-01-10 06:58:08
【问题描述】:

如何获取目录 inode 号说 /home/laks/file.txt 我需要 laks 目录的 inode 数量。任何内置功能已经可用? 我想如果我删除文件名,我可以使用 stat() ......但是任何其他解决方案都可以在不删除文件名的情况下使用。

【问题讨论】:

  • 是的,这个有效 ---- #include #include main(){ struct stat statbuf; char *ff="/home/laks/file.txt"; if (stat(dirname(strdup(ff)), &statbuf) != -1) printf("\n %ld",statbuf.st_ino); } ----- 谢谢大家。

标签: linux inode


【解决方案1】:
a=/home/laks/file.txt
dir=${a%/*}
set -- $(ls -ldi $dir)
echo $1

或者如果你想递归目录

find /path -type f -name "*.txt" -printf 'stat -c "%%n:%%i" "%h"\n' | sort -u |bash

【讨论】:

  • 感谢您的回答。是否可以使用 C API 做同样的事情?
  • 抱歉,错过了编程语言。希望有人能尽快回复您。
  • 不是你的错。我在您回复后才编辑标题。所以这是我的错误:)
【解决方案2】:
#include <libgen.h>
#include <sys/stat.h>
...
struct stat statbuf;
if (stat(dirname(argv[1]), &statbuf) != -1)
    process_inode_number(statbuf.st_ino);

注意dirname()可能会修改字符串,所以如果你仍然需要它,或者它可能是字符串文字(在只读内存中),那么使用strdup()复制字符串为dirname()

【讨论】:

  • 感谢我尝试以这种方式运行 #include #include main(){ struct stat statbuf; if (stat(dirname("/home/laks/file.txt"), &statbuf) != -1) printf("\n %lld",statbuf.st_ino);但它核心转储 - 任何想法
  • 如果您使用的是字符串文字,请使用 strdup。即strdup("/home/laks/file.txt")
【解决方案3】:

尝试:ls -ali 在外壳上。您可以在第三列找到inode 号码。

【讨论】:

    猜你喜欢
    • 2014-07-27
    • 2014-07-25
    • 2013-01-26
    • 2011-06-04
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    相关资源
    最近更新 更多