How to list files and their inode numbers in current directory?

Solution 1:

stat ./*

or

man stat; stat --format=*f* ./*

Solution 2:

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