TLDR
ls parsing bad, do this
ls -1iq | grep -o '^ *[0-9]*' # get file inodes
find /path/to/search -inum <inode_number> # convert inodes to file info
You can continue your doomscroll now ππΌ
You still here? (thanks!! π) ok, then read on:
Do you know why its not recommended to parse ls output for operation on a list of files?
Itβs because the UNIX system allows any characters in filenames so things like newline, whitespaces, ~ (and a lot more weird chars) are all valid.
On a large enough fileset this can cause a lot of issues. So what to do now? Regex?! π΅βπ«
No!! Iβll stop you right there. Before you go and write (or copy paste π) a piece of regex, that you need to google (or ask ChatGPT) everytime to understand.
inode to the rescue
In UNIX-like operating systems, an inode (index node) is a data structure used to represent a filesystem object such as a file or a directory. Inodes store metadata about the file but do not contain the file name or its data.
So how do you see inodes? simple use the -i flag with ls.
ls -i # gives you the file names and inode
ls -1i # file names and inodes in a list
ls -liq # li + replace non-graphic chars as '?'
Ok now that you have the inodes, lets get em! βπΌ
Now weβll use a bit of Regex (little bit is ok! π)
ls -1iq | grep -o '^ *[0-9]*' # outputs only inode numbers as a list
Oh, did I tell you that inodes are always integers! (you would have known if you tried the commands I posted earlier π)
Ok, now you have a list of numbers, what next? What do you do with it? How would I know? π€·πΌββοΈ do whatever you want, you can now store them any way you want.
But if you want to get the file info from the inode, you can do either of these:
find /path/to/search -inum <inode_number>
# or
ls -i /path/to/search/* | grep <inode_number>
This gives you a safe way to βparse lsβ and be confident with your implementation. π₯³ And donβt worry, you can work with inodes in any programming language, I used bash because its the simplest to explain (again little bit bash is ok!)
Thatβs it, go try it out now! (donβt forget to add error handling π)
Inspired by this discussion - ππΌ this article was just supposed to be on my notes, with the TLDR. π€
P.S - I know you will doomscroll again, but I hope you learned something new π€
P.P.S - Ask AI to write a blog post like this! π