Do you really know how work ls*.c?

Olivier Guyot
6 min readSep 14, 2020

Everybody use the ls shell command in Unix systems. It’s one of the most common used command in linux but how does it work?

First what happen when we write ls?

ls is used in order to interact with a computer by using a command line. When we put a command line in a shell ( shell is the name of the command interface), it will launch the command ls.

Then once the command ls is done the bash will print the prompt again.

How to compile a C file ? with gcc !! BUT what happens?

If you do C , of course you have already compile your file. Lets go deep in the command.

Let’s go to the begining. The Terminal !

When we open the terminal, our shell will check the PS1. Don’t worry about that word, it’s just the primary prompt which is displayed. ( hint, if you want to be the BEST for you friend, you can personalized you prompt by modify it. Their is different prompt PS1, PS2 ,… . By modifying these variables, like the PS1. You can personalised your prompt at any level, you can change the form, the color, …or do what ever you want.

Yeah ! you are the boss !

That is my PS1 first prompt

Then, he is waiting a command ! ( hint, if you want to display your PS1 just type echo $PS1.

WOOOH, it looks complicated. AHAH yes a little bit but if you want more information about how to change your prompt, i will maybe do an article en that later, but for now it’s not the subject.

So our terminal display a prompt, and it will display the prompt after each command ! because he is waiting to speak with us.

It’s our time to speak. YEAH, now you can put some command in the terminal.

If you put a command, the terminal will first check if there is a what we call an Built in command.

What’s a built in command?

A build in command are command built directly in the bash . All the command can be found in a specified directory but we will see it after.

Just understand that your bash will first check if there is a built in command in a specified directory define in the PATH.

For now, it’s complicated but we will talk about that PATH variable later in this page.

What’s an aliases?

“An aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias builtin commands.”

So, when we put a command our shell will first check if there is some aliases to replace it.

In the common live, an aliases it like a word shortcut,

Prompt? OK, Aliases ? OK, and now ?

Now there is the use of a command. all the command of our shell are specified in a specifics directories, The location of these directories are specified in the PATH environmental varaible.

Here an example of a PATH.

So all our command are in a directory of my computer and i can call them?

YES!!! you get it.

What do the command ls when it’s used with a directory name?

When ls is used with directory name, the basic syntax of this function is :

$ ls [directory1] [directory2] …

In that case, directory1 and directory2 are the argument. These arguments (an argument is an informations that we can give to a function) are used to specify in what directory we want to use the function ls in order to display their files.

So when we write :

$ ls directory1 directory2 …

ls will display all the files in both of this directory.

What do *.c ?

*.c is the syntax used to select the suffix of a file name (it means the end of the file)

In a shell unix there is some special character called wildcards. Some particular wildcards have the ability to launch a special process called a Filename Substitution. Don’t stress on this word, it’s only the name of the process used in order to replace this special caractere by a list of file name which meet a condition * is one of those special character.

According to the linux documentation *: “Matches zero or more occurrences of any character”.

I know, it’s not really easy to understand,

But you will easily understand it. When we use * it means the condition will be true for any character.

So when we group * with an another chain of character ( here the “.c”), the condition will be true for a list of any character which end by “.c” In other word, it will be true if the suffix of the file end by “.c”

So far we understannd what do *.c and what do ls.

What happen when we use it together.

When we put in the terminal : ls *.c .The command ls will display all the files which meet the condition returned by *.c. So in other words, it will return all the files which end by .c

Easy right?

A small example to illustrate.

As we have seen at the begining ls can be use to display the name of all the file in my current directory.

My terminal return the list of the file in my current directory : Myfile1 Myfile2.c Myfile3 Myfile4.c

Now, if i used ls *.c in the terminal

My terminal will return all the files ended by .c : Myfile2.c Myfile4.c

So in the background, when we put ls in the terminal, we say to the terminal to use the ls command. Then as we have seen the command ls will check if the condition *.c (which means end by .*) is meet, if it’s true it will display it.

In that example :

Myfile1 : is not ended by .c , the condition is not meet it will not be displayed
Myfile2.c : is ended by .c , the condition is meet it will be displayed
Myfile3 : is not ended by .c , the condition is not meet it will not be displayed
Myfile4.c : is ended by .c , the condition is meet it will be displayed

Thank you for reading, if you want to get more information in linux, FOLLOW me or contact me i will be happy to share with you.

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

Olivier Guyot
Olivier Guyot

No responses yet

Write a response