What happens when you type ls -l in the shell

Rodrigo Sierra Vargas
4 min readApr 16, 2019

If you are a newcomer in Linux, probably this information will be worth for you because one of the first things you need to see is a list of existing files and directories and its properties. “ls” is one of the most commonly used commands, so it is really important to know its use and options to have the exact data.

When you start working with a Linux shell you have a strange sensation of knowing nothing because of this image:

What to do? What is the next step? OK, calm down and type ls and you will have a list of the files in your current directory, something like this:

Simple right? but maybe not at all, there is a whole process behind this listing, there are some actions the machine needs to execute before showing the files and here there is a guide through that procedure.

In the image, you can see files in white, blue and yellow letters, but what is that? those colors explain the type of file, blue for directories, yellow for executables and white for text files you can edit. The setting of those colors come from a thing named alias which you can see by typing alias in your shell, then you can see that ls has an auto color set to provide you a better visualization of files.

Another thing to know is that ls is an executable file, it is a program, but where is it? how does the machine do to find and execute it? In Linux, you are always in an environment that has settings and you are able to know them by typing one command: printenv, and that way is possible to see the environment variables, and you can find one named PATH, where there are addresses of directories separated by colons where the machine search for an executable file named ls, which in this case is allocated in the /bin directory.

Now it’s beautiful to know that ls command has multiple options you can consult in the manual, where is seen the basic structure:

ls [OPTION]… [FILE]…

The OPTION and FILE are optional, as we saw before, but to show more information about the current directory you can use the -l option that shows long content, for example:

This command has super powers when combined with wildcards, Commands can use wildcards to perform actions on more than one file at a time, or to find a part of a sentence in a text file. For example, when you want to list specifically files or directories with a special feature in their name, you use a wildcard to tell the command to search for a character string according to what you indicate and omit others, with the wildcard * you can do this.

Command ls with wildcard *

As you can see in this case the command lists the files or directories omitting the first characters of the name, and looking for those that end in “.c”, the wildcard * refers to any character, and depending on its location indicates that it does not matter what that is present there. For example:

Command ls with wildcard * ending

In this case, the wildcard is at the end, so it will look for the files or directories that match the first characters and from there, any character can be found. If you want to know more about the wildcards that you can use with the Linux commands, especially with the ls command you can consult the following link. happy coding!!

--

--