¿What happens when you type ls -l in the shell?
In this little blog we will see, what is ls -l and how this command works when used in our Linux console or any other work terminal. The ls command is one of the basic commands that any Linux user should know, it is used to list information about files and directories within the file system.
The very primary thing that lets you to start typing in the shell is a prompt. In most of the Linux distros, you’ll see it set as I by default in the PS1 variable.
So, when you write ls, the shell reads the command using the getline() function’s STDIN data stream. It will store the input into a buffer as a string.
Now, the string is cracked into tokens by removing whitespace and is stored in an array of strings.
After that, it checks if any token has an alias defined. If there’s an alias defined for the token, then it will replace it with that particular value. The next step is to check if any token is a built-in function. Since built-in functions are treated differently by shell voluntarily. For example cd, echo, help are all built-in commands.
If it’s not a built-in function, it will go to find the PATH variable in the directory. Since it holds the absolute paths for all the executable binary files. Each location specified in the PATH variable is separated using “:” and searches recursively by appending the command at the end of the path.
For example: usr/bin will be examined by appending usr/bin/ls. As well, since it searches recursively, it will first search in the pwd and then its parent and so on with all other commands.
When it finds the binary for ls, the program is loaded in memory and the system call fork() is made to creates a child process as ls and the shell will be the parent process. The fork() returns 0 to the child process so it knows it has to act as a child and returns PID of the child to the parent process(i.e. the shell).
Next, the ls process executes the system call execve() that will give it a new memory space with the program that it has to run. Now, the ls can start running its program.
The syntax for the ls command is as follows: ls [OPTIONS] [FILES]
When ls program runs, it will generate a list of directories or job files in format short.
The -l option causes ls to print files in a long listing format.
When the long listing format is used, the ls command 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
In LS -L we can also use extra flags that generate different types of lists with more specifications depending on the type of data required and can be used in different ways, together with “-l”, or separated by another “-”, as we will see in the following examples.
“ls -l -a” or “ls -la”
This type of listing prints the folders in a given directory that are hidden, with the specifications of each folder.
“ls -l -h” or “ls -lh”
Display the list so that you can see the weight of each folder in a user-friendly way, change the weight of the directory or folder and print it in G or K.
“ls -l -t” or “ls -lt”
Displays the list in long format with the dates of modifications of each folder or directory, from the folder that was recently modified to the one that has not been modified.
Among these options we can use combinations between flags to generate a list with more details, for example a long format list with hidden folders and last modifications that are written like this: “ls -lta”.
The following is an example of what the most specified list would look like:
We can see that our command prints a long format list with the hidden files and the file with its modified date.
We can make different combinations, for the data that must be obtained from each directory or file, such as ls -lha or ls -lht, among others.
Once ls process is done executing, it will call the exit() system call with an integer 0 that denotes a normal execution and the kernel will free up its resources.
The shell will free up memory, exits, and re-prompts the user for input.