ls (think list) is one of the most-used commands in Unix-like operating systems.
ls prints the files and sub-directories located in the current working directory, or, in the directory specified as an argument by the user.
ls is incredibly useful for navigation. When working with a graphical user interface (GUI), like Finder in MacOS or File Explorer in Windows, the files and folders in a directory are shown to you each time you navigate to and from a specific directory.
When navigating files and folders via a shell, this is not quite as straightforward. When using
cd to navigate to a particular directory, you will not have a nice list of files and folders presented to you upon navigating to said directory. Instead, we use
ls to list the files and folders, and see what options we have for navigation. For example, let’s say our current working directory is
/home/john/, and we want to go to our projects folder (
/home/john/projects) to see what projects we have. To navigate to our projects folder, we can use
cd will simply change your current working directory, and that is all that will happen. How do you know what projects reside in our projects folder? Well, we can use
ls to list the files and folders in our projects folder.
project_1 project_2 project_3 project_4 readme.txt
Now, we know we can use
cd to navigate to any of the following project folders:
project_4. Let’s navigate to our
project_1 folder, and list the files and folders in our
cd project_1 ls
This would result in an output that looks something like this.
schedule.pdf code.py STAT_190 MA_165
Here you can see that there are two files, and two sub-directories located in the
project_1 directory. From here, you can
cd into the
STAT_190 or the
cd are often used hand-in-hand. This makes sense: it is unlikely that you will remember the files located in each directory and sub-directory when navigating in the terminal.
Like most unix utilities, there are many options that are used with
ls. The following is an example of using
ls with the
Here are a few, and what they do!
-a: list all files, including hidden files.
-l: outputs the files in the directory in a list format.
-S: sorts the output by the size of each file (please note that this is a capital S).
-h: stands for "human-readable" and outputs the size of each file in a human-readable format.
Multiple options can be given 1-by-1, like in the following example.
ls -a -l # order of options does not matter ls -l -a
Or, rather than providing a new dash for each option, you can "smash" every option together.
ls -la # order still doesn't matter ls -al