LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What happens when you type 'ls' (https://www.linuxquestions.org/questions/linux-newbie-8/what-happens-when-you-type-ls-936411/)

lsteacke 03-25-2012 11:21 PM

What happens when you type 'ls'
 
Hey all,
So I was recently asked a question and couldn't really come up with a technical answer to it. What exactly happens, in terms of the shell and the operating system when you type the command 'ls'. Answers would be appreciated or links to appropriate reading materials. I suggested stracing it, but does anyone know, in a point by point break down, what happens?

Thanks

jhwilliams 03-25-2012 11:30 PM

Bash fork()s a new subprocess and exec()'s the ls executable. The ls executable is built from the Coreutils sources found here:

http://ftp.gnu.org/gnu/coreutils/coreutils-8.15.tar.xz

My suggestion would be to have a look around that code base. Look for the main() function in the ls-* files, and start from there.

I did write an ls implementation once upon a time for college; I think it was mostly based on stat(), readdir() and friends.

chrism01 03-25-2012 11:30 PM

Off the top of my head, I'd mention inodes.
If I remember, there's no such thing as files/dirs really, all the metadata is stored in the inodes and then ptrs point to the relevant data blocks ...

https://en.wikipedia.org/wiki/Inode

catkin 03-25-2012 11:37 PM

After the command is typed and enter pressed (and assuming no variables or patterns to expand in the command), bash goes through aliases, functions, hash table and PATH to determine which executable to run.


All times are GMT -5. The time now is 03:15 PM.