What happens when you type ls *.c

My first post for Holberton School

Roberto Ribeiro
IT student life
3 min readSep 15, 2020

--

Introduction

Today we will talk about the use of the ls command and its behavior with special characters.

If you are here, you will surely come looking for the explanation of the operation and its step by step. If you’ve arrived by chance, I’ll tell you that we’ll talk about the use of a command that is used in the Linux Terminal.

What happens when you insert a command?

Shell is the program that receives what is written in the terminal and converts it into instructions for the operating system.

In other words, the objective is to execute the programs that the user types at the same prompt.
When entering the command with the ‘Enter’ key, the interpreter analyzes the sequence of characters entered and, if the syntax of the command is correct, executes it, resorting to the functions it offers.

The command “ls”

Let’s review. The command “ls” lists the contents of the directories and by default it sorts them alphabetically. To use it we write it in the terminal and press enter. But its content can be expanded using parameters and wildcards.

What are wildcards and how does the “*” work?

You may be in a system with many file names and need a way to select them according to a pattern. For this we use special characters called wildcards.
For this example we will use the “*”. Its behavior depends on the place we use it and it fulfills the function of autocomplete with any character within a given syntax.

Applying ls with *

If we use the command “ls” followed by a “*”, with a space between them, the system will try to autocomplete the “ls” with any character. In this case, it behaves in the same way as simply using “ls”.

But if we put a conditioner we can see how it works.
For example. If we use “ls *a”, it will show us all the content, files and folders, in the folder that has the ending “a” at the end of its name.

If we change the place of the asterisk and for example we execute “ls a*” it will show us all the content of the directory that begins with the letter “a”.

Using ls *.txt

Let’s look “ls *.c”

It fulfills the same function if we use it by specifying a certain file extension.

For the case we are dealing with today, the command “ls *.c” will show us all the files that end with the extension “.c”. I invite you to try it in your terminal and explore its different possibilities.

References

What is a terminal in linux: http://linuxcommand.org/lc3_learning_the_shell.php

MAN ls: http://man.he.net/?topic=ls&section=all

Wildcards: http://linuxcommand.org/lc3_lts0050.php

--

--