What Happens When You Type ls *.c?
You may be new to coding. You crack open a coding book with exercises, or even enroll in a coding school, and the first thing you see is shell exercises. You ask yourself, “What? Why do I need to know this when I just want to code? Why can’t I use the GUI instead?” Well, this little example will show you the usefulness of knowing how to use the shell.
Let’s consider the command:
We will start by breaking this down step by step. When you open your terminal application, type
ls, and hit enter, you are greeted with a list of the files and directories in your current directory. If you want to know more, type
man ls for the manual page.
Next we have the asterisk, which represents anything and everything. If you enter
ls *, you will see all the files and directories in the current directory, along with the files and directories the next level down, which are inside the directories that are shown with just
ls. For more information, see the Wildcards section of learning the shell.
That lists too much information to be helpful, so we will add
.c onto the asterisk.
*.c represents every filename that ends with a
.c. Anything can come before the
.c because the asterisk represents anything. In the below picture, I’ve navigated to a directory on my computer that has a lot of C files. As you can see, entering
ls *.c brings up all my C files in this directory, and none of my other files (header files). You can try this exercising by navigating to the directory where you keep your code files, and entering
ls *[extension] with the filename extension of your choice.