What happens when you type ls -s, behind the scenes

David Latorre
4 min readApr 16, 2019

--

initially, what happens when you type ls -l?

This happens

ls, in unix systems is a command for list the files of the current directory, but if you add the -l flag it shows the list files in long format

What is long format?

long format is a very descriptive list that includes for ls include the permissions , how many files are inside a folder (only apply for folders, a single file will show “1”), who is the owner, bytes, last modification in month, day and hour format, and finally the name of the folder or the file, look the picture again!.

But how all this simple command is executed? The history behind

All begins with SHELL, shell is the user interface to communicate with the operative system services, and also, has his own language (scripting language) called bash scripting , all of this are built in C (C, not C++ ;) ) under the hood.

So, let’s go to eviscerate this!

First step, show me the f***ig prompt!

The prompt is in C (Remember, C is the language behind the shell, and in general, the operative system) a infinite loop, yes, like a while without an increment . It will show a line ready to receive text, the infinite loop is printed to the standard output of the system

Second step, give me arguments!

inside our infinite loop we have a getline function expecting text provided by the user, when he/she pass an argument and of course, compare if this isn’t null (Yeah, we provide our shell to troll-proof) in case of a null argument, the prompt is shown again.

Third step, lets parse!

Next, the shell program parse the provided text into a string (yes, an array of characters)

Fourth step, lets tokenize!

“Tokenize” is the act of take the user text converted to a string, and in our code separate this into “tokens” using a deliminator in this case the blankspaces, for ls -l will be tho tokens, the ls and the -l

Fifth step, searching in the path

The path is simple, is an environment variable that stores the addresses of the folders where the executables programs are. for ls is /bin/ls

In our C code we have a function that invoke all the environ, match with path, and store that folders address in a new variable to be called when we need to compare and search inside a folder to match to the argument passed, in our case, ls.

if nothing match, the program will show “command not found” this is possible using a simple “if” and “else” in our code, is very important evaluate all the possible cases.

New challenge concept is approaching!

The system calls, this concepts refers to a special calls for services in the kernel, this is commonly handled by low level code like C and glib library, it transfer privileged code to be executed direct in the kernel, when a system call is invoked the execution of the program is interrupted and wait for the process of the code in the kernel, in this case we have system calls that perform that tasks, wait, exit, fork, open, read.

For example, the wait system call is the responsible to interrupt the execution of our parent process until the child process finish.

Sixth step

When the token match, it call the executable file from the path, creating a fork process called child process from the parent process (shell)

Seventh step, please wait…

when the command is called and executed(ls), the kernel process the signals and block the shell until all the process finish, in this case when ls command is called, is passed the “-l” argument to this, inside the ls source code is an option about what show in case of that argument, but is very deeply for now, the ls source code in coreutils is about 2000 lines of code in C, so I need to be gently with your brain (and my mental health), so I will not explain the ls internal process.

Finally

The expected output (provided by the ls source code) is printed properly in the stdout (standard output)

Yes… That’s all the process of type a single ls -l but now under the hood.

--

--

David Latorre

Entrepreneur, Backend Developer & Dev Ops & Linux Sys Admin, ArchColombia community founder