Under the Hood of the Command “ls *.c”
Have you ever seen this command (or one like it) and wondered what’s truly going on? Are you getting into bash scripting and want to know how the ls
command works? Do you have no idea what any of that means? Then this post is for you.
First of all, what is Bash?
Bash is a command-processor that is run through a terminal (window containing only lines of text). Bash is an interface between you and the system to make changes to the computer or get information from it. Bash runs on most Unix-based systems such as Linux, BSD, and MacOS.
How does one use bash?
When a bash terminal is opened, there is very little on the screen, but what is there is very important.
For many, this is what the bash terminal would look like when opened initially. Take note of the info between the :
and the $
. For now there is just a ~
, but this is the directory that we are in. A directory works like a folder in your computer, it can hold files and also other folders. The ~
signifies that we are in the home
directory. This will come into play later.
It is important to note that there is no clicking in a terminal. All operations are done through the keyboard.
Commands With Bash
Now that we are familiar with bash, how does this connect to ls *.c
? Well, ls *.c
is a command within the bash interface. A command is how one tells bash what to do. When a command is typed, nothing happens until the enter
or return
key is pressed, letting bash know that you are done typing the command and ready for it to be executed. After the command has been run to completion, bash will return to being ready for user input.
Let’s go back a step to: “When a command is typed”. In this post, we will only be covering simple commands in bash, but there are other, more complex types for more complicated tasks. How is a simple command typed? The simplest structure of a simple command is as follows: name [argument(s)]
. In this case, name
is the name of the command. In our case the name of the command is ls
and the argument is *.c
.
What does this mean?
Let’s start with the ls
command. At its simplest, the ls
command lists the contents of a directory. Seems simple enough. Want to know which files and directories are in a certain directory? Use ls
. Let’s try it.
Look at all this text! I didn’t type that! The second line seen in the picture is the output from the ls
command (entered in the first line). So what is this stuff? Well, it’s “the contents of a directory”. There are files (white) file.txt
, foo.c
, HowToRuleTheWorld.pptx
, and source.c
. There are directories (blue) codes.c
, holberton
, memes
, and pictures
. Lastly, there is a graphics file (pink) cat_meme.gif
. Now we know all of these files are in a directory, but which directory?
“ls” Syntax
So, it’s possible to run the ls
command without any arguments, but we have an argument in our command, *.c
. Let’s look into how ls
handles arguments. The manual for ls
shows that the syntax is: ls [OPTION]… [FILE]…
. We didn’t specify any options or files, but we still got an output? Both option
and file
have []
around them, showing that they are optional arguments, not necessary in order to run the command.
Let’s focus on the [FILE]
argument for a moment. This argument specifies which directory we want to list the contents of. We didn’t give a [FILE]
argument, so which directory were we listing the contents of? The manual also informs us: List information about the FILEs (the current directory by default)
. The contents listed in our example are in the directory that we are currently in, which is the home directory (~
).
In the command that we’re focusing on, we have one argument for [FILES]
, *.c
. This option means that we are only looking for files or directories that match this pattern. The obvious indicator of this is the asterisk (*
), known as the wildcard. The wildcard character represents any character, characters, or no characters at all. The .c
is the rest of the pattern, meaning *.c
will match any file or directory that ends with .c
. The command will then list all files that match and then list each directory that matches along with all of its contents (not just the items that match).
The [OPTION]
argument in a command is used for changing how the command works in small ways. For example: the -a
option for the command ls
would list all files and directories, including ones that have names beginning with .
(also called hidden files).
Conclusion
From what we’ve learned, the command ls *.c
will list all directories (and contents thereof) and files ending with .c
. Let’s try it out.
As we can see, ls *.c
first lists the files ending in .c
: foo.c
and source.c
. Then the directory that ends in .c
is listed: codes.c
along with all files: cookies_recipe.txt
, index.html
, and main.c
and directories within it: styles
.
That’s what happens when you type ls *.c
into a bash terminal.