What happens when “ls -l” is typed into the linux shell?

Laura Peraltav
4 min readAug 22, 2019

By: Julian David Gaitán Saenz and Laura Peralta Villegas.

Before we begin with this explanation, it is important for the reader to unsderstand what a shell is. A shell or so called a CLI (command line interface) is a textual user interface that allows user communication or inteaction with the OS kernel (AKA core of the OS), and asks for an action, and resources from it to fulfill the user’s request. The kernel interacts directly with the hardware unit.

Source: https://www.taringa.net/+linux/

Also, to fully understand what happens when you type any command on the CLI you must know how the request process works under the hood. For practical reasons we are going to work with ´ls -l´, where ´ls´ is the command and first argument entered by the user, and ´-l´ is the flag assigned to the command.

How does it work then?

So let’s begin, when the shell program is opened on your computer the first thing it does is to look for the PS1 environment variable to format the prompt which is where the user is going to input the command.

Open terminal waiting for user’s input.

When the user enters the command and/or arguments they are read by the ´getline()´ function, that reads the whole line until the newline character, and stores its data into a buffer.

Typed command

After that the program takes the string and tokenize it, this means that the string will be broken up into words and a null charecter will be added at the end of each new token, for this the ´strtok()´ function is used, which takes the string and a delimeter as arguments.

Once the program has the tokens it checks if the token has an alias in the system, this means it is going to look for coincidences between the string passed by the user and the commands inside the system.

If there are no coincidences, it will proceed to check if it is a “built-in” command, those are commands built into the Linux OS. Some examples are ´exit´ and ´env´.

In case the first two conditions were not met the program will continue with its search through the custom commands, to get to them the program must complete some steps to find them inside the system.

  • The program looks for the PATH global variable, and then will tokenize it to divide the original string into the different directories.
input received by strtok().
  • In order to break this string into the different directories, the delimiter used must be “:”. After the program receives the tokens (i.e. ´/usr/local/sbin´ and ´/usr/local/bin´) it will append the command at the end of each token to lok for it on that directory (i.e. ´/usr/local/sbin/ls´ and ´/usr/local/bin/ls´).
  • If the executable does not exist the program will return an error, otherwise it will invoke the fork syscall that will create a child to the current process and both programs will continue to execute simultaneously how ever the parent will need to use ´wait´ syscall to literaly wait for its child to finish before continuing with its own execution, on the other hand the child will need to call ´execve´syscall to replace the copy of the parent process (that is created when fork is invoked) with the process that has been requested by the user, in this case ´ls -l´.
Creating a new process.

That’s how the process looks like internally, and the return in the terminal for our example would be as follows:

Result printed on the terminal after typing “ls -l”

Now, what does this mean? ´ls´ is a command that lists all the files in the current directory, meanwhile the ´-l´ flag refers to the long format, which includes the following items:

  • permissions given to owner, group and others
  • number of hardlinks
  • names of group and owner
  • file size (in bytes)
  • date/time of last modification
  • file name

Finally the memory that might have been allocated during the process is freed, the main function is called again to prompt, and require the user to input commands/arguments.

(reference “What happens when you type “ls -l” in the linux shell? by Connor Brereton and “Want to build a simple shell? Here’s how you do it” by Nickolas Teixeira)

--

--

Laura Peraltav

A free soul seeking purpose, willing to learn new things every day. Passionate about photography, digital painting, mindset and entrepreneurship.