What happens when you type ls -l

There’s adorable cats inside

Sam Scislowicz
3 min readDec 22, 2016

The ls command prints all of the subdirectories and files within a given directory. The -l is a flag which makes the output listed in long format.

So you type ls -l in terminal and get a list. Cool, but how did the computer make this list? That’s what you’re about to find out.

Yeah my terminal is pretty sweet

Lets get started:

The computer is going to have to read the input from the user. Makes sense, but how? This is a lengthy process. What the computer must use is something called system calls.

The system call the computer will use first will be read. Because it needs to read what we told it to do. Pretty straight forward. The way a computer reads is different than the way humans do, kinda. The read system call saves the input in a buffer and sends it along to the next steps.

Before any real work is done the computer is going to save the command in its history file. It just keeps a history of the commands you have entered. For you know, fun.

Then the computer is going to check to see if you have given it an alias. An alias is a command that basically re-directs you to a different command. So for example you could tell the computer to alias the command ls so that it doesn’t mean ls, it now means open up a picture of an adorable cat. If it finds a command has an alias then it will go onward to open your picture of an adorable cat. If it doesn’t find an alias, it will continue on the riveting journey to ls -l.

I wouldn’t even be mad.

The computer is going to use the fork function. Forking just makes a copy of the process for the command to run. They have some weird names for these processes. The initial process is the parent, then the forked process (or the copy) is called the child. So at first the child is just like the parent, and exact copy. It just gives the computer a separate space to run the child process without messing anything up on the parent process.

And now your command goes down the $PATH. It’s like a place where all the commands live. ls lives in /bin/ls. So the computer finds the home of ls and now the ls command is ran with the execve system call. The execve system call will execute the program.

Yes. The program is finally executed. ls -l prints out and then the program exits. The child dies. I mean the child process. The prompt is printed using PS1 (prompt string 1) and it patiently waits for the next command.

And as a thank you for reading through the entire process, here’s some more adorable cats.

--

--

Sam Scislowicz

Software engineer student, creator, avid reader, photographer.