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

Emna Bouaziz
3 min readNov 20, 2020

--

The LS -L COMMAND

ls is a Linux shell command that lists directory contents of files and directories. if you’re typing in your terminal “man ls” it will display to you the user manual of any command that we can run on the terminal. It provides a detailed view of the command which includes NAME, SYNOPSIS, DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES, AUTHORS and SEE ALSO.

Here in our article we’re wondering what would happen if we type ls -l in the shell ?

ls syntax

$ ls [options] [file|dir]

the ls option is where we can choose what exactly we want to display from our file or directory. In our case we will be using the ls -l command and see what results we got.

example 1: used the ls -l command in the shell

So this is exactly what happens when we type ls -l in the shell. As a description ls -l list with long format — show permissions.

Okay that was a little short for an example let’s give more of it ..

example 2: used the ls -l command in the shell

-l long format, displaying Unix file types, permissions, number of hard links, owner, group, size, last-modified date and filename.

I guess it’s more obvious now in this second example now let’s try to go further and deeply on the explanation of the ls -l command by using examples and diagrams.

ls -la

Basically, there are 3 types of permission:

  • Read permission (r): User or group could read file content.
  • Write permission (w): User or group could edit or modify file content.
  • Execute permission (x): User or group could execute file.

This was ls -la we check with it a file permission ls -la file_name

An example of the execution command ls -l drawn on Untitled Diagram.drawio

A parent process should create a child process which will execute command “ls -l” using execlp() and will create its child process. The newly created child process should execute system call command “cat hello.txt” and create a child process. The newly created process should execute system call”whoami” and then terminate. Every parent process should wait for terminating its child process.

Solution : In previous post we have use system() function to execute Shell Commands through C program. In this program I have used execlp() method to execute Linux /Unix Commands .In this solution we have also use waitpid() method in which each parent will wait for its child until child execution.This program uses fork() and execlp() together with waitpid() function as each parent should wait for its child execution. Problem given with following images which will clarify the question.

execlp():In this function p stands for this execlp method it accept program name.

Syntax : int execlp(const char *file, const char *arg, …….);

waitpid():This function can be used to wait for a specific child process to exit with repeat to Child pid.

Syntax: pid_t waitpid(pid_t pid, int *child_status, int options);

Code :-

Output on Shell/Terminal :

--

--