What does ls *.c really do?

The goal of this piece is to explain what happens when a user types ls *.c into their terminal. I recognize that some readers may not know what a terminal is so I will start from absolute basics and by the end of this piece anyone will understand what ls *.c does at a deep, intuitive level.

(i) What is a terminal? -> (ii) What does ls mean? -> (iii) What does ls do? ->(iv) What does ls *.c do?

1. What is a terminal?

When you use your laptop to find files you normally go through your Finder because it makes navigating and searching for files really simple and it’s visual. But, when you use the Finder you are really just sending code to the operating system to run the functionality you want. You are one layer of abstraction away from what is really going on. Software engineers use the terminal because it gets rid of that extra layer of work and also exposes them to a lot of things they would not normally be able to do without the terminal. Below you will see a side by side comparison of using the GUI (graphical user interface) vs. Terminal for the exact same task.

Searching for Vagrantfile via Finder
Searching for Vagrantfile via Terminal

As you can see the Terminal is just another way to navigate the computer’s file system.

2. What does ls mean?

Taken straight from the user manual ls means — list directory contents. Simply put, it basically means “show me everything in the directory that I choose”. If I were to run ls on my Desktop directory I would see a bunch of files and other directories that I can run the same command on.

Note: the directories below are the ones that have a blue folder to the left

Directory contents of my Desktop directory

3. What does ls do?

Software engineers love to use the command line arguments because each argument is written to do a task really well and nothing else. Individually they serve a simple function but collectively they are extremely powerful. ls is tasked to do one thing and one thing only — really well. Its job is to show every single thing located in a specified directory. A directory sounds like a complicated technical term but it’s not. Reasoning by analogy, a directory is akin to a manilla folder inside of a file cabinet. You wouldn’t store all of your important information in one folder would you? No. Computers don’t do that either. They organize files into directories that at a meta level hold files and other directories that are related to one another in some way or another. Looking below you can abstractly represent a directory in a computers file system as one of those white tabs below.

Source

Below I ran a command called tree that basically creates a visual representation of the “file cabinet” of my computers file system for one of my directories.

  • ~: my home directory (this is like the entire file cabinet at an abstract level)
  • .: this is my current directory (think of it as the start of all the manilla files in the file cabinet)
  • holbertonschool-zero_day (blue text): these are all directories. As you can see there is a directory below the holbertonschool-zero_day called 0x00-vagrant which is known as a nested directory. Reasoning by analogy this is like having one manilla folder inside of another manilla folder.
  • 0-hello_ubuntu (black text): these are non-executable files. That means they are files that do not compile down to anything. Kinda like cheques that are blank and waiting to be used.
  • 0-current_working_directory (green text): these are executable files. Going off of the cheque analogy these are checks that are ready to be cashed. They are already compiled.

Where I am going with this is that ls isn’t only used to list directories. Often times it is used to list files inside directories. These files all have different file extensions that make them easy to sift through.

4. What does ls *.c do?

The ‘*’ aka wildcard expansion is a way of telling the Linux operating system to find ALL of a certain criteria. Very rarely does a programmer ask to see ALL without defining something specific so instead it is often paired with a file extension such as .jpg (the file extension for pictures) or .js (Javascript files).

If a programmer was to type ls *.c the computer would interpret this as find all files in the current directory that have a .c (C programming language) file extension.

To simplify this imagine all of the files in your file system are represented by Skittles. The different colors represent different things such as directories, folders, and files.

Source

When you use a wildcard you are separating out what you want from what you don’t want. For example lets say I wrote

ls *.orange

What you would get is something that looks like this

Source

Where all the other colors (files, directories, folders, etc.) were ignored and your search filtered out what you were not interested in.

In conclusion there are so many commands that you can use to solve problem with Linux and other operating systems. They may seem daunting to learn at first but with enough practice and reasoning anyone can learn what they do at a real intuitive level with enough research!

If you want to learn more about Linux commands here is a great link

I write and sell software ex: @auth0, @onelogin, @cisco, @stanfordGSE