What happens when you type `ls -l *.c` in the shell?
INTRODUCTION
In this occasion, and as part of the “simple_shell” project, we have to write about the “ls -l *.c” command and its operation process in a Shell. However, before mentioning it, we will introduce ourselves to the subject by explaining what the Shell is. Also called a command interpreter, it is a program that provides to the user, an interface to access the files of the operating system, through the translation of commands that the user passes to it, and that give instructions that only the Kernel of the operating system can understand. Depending on the interface, these can be:
-?????????Text lines
-?????????Graphics
-?????????Natural language
Commands
The commands can be of 4 different types:
-?????????An executable program
-?????????A command integrated by the Shell itself
-?????????A Shell function
-?????????An Alias
The Shell, in addition to being able to be used from the command line, also allows the interpretation of shell-scripts, which are also called "command scripts", which contain a set of orders and instructions that it must consider and interpret.
Having given this brief introduction to the Shell and the commands, let's get into the subject of the “ls” command.
Basically, what this command does is to list the contents of a given directory that we pass to it, or else, it will list the contents of the current directory we're working on. But, although that is what we see as output when executing the command, a series of processes take place behind it prior to showing the output of the command to be executed, in our case, the “ls”. Once we type the command and press the "enter" key, we enter a three-step process. First, the Shell will try to match a command input to the command line with a system-defined alias. Otherwise, failing to match an alias, the system will go to the second process, which will match the command passed through the command line with a command that is built into the shell. These are commands that are integrated into the Shell itself, and do not require the execution of an external program for their own execution. Once neither an alias nor a built-in command is matched, the Shell will proceed to match the input passed to the command line against a process identifier. The "ls" command is an executable program identified by a PID (Unique Process Identifier), and what the system does is look for the corresponding PID in another environment variable, called "PATH", which contains a list of directories and paths separated by “:”.
The Shell will go through these routes one by one, until it finds the corresponding identifier. In the case of the "ls" command, it is found in the path "/usr/local/bin", and once the path is found, the Shell will proceed to execute the command.
领英推荐
But, as we previously mentioned, the "ls" command fulfils the function of giving instructions to print the contents of the current directory, in case we establish a specific directory, it will print the contents of that specific directory. However, this command can be passed to the command line with what we call "flags". The flags can be defined as parameters that we pass to the command on the command line.
The particular parameter on which we will detail below is the “-l”. What this does is make the command list the contents of the current directory, showing the user more detailed information about the files in it:
-?????????File Name: Name of the directory or file
-?????????Modification Time: Specifies the last time the file was modified
-?????????Size: Detailed file size in bytes
-?????????Group: Name of the group that has permissions on the file in addition to the owner.
-?????????Owner: Name of the Owner of the file
-?????????File Permissions: It details the permissions that the file's owner, the file's group, and everyone else has.
Once the “-l” parameter has been specified, we move on to the last parameter that we passed to the “ls” command, which is “*.c”.
Para comenzar a explicar la función del “*.c”, tenemos que entender que son los “Wildcards”. Estos, hacen a los comandos más poderosos, a través de parámetros que filtran y modifican la ejecución de un comando. Estos, son caracteres que permiten seleccionar los archivos basados en ciertos patrones o caracteres. Para nuestro caso en particular, veremos el caso de “*”. Este carácter, realiza la función de coincidir los archivos con cualquier carácter que le pasemos a continuación. Y con estos, podremos realizar búsquedas con mas criterios, y para nuestro caso en particular, “*.c”, el comando “ls”, buscara y listara todos los archivos que contengas los caracteres “.c” en su nombre.
To begin to explain the function of the “*.c”, we have to understand what the “Wildcards” are. These make the commands more powerful, through parameters that filter and modify the execution of a command. These are characters that allow you to select files based on certain patterns or characters. For our particular case, we will see the case of “*”. This character performs the function of matching the files with any character that we pass below. And with these, we will be able to carry out searches with more criteria, and for our particular case, “*.c”, the command “ls”, will search and list all the files that contain the characters “.c” in their name.
As a summary, the command “ls -l *.c”, will fulfil the function of listing all the “.c” files in a specific directory (if specified), or in the current working directory, with extra information over each file.
Authors: Mateo Arbini / Valentin Repetto