What happens when you type ls *.c in your shell?

Andrés Sotelo
5 min readSep 20, 2020

If you read this title and think, what are they talking about? Don’t worry, the same thing happened to me when I started reading and understanding about programming.

If you are reading this and you are like me, a beginner in Linux, shell, bash (terms that sometimes can be disturbing for a newbie) and programming issues, this post can help you to clarify some things. Between you and me we can understand several things.

Let’s start with the basics, it maybe sounds obvious, and that is to know what programming concept is. Let’s understand it as the action of creating programs through instructions for a computer to perform the tasks we need, which must be written in a programming language.

That was pretty easy, right? Ok, let’s continue, what is a programming language?

In general terms a programming language is an “artificial language” formed by numbers, letters, words and symbols that allows us humans to communicate with a computer. But, what does this mean? This means that humans and computers do not understand the same language, although sometimes it seems so, when we give the order to a computer to delete a file or play our favorite song, it translates that order into a series of 0 and 1 (binary system), which is almost impossible for a human to recognize, and gives us an output.

This is getting more and more interesting.

What is shell?

Now, to understand a little more about the way we can communicate with a computer and its operating system, let’s see what the meaning of Shell is.

Shell is a program (user interface) like any other you have in your computer, which allows you to access the services of the OS. For example, if you are working in Windows, an example of a shell is PowerShell (image 1). In other words, the shell manages the interaction between the user and the operating system.

Power Shell — Windows

On several occasions, while I was sitting in front of my computer, I accidentally opened this program. Yes, the first time I opened it I panicked because I thought I had damaged my computer. Beginner’s scare.

The way to communicate with the operating system is through a shell script, which contains a set of commands (this is one of our keywords) that can be interpreted by the operating system. These instructions must be entered on the command line, which we will identify by a $ indicator.

Shell prompt. Requesting an imput.

It works as follows:

  • Requesting an input.
  • Interpreting that data or instruction so that the operating system understands it.
  • Returning a response output from the operating system.
  • Returning to the initial state of command waiting.

What is a command?

Basically, commands are the instructions that we enter in the shell, so that the system returns an output. There is a very long list of commands, with their options and modifiers. One of the most basic ones we find in the shell is the ls command, which allows us to list the contents of a directory.

One of the most basic ones we find in the shell is the ls command, which allows us to list the contents of a directory.

If we use it without any of its options and modifiers we will get an output like this. Where the system is showing us the contents of the current directory.

ls command

Let’s see now one of its modifiers. If we use the command ls followed by -l we will see that the system lists the contents of the current directory in long format, showing permissions, file size and even date and time of its last update.

ls -l command

This command is an important tool within shell navigation. To answer the question in the title of this post, let’s see what happens when we type ls *.c in the shell.

Wildcards

We already saw in a basic way what the ls command does. Now, let’s understand the structure of the command we’re going to use, it has the main command ls followed by a * that in shell refers to a wildcard (or meta character).

Now, let’s understand the structure of the command we’re going to use, it has the main command ls followed by a * that in shell refers to a wildcard

Wildcards are special symbols or characters that represent or replace other characters. But, what the he** does this mean? It means that the shell interprets these symbols as an indicator that a character or series of characters is in that position. Let’s see this with the example below.

  • If we use * before a character (i.e. *p), we are telling the shell that we are referring to everything that ends in “p”.
  • If we use the * after a character (i.e. p*) we are referring to everything that begins with “p”.

There are many wildcards that can be used in the shell.

Using the command ls *.c

Let’s see with an example what happens when we write “ls *.c”.
We must clarify that when we refer to the extension of a file, these are preceded by a “.”

  • If we are talking about image files we see them as .jpg or .png
  • If we refer to text files we see .txt
- ls list the files of the current directory -

So, when we write “.c” we are referring to files written in C language and when we use ls command followed by “*.c” we are telling the computer to list all the files ending with the extension .c

Let’s try in the terminal

- ls *.c matches all .c files -

If you liked this post, don’t forget to share it with your friends and I hope it will help you learn more about shell and commands as I learned while writing it.

See you at the next!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response