How can I obtain list of items and their inode numbers in the current working directory ?
Asked
Active
Viewed 4,490 times
3
-
@VolkerSiegel A comment ? That's an interesting reason. Can you explain what exactly you mean ? – Sergiy Kolodyazhnyy Sep 10 '18 at 05:39
-
@VolkerSiegel It's intended as "canonical" question for the site, which can be used to close other questions. If someone comes with the same question, we can use this one as duplicate. But to argue the case, many canonical questions themselves look simplistic and "lacking research", and for a good reason - they're intended as questions that address the basics. Take for instance [this one](How to remove all files from a directory? ). I'm sure at the time when the question was creted, stackoverflow already had one; problem is that you can't close question on site A with link on site B – Sergiy Kolodyazhnyy Sep 10 '18 at 06:48
-
Makes sense. I retracted my close vote. – Volker Siegel Sep 10 '18 at 07:10
-
Today this question reached 1000 views. I think this serves as a somewhat testimony that it is a useful question – Sergiy Kolodyazhnyy Oct 07 '19 at 00:03
-
I agree. And I'll remove my older comments, as they are just confusing now. Thanks for pointing it out! – Volker Siegel Oct 07 '19 at 00:08
2 Answers
5
ls
has -i
flag:
$ ls -i
1054235 a.out 1094297 filename.txt
But if you are feeling adventurous , build ls -i
yourself:
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
void print_dirents(DIR * dir){
struct dirent *entry;
while ( (entry=readdir(dir)) != NULL ){
printf("%s,%d\n",entry->d_name,entry->d_ino);
}
}
int main(){
char current_dir[PATH_MAX];
DIR *cwd_p;
if ( getcwd(current_dir,sizeof(current_dir)) != NULL){
cwd_p = opendir(current_dir);
print_dirents(cwd_p);
closedir(cwd_p);
} else {
perror("NULL pointer returned from getcwd()");
}
return 0;
}
And it works as so:
$ gcc lsi.c && ./a.out
filename.txt,1094297
a.out,1054235
..,1068492
.,1122721
lsi.c,1094294

Sergiy Kolodyazhnyy
- 105,154
- 20
- 279
- 497
4
stat ./*
or
man stat; stat --format=*f* ./*

Sergiy Kolodyazhnyy
- 105,154
- 20
- 279
- 497

Volker Siegel
- 13,065
- 5
- 49
- 65