What happens when you type ls *.c
A real story using shell
Sun Jun 23 2019 - 3 min read
When you start to use the terminal as an interface for interacting with your computer you are going to become familiar with all the commands that will give you the superpower of making your life easier and solve complex requirements to retrieve some specific data. Sometimes the terminal as we know it seems complicate but believe me, it could be your favorite program once you start to become familiar wit it, so, let’s dive in, and define some core concepts before using it.
Shell
It is a program that you use to give orders to the operating system, long time ago it was the only way to interact with your system. On most Linux systems there is a program called bash, that stands for Bourne Again Shell that is an enhanced shell.
A terminal is a program tht open a window and lets you interact with the shell, so next time you use the terminal remember that you are interacting with the shell and you are going to give commands using your keyboard.
ls
vagrant@vagrant-ubuntu-trusty-64:/$ ls
This command list all files on the current directory
When you use the ls command and hit enter, it’s going to list the files in the working directory (where you are right now), so, if you have some folders and files it’s going to list all of them, but in the case that you have some hidden files, these are not going to show using ls, in order to show these hidden files or hidden directories you need to use a flag or in other words and option to supercharge the ls command, if you are wondering how to show hidden files you just need to add the option a, for example, ls -a
, but don’t take for granted what I just said, you just need to use the command man ls
and see with your own eyes the different options at your disposal.
ls *.c
Now that you are a little bit more familiar with the ls command, you can guess what the ls *.c
command does, right?, It is going to list all the files in the current directory with the extension .c, but only those files, if you guessed right, congratulations, and if you don’t, do not worry, practice make progress, and remember, don’t take for granted what I just said, do it yourself in the terminal (command line) and see for yourself how this command works and how this command behave if you change the extension to maybe .pdf.
In the example above it only shows the files with the .c extension, in case you don’t have any file ending with .c extension, there is going to show you a message telling you that there are no files or directory with that extension.
This command is very powerful and I know that you are wondering what the * character means, so rest assured, I’m going to give you a brief explanation of what it does, so, the * is a wildcard that is used as a substitute of any character, for example, if you use ls *t.c
it’s going to retrieve all the files that end with t.c like the file alphabet.c.
Last words
My advice is that after you finish to read this post try to understand the concepts that you just learned and experiment with the commands, be creative and look for a problem that could be solve using this command, and when you solve it, you are going to remember more easily how this command works, so have fun!