What is ls -l and how does it work?
Inside the shell, there are several methods of going about acquiring information. There’s a shortcut for just about anything. Here, we’ll be looking at the command ls with the flag -l.
ls will show the files and folders in the current working directory. By looking at the man page for ls you can see there are many different ways to further refine that data depending on what information you want.So we know that ls will display the files and folders in your current working directory. When you add -l, it displays the long format of the file path.
So we know that ls will display the files and folders in your current working directory. When you add -l, it displays the long format includes additional information like permissions, number of links or directories in the directory, the user that owns the file/directory, the group the file belongs to, size in bytes, the date and time it was last modified, and the name of the file.
How does this work?
Firstly, the shell displays the prompt, using PS1 (prompt string one) requesting a command given by the user. When the shell reads ls — l STDIN (Standard Input), and parses this into arguments to the executable upon inputting the command. If ls is an alias (a shortened name that the shell translates into another longer command), its value will take whatever was designated. Otherwise, if not an alias, ls functions to its default setting.
After the alias check, the shell first checks if the command is a built-in. Built-ins are programs that are part of the shell. If the program is a built-in, the shell executes the command directly. If the command is not a built-in, the shell will search for the program file in the PATH environment variable, which contains directories where executable files external to the shell can be found. Environment variables store session information for the shell to use when it executes commands. The PATH is parsed once where each token is a potential file path where an executable could be located. The command is appended to the end of each token. Then the file’s existence and execution permissions are checked at that location. If both checks pass, then the file is executed using execve, which is discussed below. If the checks don’t pass, nothing will be executed, and an error message along the lines of “command not found” will be displayed.
Ls as an executable file can be found in /usr/bin/ls (/usr/bin is where most executables can be located). Ls is then executed with three system calls. First fork clones the parent process and creating a child of the original parent process.
Then execve ceases the replication of the parent process, starts the ls program and replaces the current memory with what is loaded from ls.
While doing this, the parent process checks the child process with wait. Finally, ls -l processes through exit commands, frees memory and brings up a new prompt for the user.
Written as part of a group project by Ashley Price and Maddi Laizure.