How the ls -l command works

Diego Felipe Quijano Zuñiga
3 min readNov 28, 2019

--

In the simple shell the command ls lists all the files and directories existing in the current directory that you are placed in the shell.

The ls command as its initials realize refers to the word List in English which means in Spanish: lista.

ls is a command that list directories and files of a certain directory, in order to show the results alphabetically.

It should be noted that some files that start with a period (.) are not shown with the ls instruction because it is known as “hidden files” and for that reason they are not visible when a file starts. But with the -l option of ls, this behavior is excluded which allows it to be displayed and shows all files and sub-directories, this includes those that begin with the period.

the flag -l it will list them too but in long list format what it means is that all the files and directories will be printed with all their attributes like the permissions, the owner, the group, the number of bytes, the date of the last modification.

Below is an example to better understand the ls -l command

The command “ls” is a file containing the program to execute the “ls”command. So when you type in a command, shell will attempt to locate the command file by searching through its disk and directories in your PATH. It is necessary a copy of the ENVIRONMENT. If the search is successful and the program is found, shell will clone itself and run the corresponding program in the new process. The cloned process will be replaced with the program it wants to run, and the original process will wait until the new process completes its execution before moving forward. The output can be written in standard output, which is what you see in your terminal. It can also be piped, or redirected, into a file or even to another command. When we type “ls” and hit enter, we are typing our command from the standard input. Similarly, you can invoke the command by having it piped from a file or a command. After the command is executed, control will be returned to the original process and you will be greeted with the login shell, once again.

After we’ve entered “ls -l” and at the point where the shell is ready to invoke the “/bin/ls” executable, it creates a new process using the syscall “fork” (the child process).

Having created a new child process, the shell can finally, execute its entire command /bin/ls -l. To do that, it makes another syscall, “execve”, which receives the calling command (/bin/ls) and all proceeding arguments (-l). The call to “execve” executes “/bin/ls -l” within the child process, returning 0 to indicate success.

Once the call to “execve” has finished, the child process terminates (through either a call to the syscall “exit” or the processing of a Linux signal, its PID goes out of scope, and the active operation returns to the parent process. Another syscall, “wait”, is typically used to ensure that the child process terminates before returning to the parent.

The command ls without any location specified prints all the contents of the current working directory to standard output, with the option -l doing so in long listing format.

Having raised and seen a child off to the world, the parent shell process has finished its job — it re-prints the prompt and awaits a new command from standard input to repeat the process all over again!

--

--