Introduction to ‘ls’ command

holberton

About the ls command, using wildcards to filter files like .jpg or .c, and its default behavior of sorting results alphabetically.

Hello everyone.

I started recently a training to change my career and become a dev. For this training, we should learn to code for sure, but also communicate with other people and particularly writing technical documentation. In this idea, it’s with this in mind that I’m writing a popularization article. This article should be designed to be understandable even by a beginner. That’s why, even though we’re going to be tackling some special cases, or some fairly advanced technical concepts, I’m not going to go into detail on certain aspects, and I’m going to take a few shortcuts, because the aim is really to get to the heart of the matter and understand what’s really important.

The ‘ls’ command

First of all, it’s very common in computing to use commands (i.e. textual instructions) rather than graphical interfaces (pretty icons on nice backgrounds). There are obviously very good reasons for this, but it’s not the aim of this article and we’ll certainly discuss it another time.

Among all these textual instructions, let’s study the ls command

The ls command is short for “list”. The purpose of this command is to display the list of files in a directory. (For beginners, to a better understanding, i will display the file with a graphical interface, and with the terminal (aka ugly white text on black background) )

So… lets try to type ls in the terminal ! Lets show an example : screen1

As i said, ls can list and display the file of the directory. If i want to be more precise, we can list information about the files (not just the name) but that’s another subject…

But we can do better!

Now, This is where the power of the command line comes into play : we can pimp a little bit this command to show only the file we are looking for.

For example if you need to only display the .jpg file (image file) you can type ls *.jpg

Lets show an example : screen2

Now let’s break it down

ls = list the file in the directory * = is a kind of joker, that mean “all” … .jpg = is the extension of the file

So if we combine everything : “Show me all the file who finish with “.jpg” ”

For your information, by default, the command list the file alphabetically.

Here's another example

Now, if you have understanding how do you only show programming file written in C ? The extension of this kind of file is .c The answer below…

You find it ? I’ts ls *.c

What happen when you type ls *.c ? As seen above, you ask to list all files ending in *.c and by default, the result is displayed in alphabetical order.

If you want to go further, you need to know that before the command lists the requested files, there are actually two other actions performed upstream (but not seen by the user), namely executable calling and executable execution.

screen3

That’s all for today. Thank you to have read this article ; don’t hesitate to comment if you have question of if you wish to provide details.

Photo de Glenn Carstens-Peters sur Unsplash

Article précédent Article suivant