What happens when you type ‘ls -l’ in a Linux terminal?

Santiago Zapata Bedoya
3 min readNov 26, 2020
img source: https://ubunlog.com/introduciendonos-en-la-terminal-comandos-basicos/

So Linux is an Operating System, same as Microsoft. The main difference is that Linux is for free xD. And that with Linux we don’t have as much restrictions as with Microsoft. And just like the windows command prompt, Linux also has one, also known as terminal or shell.

So when you open either cmd (for Microsoft)or terminal (for Linux), the prompt is gonna be displayed waiting for the command to execute it! As we are focusing only in Linux, the prompt can be either ‘user@username:~/…/$’ or ‘root@username:~/…/#’ This shows current user, username, this ‘~’ is home directory and ‘$’ is normal prompt and ‘#’ is root user prompt.

So in order to execute either a command or a program, the operating system passes them to the kernel, which is like a processes and memory manager. Imagine a guy taking super fast decisions in order to handle as best as possible the whole potential of your computer avoiding it a collapse. It communicates with the hardware directly and then with the shell. The kernel comes with a specific configuration already setted up.

img source: https://flylib.com/books/en/3.475.1.15/1/

When the shell is ready to take commands, it prints the prompt as we already said. When you type ‘ls -l’ The shell takes that input with a function called getline() and parses the text into arguments that will be passed to the executing program.

Aliases
The Shell looks if the first argument ‘ls’ is an alias of any command. An alias is just that.. An alias. For example you can alias your ‘ls’ with ‘print_everything’ so when you type ‘print_everything’ it will behave as ls.

If it is an alias, repeats the procedure until it finds no aliases anymore.

Built-ins
These are like the default programs that come with any POSIX.

Environment copied
So the shell looks for some program file called ls where every executable file is in the system: Shell’s environment; specifically in the $PATH variable. This variable is a list of directories the shell searches for every time any command is entered.

When the shell finds the executable ‘ls’ in /usr/bin/ls, that’s where most of the executables are in the shell, then proceeds to execute it.

Child process
To execute the program, the shell creates a child process for ls command. This is made using fork() function. What a child process do is to make the same thing as the main process, but as they both can be diferenciated because of their ID’s, a Child process’ Id is always 0, with that the shell specifies the child process to make whatever it has to do, while the main (or better the Parent) process waits with the wait() function until the child process dies.

Absolute path
The absolute path of ls is /bin/ls. If it is an executable, the shell executes it with the execve() system call inside the child process.

Once everything is done. Either error or success, everything ends when the prompt string 1 (PS1)appears once again waiting for another command to execute.

--

--