What Happens When You Type in ls *.c?

What does a restaurant have to do with typing in ls *.c? Read to find out! (Credits to WordPress)

Comments

Since writing this article, I’ve learned more about the shell (and created my own version of it!) and how it works! If you want an updated version of this article that goes into more depth, check out my latest article — What Happens When You Enter Commands Into The Shell?

Your mission?

To understand what happens when we type in ls *.c into our command line.

To get an idea of what is in store for us, let’s see what happens when we type it in:

Typing ls *.c into our command line.

So what’s happening under the hood?

Let’s take a look! We’ll begin with building a basic background that we should understand before we delve in deeper.

Background Information

Operating System (OS)

The Operating System is a software composed of the fundamental set of programs that are necessary to run your computer’s basic functions. Linux, for example, is an Operating System (OS). There are many programs in the OS, but we will focus on two — the kernel and the shell.

The Kernel

The kernel is the core program of your Operating System. As a result, the term “OS” is often used interchangeably with kernel. The kernel acts as a bridge between your hardware and software.

The Shell

The shell is a program. It takes your input and sends it to your kernel to perform. There are many types of shells — the default shell on Linux is bash. As a result, you might hear shell and bash used interchangeably. To avoid any confusion, you should remember that shell is a general term that can refer to any shell, whereas bash simply refers to the bash shell.

Now, where does the shell obtain its input from? Can I simply type random keys into my keyboard and expect the shell to work its magic? Well, not really. You need to type your input into the command line for any magic to occur. Wingardium Leviosa!

The Command Line

The command line sounds like what it is — it is the line where you type in your commands. When you press enter, you send the command to your shell.

See that white block? That is the start of your command line!

Where do I find a command line? Let’s take a look at the terminal!

The Terminal

You find your command line in the terminal! The terminal is a program (called the terminal emulator) that provides an interface for typing in commands. As a result, the terminal enables interaction between the user and the shell.

Now for the final question (you are almost there!) — what is a command?

The Command

A command is an instruction given by you, the user, to the computer.

There are two categories of commands — built in and external commands.

Built in commands come with the shell (Think pre-furnished apartment where the apartment is the shell and your furniture are the commands). This type of command is often called a shell command.

External commands do not come with the shell and are added by you, the user (think buying new furniture for your apartment).

Most built in commands (like ls)have the following format:

commands -options arguments

A command is an instruction given to the computer.

An option is a customization on the command’s behavior. There will always be a ‘-’ before your options.

An argument is what the command acts on.

Dance Break!

Now that we’ve finished reviewing the fundamentals, let’s take a short dance break!

Property of The Office (NBC)

Now, let’s return to our mission — what happens when we type in ls *.c in our command line?

Understanding ls *.c

There are two parts in ls *.c. The command, “ls”, and the argument, “*.c”.

First, let’s take a look at the first part — the command “ls”.

“ls” is a Linux shell command (recall, a shell command is a built-in command that comes with your shell). The “ls” command tells the computer to list the files (Directories are also files. For more information, see article on hard links versus symbolic links). If an argument is not provided, then all files of the current directory is supplied. If an argument is provided, then the files that satisfy the argument are supplied.

What exactly does this mean?

Typing “ls” into our command line is analogous to commanding to see the menu of a restaurant. Once the command is entered, we are given the menu and can see all the options that are available.

There can be many different categories in the menu — entrees, appetizers, and drinks. These are like your directories.

In each directory, there are specific options. For example, if we took a look at the drink directory, we would find things like water, soda, and juice. These are like your files!

Now you might be wondering — How does the shell know that ls is a command? Let’s delve in deeper.

Imagine this — You are sitting at a restaurant and you’ve been handed a plate of the most delicious food in the world! But wait, there is something in it that is causing a severe allergic reaction. Looking down at the plate of food, you realize that you have absolutely no idea as to what is the source of your bloated cheeks and red eyes.

If you want to find the root of your allergic reaction, then you have to break down the dish so that you can determine the ingredients behind it. When you do so, you find ingredients like bread. Since bread is made from several ingredients, we can expand bread into its separate components, such as yeast and flour. Once everything has been broken down, you will have a list of all the base ingredients in this dish. From there, it is up to you to look up the properties of each ingredient and find out what you are allergic to!

Let us apply that analogy to commands. When you type in a command, the command is sent to the shell as a string of characters (your plate of delicious food). In order for the shell to understand the string, the shell needs to break it down into tokens (ketchup). A token is a basic unit of code. Once this step is complete, the tokens undergo expansion (yeast, flour). From here, the expanded tokens have been successfully broken down to commands and arguments that the shell can understand and utilize.

Once this process is complete, the shell has to search for the command (looking up the properties of each ingredient). First, the command is checked with a function list. If the command is not a function, then the shell will search the built in list. If the command cannot be found in the function list or the built in list, the shell will begin searching through the directories listed in PATH. If the shell was able to find the command in at least one of the three aforementioned categories, then the shell will begin executing the command in a separate execution environment.

Second, let’s take a look at the second part — the argument “*.c”.

Let’s return to our menu analogy. What if we wanted to see the menu at a specific restaurant? How would we specify that? That is where the argument comes in!

If we want to see the menu at Mcdonald’s, we would do the following.

ls *.c -> see_menu Mcdonalds.

Property of Mcdonald’s
Property of Mcdonald’s

There you go! Now you can see all of the menu’s offered from Mcdonald’s.

Let’s apply this logic to ls *.c. We want to see all the files that have the format “*.c”.

Results from typing ls and ls *.c

After seeing the results — you might be wondering why disney*.cmovies was not listed. To understand that, let’s learn more about the ‘*’ character.

The “*” is a wildcard character. The shell uses characters like these to search for filenames. The “*” represents any character and any number of character. Another way of understanding this is to look at hamburgers again!

Say you must have cheese and one bun on the bottom of your hamburger, but you do not care about anything that goes on top, this would be equivalent to the following -

“*” = Anything on top of my burger

“.” = Must have a layer of cheese

“c” = Must have one bun at the end

Anything that matches this format will be listed! Therefore, the disney*.cmovies was not listed because the “layer of cheese” and “bun” was not at the end of the hamburger.

In summary, typing in ls *.c will present a list of files that correspond to the format specified in the argument. And to truly understand what happens? Well, we have to take a look at the shell and kernel… and take a trip to a restaurant!

Sources:

https://www.tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html#sect_01_03