What happens when you type ls -l *.c in the shell?

Eddie HG
3 min readApr 13, 2021

When writing ls * .c in the shell, we must first understand the concept of expansions. The shell will take specific metacharacters and expand them to something else before interpreting the command. The asterisk * is one of those expansions, also known as a wildcard. This is used to match any specified character.

The shell executes any command starting from PATH, one of the global variables. These affect the behavior of the computer and therefore determine the environment of the computer. The PATH variable lists all the directories that the shell must search to find executable files or files that perform various operations or functions.

In this example, / home / eddie, /local, / bin / usr, / local /sbin, and / usr / local / bin are the directories listed in the PATH variable on my computer. The shell will cycle through this list of directories, in the order they appear, until it finds the file it is looking for. In our case, the file for the ls command is contained in the / bin directory. The shell will then run the script with whatever input is listed on the command line.

ls: list directory contents.

ls uses opendir() and readdir() to step through all the files in the directory.

opendir(): Function opens a directory stream corresponding to the directory name, and returns a pointer to the directory stream. The stream is positioned at the first entry in the directory.

readdir(): Read a directory, function returns a pointer to a dirent structure representing the next directory entry in the directory stream pointed to by dirp. It returns NULL on reaching the end of the directory stream or if an error occurred.

l or log: use a long listing format.

This will bring up more details in the file search. Namely, long format, displaying Unix file types, permissions, number of hard links, owner, group, size, last-modified date and filename.

*: All files

The Asterisk is a symbol that we call a wildcard in the Command Line for of the Shell.

.c: File related to extension C.

It’s the part more specific as only search with extension C.

EXAMPLE

1. ls

2. ls -l

3. ls -l *

4. ls -l *.c

--

--