Behind the scenes: ls -l in the shell

The command “ ls -l” needs a detail explanation

ls command might be one of the most frequently used in Linux. But, to understand what really happens when you type ls -l in the shell, you have to understand what the shell is, in relation to the kernel.

A kernel, though an important part of the operating system, cannot interact with the user. It only talks to the hardware and software, highly involved in resource management, like memory, IO (Input/Output) and CPU management, among others.

The shell is an application that allows the user or programmer to run other programs. It is the way a user talks to the kernel, by typing commands into the command line. The other way of controlling a computer is through the Graphical User Interface.

So, at the beginning, the kernel execute the “init” process as a boot loader that call start_kernel(), also its role is to create all processes that are responsible to execute all programs set in the operative system from a script stored in the file /etc/inittab. The init is the parent of all processes and its ID is 1.

What happens when you type “ls -l”

First, the shell prints the prompt, that normally ends with a ($) sign or a (%) one, suggesting the user to enter a command. The shell reads the command ls -l from the getline() function’s STDIN, parsing the command line into arguments that it is passing to the program it is executing.

Basically, ls command is a command in Linux used to list files and directories. ls command comes with so many arguments and features like you can sort them by date, by size, able to check hidden files and directories, permissions, inode information and so on.

The shell checks if ls is an alias. If it is, the alias replaces ls with its value. If ls isn’t an alias, the shell checks if the word of a command is a built-in (a command with special properties stored at the library functions). So the shell takes ls to concatenate with every directory and verifies each one to match them.

Secondly, after the command, the shell reads what you typed using the getline function. The getline function is the preferred method for reading lines of text up to and including the next newline character.

Getline function´s syntaxis:

size_t getline(char **lineptr, size_t *n, FILE *stream);

Linux distributions alias, the ls command, includes flags (e.g., –F) that requires access to data structure for files in the directory, and this requires execute permission on the directory.

The function that is going to execute it is called execve. The function returns nothing on success and -1 on error.

int execve(const char *filename, char *const array[], char *const envp[]);

Creating the process

The concept of processes is fundamental to the Unix/Linux operating systems, and all running instance of a program is known as a process. The way to distinguish processes has it’s by ID or identifier and each process has it’s own ID. The ID is a non negative number and associated with the process.

To create a process Unix system has it’s own tools and are what we call system calls. The fork() function it’s a system call to create a new process and technically a child process. For example if we want to run a program or execute a command from terminal like in our case. To be sure that the call process is terminated we can use the call system wait() to block the parent process until it’s done.

Sources:

The Linux programming interface. No Starch Press (2010). Michael Kerrisk.

https://medium.com/@1401_15680/what-happens-when-you-type-ls-l-in-the-shell-f4ac171c2534

https://www.quora.com/What-is-the-relationship-between-Kernel-and-Shell-in-UNIX-OS

https://www.howtogeek.com/435903/what-are-stdin-stdout-and-stderr-on-linux/

I am a Science and Tech writer from Colombia, South America. I am interested in humanity and its prospects and in the impact of future technology capabilities.

I am a Science and Tech writer from Colombia, South America. I am interested in humanity and its prospects and in the impact of future technology capabilities.