What happens when you type ‘ls *.c’ and hit enter in your shell?
Here, we’re looking at the first set of commands to navigate across the Linux OS. This post will explain in detail what happens when you are in your shell program and type the
ls *.c. Let’s begin with you turning on your computer and accessing your terminal. When you type in a statement like
ls *.c and hit enter, the shell program executes this statement and outputs certain characters back to the terminal.
Okay, now here she interrupted me and asked immediately, “What’s a terminal? What’s Shell? What does
ls *.c mean? What’s the difference between a command and a statement?” I was just getting there.
What’s a terminal?
It’s a program called terminal emulator. A terminal is a text-based interface that allows you to type and execute commands to your operating system. “So it’s a program that allows you to tell your computer what to do?” Yes, that’s correct.
Shell is a command line interface that takes commands from the keyboard and gives them to the computer’s operating system to perform. As opposed to graphical user interfaces (GUIs), where you can visually see what happens when you click around, Shell is only able to take commands with texts via a command line interface (CLI) and send those commands to the operating system to perform. There are multiples types of Shell programs. “So, it’s the middle man that distributes your message to the computer, but it only does it using text?” Exactly.
The Shell communicates with the operating system, which is the organized mind of the computer. For this example, I’m using the Ubuntu Trusty64 OS distribution of Linux, which is an open source operating system. The Linux OS comes in multiple distributions, each one used for specific tasks. “Okay, so the user types on the terminal, which communicates using the Shell program to deliver the task to the operating system?” You’re getting it now.
ls *.c mean?
Take a look at the picture of my prompt above, in my case, it is the bash prompt because I’m using the bash program. The prompt consists of all the text in blue, green and yellow coloring, ending with a $ symbol. In this example, the prompt would be:
The blue text is the name of the user, the green text is the name of the computer and the yellow text is my current directory. The red square after $ is the cursor. When you type commands in the shell, the cursor moves to the right.
Okay, basics out of the way. Let’s get to the good stuff. What does
ls *.c mean?
ls is a command in Linux that lists out the contents of your current directory. What that means is that the
ls command will list out all the contents of your directory. If you ever have any questions about commands, type
man ‘command_name' into your shell. The man command displays the manual of your specific command in question. Based on the example above,
ls is listing out all the files and directories in my current directory. Let’s move to the next part of the
ls *.c statement,
*.c. As you’ve discovered, if you went to the man page
ls has a statement structure of:
ls [OPTION]... [FILE]...
This means that the
ls command has a structure of taking in options and files, both being optional by the
 around both words. The
... means that it can take multiple files or options.
*.c is the
[file] part of the statement. In my picture example above, if I want to write out a specific file, I can type the file name like this:
and it will list out the README.md file to the shell prompt. The
* in this example is a wildcard. The
* will find all the files in this directory that end with
.c , which are C files and list them out to bash. If you take a look at the first picture again, when I type
ls it lists out all the files. None of those files end with
.c , so when I type the
ls *.c command, the shell outputs an error,
ls: cannot access *.c: No such file or directory because there isn’t a file that ends with
.c . I add a file to my working directory by using the
touch file.c command, then retype the
ls *.c command. Voila! A file with a
.c extension is found.
If you have any questions or feedback, feel free to leave them below in the comments.