What happens when you type ls *.c
Several essential commands’ are needed to thrive in a Unix shell one of those is “ls” — list. The ls command offers the simplest way to list all the contents of working or chosen directory while still having a lot of flexible through the use of options — which are subcommand that can be called through the use of a hyphen[-] then generally a letter. These options — also the term “flags” is commonly used — could be thought of as additional conditions or filters.
ls alone without any flags show only visible files and directories(Dir), the image below illustrates this using
ls -a the
-a shows hidden files and dir’s, as a side note the object with “.” in front of the title is considered hidden.
Most commands execute at the current working Dir, ls being no exception. So if we would like to be more specific on what we want to find in the current dir, we could use some wildcards to further cherry pick at our list.
Wildcards are a powerful set of symbols that can be tagged onto or in a body of text to narrow down a search with a pattern. The most commonly seen is the “*”. However, there three primary wildcards that are understood by most commands’ in Unix.
The asterisks is an unbiased wildcard in that it can mean all, up to and after this point depending on how you format your pattern. Which also mean should be used with care when using it with some commands such as
A question mark is a single character match — for example
h?p would match with
hop but not
The square brackets is a single character match while offering a mean to be more specific by surrounding list of desired characters. For example
[Cc]hapter would return both
chapter, however, trying
hapter. The inclusion of hyphen[-] between the character sets a range — this works for numeric and alphabetic chars
Going forward well focus on only asterisks so worry not if the behaviour of the other two confuses you.
What happens when we type ls *.c?
Back to the point of this article. we’ve learned that
ls list all visible files and directories within the current workspace. We’ve also learned that a wildcard is a form of pattern matching.
So instead of attempting to explain what is happening through a large body of text let try breaking this up into steps.
Let begin with the command
ls. I’ve already demonstrated this once at the start, but this time I have a few more file thrown in.
so as you can see after we type
ls then “Enter” we've treated to a list of file some different names and a few almost the same name.
before just running
ls *.c I would like to try another more direct demonstration, In this case, ill use
So I want to run this first cus I touched on
ls and using it with an option, but didn’t mention it could also take another value.
ls has two optional fields and is formatted like so —
ls [OPTION]... [File]... . The point of the example above isn’t so much that you can check if this directory contains a file called
main.c but that you can actually specify that you want to display a file that matches with
As a side note, you can also search for multiple files at the same time by simply typing more name with space in between. This is especially powerful when used with directories.
Without further delay let talk about
ls *.c or rather
*.c part. So now we get to see an example of a wildcard in play, notably an asterisk. The asterisk as I mentioned is pretty unbiased if you were to only use “*” alone you would see something that looks fairly familiar but also a bit or a lot more pending; That’s all I’ll say for this right now since this section getting a bit lengthy.
A good way to think of using the asterisk is to think of it in a more symbolic. Let say “ * “ = Everything, by itself it looks at everything but if you were to say attach
.c at the end now it would read: “Everything with
.c at the end”. Using the example from before rather than “main” if we were to replace it with an asterisk we would get more results.
There we go now using
ls *.c not only do we print
main.c , we now see all other files with the files with extension
awesome you made it this far!
You can see that while ls by itself is useful with the inclusion of wildcard you really can be more than just a command that shows a generic list of files and dirs.
lsis also great for learning wildcards since it’s a safe command to run and not worry about affecting your working dir or system while getting a visual feedback. As you dive deeper into shell scripting you’ll find that being able to use ls and cherrypick your results will pair well with the pipe[ | ] operator, but that’s for another post.