WHAT HAPPENS WHEN YOU TYPE ‘ls -l *.c’ IN THE SHELL
First of all, let’s talk about the shell.
The Linux command line interpreter provides an interface between the user and the kernel and executes programs called commands. It is also called the “user-space”. The shell has to pass it to the kernel, the core program of a computer’s operating system to run programs and start processes. The shell runs system calls as the interface between the user-space and the kernel-space. To understand what (really) happens when you type a command, in the shell, you have to understand what the shell is, with the kernel. The kernel is the central, most fundamental part of a computer operating system. It’s a program that controls all other programs on the computer, talking to the hardware and software, highly involved in resource management. The shell is an application, which is one (the other being GUI) of the two major ways of controlling a computer. For example, if a user enters ls, the shell executes the ls command by listing the files (and or subdirectories) in that directory. Bash, a Unix shell, also called the “Bourne-again shell”, is the most common shell used today. It was written for the GNU project.
What does ‘ls -l’ do?
When one types ls –l, and hits enter, it displays all the files and directories in the current working directory, along with respective permissions, owners, file size, and created date and time in a long listing format.
As shown below;
Recommended by LinkedIn
Under the hood
First and foremost, the shell prints the prompt, prompting the user to enter a command. The shell reads the command ls -l from the getline() function’s STDIN, parsing the command line into arguments that will be passed to the program it is executing. The shell checks if ls is an alias. If it is, the alias replaces ls with its value. If ls isn’t an alias, the shell checks if the word (command) is built-in. The shell then looks for a program file called ls where all the executable files are in the system — in the shell’s environment (an array of strings), specifically in the $PATH variable. The $PATH variable is a list of directories the shell searches every time a command is entered. $PATH is one of the environment variables that is parsed using the ‘=’ as a delimiter. Once the $PATH is identified, all the directories in $PATH are tokenized, parsed further using ‘:’ as a delimiter.
The ls binary executable file will be located [in one of the major subdirectories of the ‘/usr’ directory] in the file ‘/usr/bin/ls’ — ‘/usr/bin’ contains most of the executable files (i.e. ready-to-run programs).
“ls” is the name of the program or command built into the shell to run. “-l” is a special arguments called options, that helps the “ls” command to list the files in a long format, including the permissions, ownership, timestamps and file sizes. Most options start with a hyphen then a letter (the option itself).
Considering ls –l *.c
The new argument added to the command is “*.c”. The “*” is called a wildcard which, here, we can simply say it means all. The “.c” is an extension for files that contain programs written in the C language – in short files ending with “.c” Thus, when one types this command, it tells the shell to list all C files, in the current directory, in a long list format. An example is as shown below;