C: how to get the index of the parent directory?

How do I get the inode number of the say / home / laks / file.txt directory I need the inode laks number. Any built-in feature is already available? I think I could use stat () if I cut the file name ... but any other solution for this without deleting the file name.

+5
source share
2 answers
#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 it dirname()can change the string, so if you still need to, or if it can be a string literal (which is in read-only memory), use strdup()to make a copy of the string for dirname().

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

or if you want to overwrite the directory

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

All Articles