BASH Breakdown: ls *.c
For any Software Engineer, being able to work with and understand Shell inside a terminal is an essential skill. One of the most important and fundamental tools used to build that understanding is the command ls.
The Basics
The primary function and namesake of ls is to list files and directories in any given directory. It does this by scanning over every item in the directory, grabbing the name, and printing that to the screen.
As you can see in the above example, typing ls and hitting return will print out the files and directories in the current working directory. In order to list files from a different directory, we can specify the directory's path as an argument for ls.
Multiple Directories
Listing the contents of a directory is useful, but what if you need to list contents from multiple directories? This can be accomplished by typing each directory as it's own argument separated by a space.
This will display the results in an easy-to-read format with each directory being separated by a blank line and having the name of the directory above the results for said directory.
Using Asterisk
When you need to process large numbers of directories, it can get cumbersome to type out every path. Fortunately, there is a solution for that in the form of the wildcard *. This character represents anything. Quite literally. In this case, it can represent any unknown directory name.
Looking at these results, we can see that it not only shows all of the files in the current directory, but also the sub-directories dir1 and dir2.c.
We can also use * to represent an unknown or uncertain part of a directory as shown in the example below. This allows us to get more specific about what we wish to see.
In this example we can see that the command only prints the files and directories with the extension .c except the ones inside dir2.c do not have this rule applied to them. The argument *.c represents anything ending in .c. This means the command will scan over each item in the current working directory and if it ends with .c, it will print it. If that item happens to be a directory, it will also print the contents of said directory.