How Does ls command with option -l work in Shell?

Paulo Morillo
4 min readAug 19, 2019

This blog has the information about Linux command list “ls” for list files and directories with the long option “-l” step by step.

First, we are going to see the ls syntax and how it works:

ls [OPTION] [file]

only type ls print the files and directories in your current directory. But when you use options you can print long formats “-l”, hide files “-a”, information about the author “- - author”, etc.

To use this command we should use a shell who is a program that takes keyboard commands and passes them to the operating system to carry out.

if you want to interact with the Shell program, you can use a terminal emulator like KDE, GNOME usually called them “terminal”, in Apple OS the emulator name is terminal.

For this example, we are going to use an AMI2 It’s a Linux distribution for Amazon and we are connected with putty by a SSH protocol and in this case, we have the terminal of Putty program. (We are working from Windows OS for this reason we used Putty to connect to AMI2)

When you open a terminal you can see some text this is PS1:

PS1 is the Primary prompt String and in this case, has this format:

[\u@\h \W]\$

U = user, h = hostname, W =current or Working directory, $ = User identifier when is different to superuser (user with all permissions).

you can type this echo $PS1 and you can see the format of your PS1.

also, you can see PS1 after you type a command and hit enter (you will see the output information for the command). Because Shell prints the prompt again.

In our example:

[ec2-user@ip-172–31–91–220 ~]$

Where:

ec2-user = Username. @ = Separator. ip-172–31–91–220 = Hostname.

~ = The current directory that we are ( in this case Home directory).

$ = Dollar Symbol to indicate where you can type your commands (after this). In some cases, the Dollar symbol could be a # hashtag numeral when you are a superuser.

Now that we have opened a terminal, we are going to type the ls command to see what happens.

after this, we can see, the command ls list the files and directories in the current directory but, why did the shell do that, and how to use arguments or options like ls -l?.

We can see that -l option shows us the files and directories in the working directory with long format (see the image to know the long format description).

Image from Linuxcommands.org

When we typed and hit enter the shell received the input with a function (getline) like strings, later It splits the string with tokenization (Process to separate words and operators) and It gets the command ls because in this example it doesn’t receive arguments for this function. Now that shell knows what its the word, It searches if the word is an alias (shortcut to do commands faster than usually type the commands and arguments, see example below) if It’s not an alias he tries to find it is a Shell Builtin command (Commands who comes with the shell (Built-in)). If Shell still doesn’t find the word, It tries with PATH (Environment variable with specific format address to find commands to run in Shell, see example below). In other cases, the word didn’t find and should print an error message.

In case of finding, the word in alias or builtins or Path the shell should use system calls to create a new process (fork) call child process and execute the program when It found the file to execute (execve) and this should happen while shell waits for the end of this process (for this It should use exit system call). This could be explained in the next picture:

Image from http://web.sfc.keio.ac.jp/~hagino/sa16/03.pdf — Tatsuya Hagino

The picture shows us that Shell also finishes with a system call Exit, It can destroy the current process.

Alias example:

alias [name][=][commands with arguments]

alias l=“ls -l ”

It creates an alias to list files and directories in a long format only typing the letter “l” and hit enter.

Path example:

To show the Path environment variable with all address we can type:

echo $PATH

Bibliography

William E. Shotts, Jr., The Linux Command line 4th edition, book

Tatsuya Hagino, SOFTWARE ARCHITECTURE, SHELL, http://web.sfc.keio.ac.jp/~hagino/sa16/03.pdf

Michael KerrisK, The Linux Programming Interface, Process Creation Chapter 24.

Authors

Edgar Miguel Rodríguez García —969@holbertonschool.com.co

Paulo Andrés Morillo Muñoz — pauloan@hotmail.com

--

--