What happens when you type ls -l in the shell ?
ls -l on the surface:
As one of the first commands once introduced to many programming languages, « ls »
is a short abbreviation for listing files in the current working directory. Typing and passing uniquely « ls » to the CLI displays files in bare format. Bare format doesn’t show many informations.
To display more files informations « ls » can be combined with several options to reveal further details such as «ls-l ».
Now with the « ls -l », you can list files in long format displaying permissions, file types, number of hard link, owner, group, size, last modified date and filename .
What’s going on under the hood when typing ls — l in shell ?
Let’s breakdown the process of exciting this command into steps :
1st : The shell prints out the prompt and wait for user input which in this case will be the ls — l command .
2nd: The shell reads the ls -l command with the getline function STDIN.
3rd: The shell looks for the program file corresponding to the command usually it’s a subdirectory located inside the ‘/usr’ (‘/usr/bin/ls’).
4th: In order to execute the ‘ls’ command three syscalls are made « fork/execve/wait ».
- fork(): fork is a syscall that creat a child process of the parent process.
- execve():load the program file, « ls »in this case, replacing the duplicated newly created child process and start the new program.
- wait(): this syscall is responsible for keeping the parents process running until one of it’s child has terminated.
5th: After the command has been executed, memory will be freed and a new prompt will be executed.
Written by: Taha Yazidi and Seif Jelidi.