Inner-workings of commands in the terminal: ls *.c

From the moment you press the keys, which send electricity down through the circuitboard to the operating system where it… First sentence and it already sounds a little complicated, so how about I tell you where you and I can understand?

Inner workings of the command ‘ls’

display all my files and directories

To average users, these commands that we use daily to help us visually interpret information we need … and its only a couple clicks of the key. Hardly ever do you wonder, why? Why and how does a simple two letter command give us so much information?

Well to keep things a little simple, many hardworking engineers created a whole system to let average users be able to manage the vast world of the operating system called the shell.

The commands we enter can be keywords that let the shell, which is the intermediary between the user and the operating system, interpret the characters we send and the first thing a shell will do is to look for a command with those characters.

Where does it look you ask? There is a special shell variable or a box called PATH that holds information. In PATH contains a bunch of directory paths or locations that many commands and customized commands are generally stored. The shell will tell the OS to go into the disk where PATH physically exists. The shell will give the OS a couple commands automatically to traverse the disk and go through each path to see if the command you gave exists.

Once the command is found (or gives an err if it doesn’t) many more commands are given to load the memory with the code written inside of ls. The memory we are talking about is all temporary, since the OS has to continually move things along. So the OS moves the code inside of the memory to process what it should do.

Lets go a step higher and explain expansion

Typically when you use ls alone, it will display all files and directory inside the current working directory, but if you add a specific file to look for like:

ls 0_0 , if this file exists

or even ls 0_0 hello.c

output: 0_0

0_0 hello.c

This is where expansion becomes interesting. There are a variety of expansions, but we will stick with ‘*’. This is a special character which the shell will interpret differently. The * in particular will have a pre/suffix of characters that it uses to match files that contain the pre/suffix and expands until it matches every single file in the given path (if nothing is provided, it will work in the current directory), which does various things given the commands (ls, echo, etc)

For example:

ls * will display all the visible files and directory

echo d* will display all the files and directory starting with d

and drum roll…..

ls *.c will display all files or directory ending with .c

So to finish this, as you know ls will display files you pass as an argument

ls hello.c >> hello.c, and the wildcard * will store all the files and folders with the pre/suffix you give it.

ls *.c will expand to ls hello.c because remember expansion works before ls has a chance to display anything.

I hope this gave you some understanding of how ls and a little of expansion works. Keep on coding~


Like what you read? Give Shoji Takashima a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.