Get the directory number that ls -l shows


How can I get the exact directory number that ls -l shows under linux/unix. It must be this number, not a number that comes with counting the entries manually in rust.


What do you mean by "directory number"? The inode number or something else?

Identical numbers are the same no matter where they come from, so why this restriction?

Do you mean the number 332 in the following output from ls -l on my system?

drwxr-xr-x. 1 farnz farnz       332 Jul 31 18:23  Documents

If so, it's the number you get from Metadata::len(). See the following example code, which prints 332 for me:

fn main() {
    let meta = std::fs::metadata("/home/farnz/Documents").unwrap();
    println!("{}", meta.len());

Alternatively, if you mean the inode number that comes from ls -i, not the on-disk size from ls -l, you want MetadataExt::ino:

use std::os::unix::fs::MetadataExt as _;

fn main() {
    let meta = std::fs::metadata("/home/sfarnsworth/Documents").unwrap();
    println!("{}", meta.ino());
1 Like

Thanks for the answers. But it was the MetadataExt::nlink I searched for.

Thank you