What happens when you type ls -l in the shell

Erika Osorio Guerrero
6 min readApr 15, 2020

In this article i will go to a hole proccess of how the shell works, and I will try to explain the each step. So In order to understan the process we need to understand the programs I use to interact with the shell. First of all what is LINUX, The terminal and the shell?

What Linux

Linux is the kernel of the operating system of The Gnu project, it was developed by Linus Torvalds in 1991. first it was calles GNU/Linux operating system but Today it’s known for just Linux. It is free, open source and is available under GNU project.

To run a program in our computer and start a processes, the shell has to pass the information to the kernel, the core program of a computer’s operating system. This is the “kernel-space”. The shell runs system calls as the interface between the user-space and the kernel-space.

What is the terminal

It’s a program that opens a window, receives input with the keyboard and gives it to the Shell

What is the shell

It’s program “interpreter” takes the commands line from the terminal and figures what needs to be done by giving the information to the OS and then brings it back to the terminal. Using the shell gives us a way to control and communicate with the computer.

What is ls (list)

ls [option1] [option2] … [FILE]

ls is the short word or an “alias” for list, it is an executable file that list files (or directories) of the current directory, sort alphabetically.

waterfalls is the current directory

ls could be fallowed by one or more options; and today we are going to see just one of them and it will be the [-l] dash l.

-l (Long listing format)

So -l (lowercase L) option use a long listing format and causes ls to print it. so you can find useful information such :

1. File type
2. The file permissions
3. Number of hard links to the file
4. File owner
5. File group
6. File size
7.Date and time
8.File name.

Consider the following example of listing the files of the directory waterfalls:

Let’s see the meaning of each segment. The first character shows the file type, so in the example there are two files. The first file (file1) has a character [-] indicating a regular file, for file 2 [d] indicates it’s a directory.

The next nine characters are the file permissions, they are group into 3 characters. The first three are permissions for the user, the next three are for the group, and the las three are for other users. so the file1 [rw-rw-r- -] means the user and the group can read and write, but the others can only read the file. The number 1 and 2 after the files indicate the number of hard links to this file.

The next two segments [vagrant] [vagrant] are showing the owner and the group names follow by the size of the files 34 (file1) and 4096 (file2) bytes each one. The next column is the last modification date and time of the file. and Finally the last command column is the name of the file.

So now that we know how the command [ls -l] works let’s go a little deeper into how actually this command works in the shell in order to display a list in that long listing format.

The shell prompt is the command line where we input the commands, the shell will wait for us to put the command and once we hit enter the process will start. The following image is a representation of a shell prompt

The shell prompt displays the current directory follow by the dollar sign ($)

once we enter a command such us ls -l, the shell get’s that line from the getline() function, and then it will be divide it into spaces for this case. The shell takes the first word [ls] and search if it is a built in command, in case it is goes to that specific function en run it (builtin such exit, env, help. etc), otherwise keep running and will start the a new process using the system call fork()

Wait what? a new process in the same program?

Yes, fork() (a useful system call) copy the environment of the parent and creates a duplicate of it, this new process is known as a child, The new process will take the word [ls] and will search in the $PATH variable which is where usually all the executable files are stored. The $PATH variable is a list of directories separated by colons (:) If you want to know the list of environment variables the shell has, you can type in the shell prompt the word “env” which is a bultin as we say before.

Example of the Variable PATH follow by its value

The [ls] executable or binary file will be located in the /bin directory, which is the file where most of the executable files are stored. So how the shell figures the file is in which directory, it will compare add the file into each directory and uses the system call stat(), this will then compare if this file really exist.

So far so good finding the executable, but now what can we do with it.

Once we have the complete path of the file like /bin/ls The next step is giving the path to the execve() system call. This will change the process that the child was working with to a hole new process base on that executable file, in this case ls for listing, but what about the option -l. So part of the arguments we sent to the last system call we parsed the arguments as well, so the system call will executed and will get display the long list format files in the the screen as an output. Once the process of the child is done the parent process will be waiting for the return value of the child process. Using the system call wait, will allow the parent to wait until that process is actually end.

Finally, the shell will show the prompt again It means the process is done, and the shell is ready to receive a new command.

So That’s all I can explain for now, i hope you enjoyed the article and see you next time!

--

--

No responses yet