DEV Community


Posted on

How Does the Shell Command ls -l work ?

Alt Text
Linux ls command is used to list files and directories. By default, it will list the content of the current directory. In this article, I will show linux operate certain process and operations in order to execute this command .
first thing first, before starting talking about the ls command, it’s important to understand how linux work …

****Shell & command line :
Alt Text

****The system calls :
To make this happens, Linux need to operate a succession of operations and tasks, to make the execution of a given command possible ..
When a user type a command , the shell will first make a call to the kernel which is the heart of this operator system , asking him the permission to start executing a process…this fundamental step is called : a system call …
Thanks to this function, the kernel will attribute a private identifier to this process (PID) , giving the birth to anew process that has the ability to exchange data with the kernel using the PID …

**Child process and PID :
It’s important to mention that processes , like the human being , have a parental relation : a process when is born , called : a child Process , and the process that made the **fork()
system call , is the father …
A child process inherits most of its attributes, such as file descriptors, from its parent. In Unix, a child process is typically created as a copy of the parent, using the fork system call. The child process can then overlay itself with a different program (using exec) as required. Each process may create many child processes but will have at most one parent process; if a process does not have a parent this usually indicates that it was created directly by the kernel. In some systems, including Linux-based systems, the very first process (called init) is started by the kernel at booting time and never terminates.
After the fork() system call , the operating system has now a free running child process available , so now we can make another function system call , called EXEC() …. thanks to the exec function a new executable file can run in the context of an already existing process (which is the child process) …

Command line interpretation :

let’s go back now to our beloved command Ls , once the shell make the call for a new process , the shell tokenize first the input user(command), and fill it into a null terminated array .

  • In this array the executable file ls and argument -l are stored separately. Once the command is stored , the program start searching for the full path of the executable file which is the ls in this case …

Alt Text

The shell will look inside the PATH that contain all the environment variables , specifically in the /bin folder where most of the executable files are installed .
If the corresponding path and executable file are found , the shell execute the command through the exec system call , otherwise an error message is printed the user interface .
similarly , and after the executable file was found , the shell will attribute the given stored argument “-l” to the the exe file “Ls” …
So no the full command ls -l is executed , and as a result , a full list of information about files and directories will be printed to the user interface :
Alt Text

Discussion (0)