What happens when you type ls -l in the shell?

What happens when you type ls -l in the shell?

The result and output of using the “ls” command with the long format option it may be easy to explain but what’s behind of all the processes under the hoodfor making it possible it’s another story.

For better understanding, you’ll have to know what the shell is.

No hay texto alternativo para esta imagen

Not this shell!

No hay texto alternativo para esta imagen

Our simple CLI shell prototype

The shell is an interactive user interface with an operating system. It takes commands from the user and executes them. Upon entering the shell, you will encounter the prompt(prompt string 1, or PS1) that serves as the work station or environment screen, also knows as the user interface, this particular example is a simple Command-Line Interface(CLI).

Most of the time shells have built-in commands that perform specific tasks, most commands the shell executes are programs, but not all of them.

They are grouped into two categories:

  • Internal Commands: Commands which are built into the shell. For all the built-in commands the execution works fast in the sense that the shelldoesn’t have to search the given path for them in the PATH variable and also no process needs to be spawned for executing it.
  • Examples: source, cd, exit, etc.
  • External Commands: Commands which aren’t built into the shell. When an external command has to be executed, the shell looks for its path given in the PATH variable and also a new process has to be spawned and the command gets executed. They are usually located in “/bin” or “/usr/bin”directory. For example, when you execute the “cat” command, which usually is at “/usr/bin”, the executable “/usr/bin/cat” gets executed.
  • Examples: ls, cat, etc.

Focusing on the function itself:

$ ls

It’s a command in the shell. If there isn’t an alias for “ls” then the shell will check first if it’s a built-in function, but since “ls” is an external command then it will interpret and execute its main function, which is to lists all the files and directories inside of a directory.

When we type and enter:

$ ls -l
No hay texto alternativo para esta imagen

Illustration from The Geek Diary

There will be an entire line of information for each file. The -l option (a dash and a lowercase letter “L”) changes the normal “ls” output to a long formatversion, displaying additional information of each file or directory enlisted by the command, you can also get the long format info of a single or even multiple files using the names of each file or directory as arguments in the prompt, or, using a wild card (asterisk “*”) for an easier sort of selection.

This is the output the command “ls -l” should display on our screen:

No hay texto alternativo para esta imagen

Command prompted and executed on our shell

Now let’s talk about the Kernel and its interactions:

No hay texto alternativo para esta imagen

Flowchart made with Lucidchart

The kernel is the central, most fundamental part of a computer operating system. It’s a program that controls all other programs on the computer, serves as a communication medium between hardware and software, highly involved in resource management, CPU, memory and System calls (syscalls) which allow applications to communicate with the kernel. An application can call functions in a library that rely on the system call interface to instruct the kernel to execute on behalf of the application.

One example of a system call is “fork()” that can create a clone of the process is currently executing.

In multitasking operating systems, processes (running programs) needs a way to create new processes, for example, to run “ls -l” command. “fork()” and its variants are typically the only way of doing so in Unix-like systems. For a process to start the execution of a different program, it first forks to create a copy of itself. Then, the copy, named as the “child process”, calls the “execve()” system call to overlay itself with the other program.

Upon completion, right after the command displays on the screen the list of content in the current directory, the child process will terminate itself and then returns to the parent process.

And that’s pretty much how it works under the hoods!

Thank you for reading! :)

要查看或添加评论,请登录

Javier Gutiérrez的更多文章

社区洞察

其他会员也浏览了