What happens when you type ls *.c into the shell

Sebastian Orozco Marin
3 min readSep 29, 2020
Photo by Markus Winkler on Unsplash

The shell

The shell is a computer program in charge of taking the commands entered by an user and send them to the operative system in orden to perform a task. The most common shell on linux distributions is called bash (Bourne again shell).

The commands passed can be of different types, which are:

  • An executable program: which is usually wrote in C, C++, Python, Perl etc.
  • A built-in command: this commands are proper programs of the shell as for example our ls command.
  • A shell function: little programs executing dedicated task.
  • An alias: This commands are allowed to be named and usually contains a group of other commands to execute a task, this alias helps to save time and typing for repetitive task that involves invoking several number on the terminal shell.

How commands are built

Commands have an structure that is always described on his manual, you can access this information in the terminal with the command man followed by the command of interest, our ls command have the following structure:

ls [option] [file]

The options are modificators that gives us different behaviors for the command, and the file is where we want to execute the action. The ls command executes a listing of the files and directories on the selected path and with the different option we can show this results in different ways for example long format listing or short format listing, all the options are listed on the manual page.

Wildcards

These are special characters that allows the user an efficient manipulation of files according to special patterns present on each of them. There are several wildcard that can be seen in the following link:

but for now we are going to focus on our (*) wildcard. This special character that matches one or more characters of a file name.

What the command ls *.c performs

Before our command is executed the wildcard is interpreted, this interpretation is called an expansion, this is performed and passed to the command to execute his task, so the result is the listing of all files and directories ended by the .c extension, we can see this behavior on the next example:

This is our original list on the directory,

After we execute the listing command with the wildcard there is a filter of the documents according to our pattern, we show the result in the following image

So as we saw wildcards allow to filter our files to perform on them different tasks. This is a great tool that will allow you to save time and headaches.

--

--