What happens when we write “ls -l”?

Julian Torres
4 min readAug 18, 2020

In order to understand what happens when we type this we must first understand the following:

What is a shell?

In computer science, the term shell is used to refer to those programs that provide a user interface to access the services of the operating system. These can be graphics or simple text, depending on the type of interface they use. Shells are designed to facilitate the way the various programs available on the computer are invoked or executed.
Simply put, the shell is the desktop environment that we use to work on our computers regardless of the distribution either by graphical environment or by terminal.

The next thing to understand is the Linux structure:

The kernel is the central part of an operating system and is responsible for performing all the secure communication between the software and the computer hardware.
The shell is an application, one of the two main ways to control one computer (GUI and the other). It’s the way a user talks to the kernel, typing commands on the command line, so it’s known as the command interpreter.

Now let’s look

When the system is ready it looks this way:

The shell displays a system symbol that is commonly “$” or “A”, when we find input with the system symbol means that it is ready to receive commands.
We could customize the system symbol by modifying the PS1 environment variable(meaning “request string 1).
Following this the user enters the command, the shell reads the ls -l command from the STDIN(standard input) of the getline() function, comprising the command line that were passed to the program that is executed.
The shell checks whether what is input is an alias or not, if it discards this checks for a built-in command, the environment is copied, and a new process is created to which the copy is passed, then the shell looks for a program file named ls in the PATH environment variable that contains the address of all executable files on the system.

Each time a command is entered the shell searches PATH over the command, when the $PATH the command directories are tokenized considering the delimiter ‘:’, the ls executable file will be located in the /usr/bin/ls address.
The /usr/bin directory contains some of the ready-to-run programs, in other words the commands.

When you type the ls command with its -l flag, all the files and directories in the working directory we are currently in are listed with the permissions, owners, date and time of creation.

Internal steps of ls -l :

  1. The line entered in the command by the user is read with the getline function.
  2. The strtok() function divides the space-separated commands.
  3. The shell checks whether an alias is being used. An alias for ls -l would be ll. When this happens, shell will replace the alias with the full command.
  4. Check if it is a builtin or not, look for the first word entered in the directory where the default executables are located if found runs the built-in.
  5. If it is not built-in command it will look for the executable you need in the $PATH.
  6. Shell uses the fork() system call to create a child process to execute the entered command. it also uses the call to the wait() system to wait for the previously created process to finish and send the next one.
  7. We can use the absolute path of ls /bin/ls if it is an executable the system call exceeds will be used to run the program in the child process.
  8. Finished, after you have run the shell comado returns with a new process and prints the prompt waiting for the next command.

--

--