开发者

C program: how to get parent directory's inode number?

How 开发者_StackOverflowto get directory inode number say /home/laks/file.txt I need the inode number of laks directory. Any built-in function is already available? I think i could use stat() if i cut the file name...but any other solution to this without removing file name.


#include <libgen.h>
#include <sys/stat.h>
...
struct stat statbuf;
if (stat(dirname(argv[1]), &statbuf) != -1)
    process_inode_number(statbuf.st_ino);

Note that dirname() may modify the string, so if you still need it, or if it may be a string literal (which is in read-only memory), then use strdup() to make a copy of the string for dirname().


a=/home/laks/file.txt
dir=${a%/*}
set -- $(ls -ldi $dir)
echo $1

or if you want to recurse directory

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


try : ls -ali on shell. You can find inode numbers on 3rd column.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜