What happens when you type “ls -l*.c” in a shell and hit Enter?

SwaeStone
3 min readAug 24, 2021

--

Photo by Alex Kulikov on Unsplash

What is the shell?

The shell is simply a program that takes commands from the keyboard and gives them to the operating system to perform. Shell is a UNIX term for interactive user interface, the shell allows a user to request actions of the kernel.

What is the kernel?

The kernel is a computer program at the core of a computer’s operating system which completely controls all actions in the system. It is always resident in memory and facilitates interactions between hardware and software components.

What is the function of the “ls” command?

The “ls” command is used to list the files in a directory, the “ls” on its own will list all the files in your directory except for hidden files.

What does the “ls -l” command do?

The “ls -l” command when entered in a shell will give a long listing of all files in your current directory, the “-l” switch turns on the long listing format.
Here’s an example of its output:

Now, what exactly happens when we type “ls -l*.c in the shell and hit enter?

  1. The command is read by the shell using the “getline()” function. The getline() function is a standard C function that will get the line from the shell’s command line.
  2. The shell will then check to see if “ls” is an alias (a non-default command declared by the user).
  3. Upon verification of “ls” not being an alias, the shell will check that “ls” is a built-in command. The shell searches for an executable “ls” file in the system in the “$PATH” variable. “$PATH” is a variable that stores a list of directories that shell looks through whenever a command is entered.
  4. The -l switch turns on the long listing format and will prompt shell to give a long listing of all files.
  5. The shell performs the necessary expansions. In this case, the shell needs to expand “*.c” into a list of files and directories.
    The symbol “*” is referred to as a wildcard and it tells the shell to look for all the files whose name matches a given set of characters. The characters that either precede or follow our wildcard “*” will be searched for by the shell and any matching files or directories will be returned. The extension “.c” follows our wildcard “*” so the shell will return a long listing of all the files or directories ending with a “.c”.
  6. The “ls” command does not act directly on our “.c” extension since the wildcard expansion takes place before the “ls” command is executed.
  7. Finally, we get a long listing of all the files or directories ending with a “.c” extension in the shell. If the current directory that you are in does not contain any files or sub-directories with either the “.c” extension, an error message stating “No such file or directory” will be displayed in our shell upon execution of the “ls” command.
  8. After execution of the “ls”, command the shell executes exit commands, frees up memory, and re-prompts the user for input.

--

--