Search code examples
pythonlinuxfilesystemsinode

How to get all the inodes under the linux filesystem with python?


I'm trying to do some calculations on inodes (Get their size, etc...) All I found in the web was how to get the inode of a certain file or directory, but I want to get all the inodes with a single call, and then use them one by one have any ideas ?

Thanks


Solution

  • How about this?

    import os
    inodes = os.popen("sudo ls -Rli / | awk '{ print $1 }'").read().split('\n')
    inodes = [int(i) for i in inodes if i.isnumeric()]
    

    For my home folder, this returns a list of the inode numbers:

    [11666512, 10223622, 10234894, 10223641, 10223637, 10617011, 10254828, 10249545, 10223642, 10223643, 10487015, 10223640, 11929556, 10223639, 10223644, 10486989]
    

    To clarify, the ls command takes three flag arguments, R, l and i. R performs a recursive search to check all files in the folders and all subfolders starting with /, l formats the output to give us a list, and i gives us the inodes. We pass the results to awk to get the first column which contains the inodes and then do some simple cleaning of that data.