Sebastián Valencia Sierra
3 min readAug 19, 2020

--

What happens when you type ls -l in the shell

First, you have to understand that [ls]is a command to list information about files. When you type this command on your shell it will display entries alphabetically if no contrary instruction [OPTION]is given.

This command is in the / bin directory: which corresponds to the basic executables (or binaries) of the system, when, for example, we execute the ls command, we actually execute / bin / ls.

In the image below you can see some “ls” command options:

In the previous image you can see an example of typing just [ls](without OPTIONS) in some directory. In this case, the command display in the shell the contents of the 0x03-shell_variables_expansions.

However, the commad has multiple options to give instructions when the command is executed. For example, if you add the [-l]option to the command, it will display files in long format and will show us their permissions, as you can see below:

In this example, we can see that if we execute just ls, it will display the files contained in the current directory, but we can see the file name and its extension. Otherwise, if we type ls -l, the command prompt will show us the peermissions, which are the first column that contains for example: -rw- r — r — ….. This line refers to writable (w), readable (r) and executable (x); and is divided by triad corresponding to owner, groups and other users.
So, in the previous example we have that the owner has read and write permissions; while the group and the other users only have read permission.

So, in this order of ideas, the -l option tells thels command to print files in a long listing format and it will display the following file information:

  • The file type
  • The file permissions
  • Number of hard links to the file
  • File owner
  • File group
  • File size
  • Date and Time
  • File name.

But this is what happens on the surface, what really happens at the machine level is that the shell waits for the user to enter a command. Once the user enters ls -l, the shell calls the getline () function to pass the arguments to the program and execute them. If things go, the shell will look for the ls program in the $ PATH variable, which is a list of directories where the shell looks for commands that are entered by the user. Once ls is run, three system calls are made: fork (to create new processes, and duplicate parent processes to run commands), execve (to stop duplicate processes and load the new program) and wait (to wait while all this happens following the path of the child processes). Once the command is executed, memory is freed and the shell reprints a message waiting for a command from the user.

Thats it.

Sebastián Valencia Sierra.

--

--