The ls command in action

Paul Manot
3 min readSep 14, 2020
ls output
A long list of files and directories

So your new to the Terminal and you don't know about man ls yet or you are scared of the output on the screen when you run it. I hear you, it can be daunting at first… But you still want to know what ls means? Right? Fear not, you’ve come to the right place.

So you are in your terminal. You've ran pwd which prints the directory in which you are currently sitting otherwise know as the current working directory and you want to know what files and other directories you can access in this directory. Well that’s when you want to enter ls at the prompt (that little blinking white rectangle), which by the way stands for list. Are you starting to see a pattern with commands? Yes that’s right they are more often then not an abbreviation of what they do. Which is a good way to memorize them.

Back to ls…

So if you run ls without any options or any arguments you get a list of all the files and directory in the current working directory like so:

user:~/my_dir$ ls
dir1 dir2 file1 file2 file3 file4.c file5.c

As you can see we have 2 directories and 5 files inside of my_dir . Did you notice the 2 files with the .c extension? Keep them in mind, we’ll do something really cool in a minute. And if you want to get the list in a more detailed view you can add the -l option to get the long format.

user:~/my_dir$ ls -l
drwxr-xr-x 2 paul paul 4096 Sep 14 14:45 dir1
drwxr-xr-x 2 paul paul 4096 Sep 14 14:45 dir2
-rw-r--r-- 1 paul paul 0 Sep 14 14:44 file1
-rw-r--r-- 1 paul paul 0 Sep 14 14:44 file2
-rw-r--r-- 1 paul paul 0 Sep 14 14:44 file3
-rw-r--r-- 1 paul paul 0 Sep 14 14:44 file4
-rw-r--r-- 1 paul paul 0 Sep 14 14:44 file5

Or you could add the -a option to get all the files and directories including the hidden ones.

user:~/my_dir$ ls -a
. .. dir1 dir2 file1 file2 file3 file4 file5 .hidden_dir .hidden_file

And you could of course combine both options like so ls -la . Why don’t you try in your own terminal?

Finally the last feature I wanted to discus here is when you combine ls with a special character called the Wildcard: *. The Wildcard is super powerful because it tells your ls command to match only the files with a certain pattern. Let’s look at an example:

user:~/my_dir$ ls *.c
file4.c file5.c

Do you remember these files with a .c extension? Well, here they are by themselves. And that’s because they were the only files matching the *.c pattern. We are basically telling the terminal to list all files or directories finishing in .c no matter what characters were used before the dot in the file or directory name. In other words we are saying match every file or directory ending in .c hence why we only get 2 files since there were only 2 C files in the current directory. All the other files are still there. Just do a normal ls if you don’t believe me. But when you do ls *.c you are filtering the results of the ls command according to a specific pattern.

I hope these examples helped you understand the ls command better and have shown you the power of the Wildcard. To learn more about ls just fire up that man page and discover a world of infinite possibilities!

--

--