How to 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.
Asked
Active
Viewed 5,250 times
5
-
yes this one works ---- #include
#include – webminal.org Jan 28 '10 at 09:09main(){ struct stat statbuf; char *ff="/home/laks/file.txt"; if (stat(dirname(strdup(ff)), &statbuf) != -1) printf("\n %ld",statbuf.st_ino); } ----- thank you all.
3 Answers
2
#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()
.

mark4o
- 58,919
- 18
- 87
- 102
-
thanks I tried to run this way #include
#include – webminal.org Jan 28 '10 at 07:41main(){ struct stat statbuf; if (stat(dirname("/home/laks/file.txt"), &statbuf) != -1) printf("\n %lld",statbuf.st_ino); } But it core dumps -- any thoughts -
If you are using a string literal then use strdup. i.e. `strdup("/home/laks/file.txt")` – mark4o Jan 28 '10 at 07:44
0
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

ghostdog74
- 327,991
- 56
- 259
- 343
-
thanks for your answer. Is it possible to do the same using C API ? – webminal.org Jan 28 '10 at 06:26
-
sorry, missed out the programming language. Hope somebody can answer you soon. – ghostdog74 Jan 28 '10 at 06:28
-
Not your fault. I edited title only after your replied. So it's my mistake :) – webminal.org Jan 28 '10 at 06:40