ls

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.

cd projects

Unfortunately, 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.

ls
Output
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_1, project_2, project_3, and project_4. Let’s navigate to our project_1 folder, and list the files and folders in our project_1 folder.

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 MA_165 sub-directory.

ls and 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.

options

Like most unix utilities, there are many options that are used with ls. The following is an example of using ls with the -a option.

ls -a

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.

  • -R: recursively lists files in the current directory and (recursively) in all subdirectories, including child directories, grandchild directories, etc.

  • -d: limits the output to the directories themselves, and does not show the contents inside the directories

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

Dr Ward usually uses ls -la, which lists all files (including hidden files) and outputs the files in the directory in a list format.

For instance, in the directory /anvil/projects/tdm/data/fars if we type: ls -la we get the following results:

[email protected]:[fars] $ ls -la
total 32849
drwxrwsr-x+ 49 x-kamstut x-tdm-admin    16384 Jul 30 05:28 .
drwxrwsr-x+ 55 x-kamstut x-tdm-admin     4096 Jul 29 17:29 ..
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:15 1975
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1976
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1977
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1978
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1979
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1980
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1981
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1982
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1983
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1984
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1985
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1986
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1987
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1988
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1989
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1990
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1991
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1992
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1993
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1994
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1995
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1996
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1997
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1998
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 1999
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2000
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2001
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2002
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2003
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2004
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2005
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2006
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2007
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2008
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2009
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2010
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2011
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2012
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2013
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2014
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2015
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2016
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2017
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2018
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2019
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2020
drwxrwsr-x+  2 x-mdw     x-tdm-admin     4096 Jul 30 05:14 2021
-rw-r--r--   1 x-mdw     x-tdm-admin 33397890 Jul 30 05:28 7581.csv
-rwxrwxr-x   1 x-kamstut x-tdm-admin      720 Jul 23  2020 states.csv

We see that there is one directory for each year from 1975 to 2021, and a file called 7581.csv and a file called states.csv.

Each line describes the permissions of a file or directory, the number of hard links to the file or directory, the owner of the file or directory, the UNIX group associated with that file or directory, the number of bytes, the date of the last modification, and the names of the file or directory.

To see the recursive file listing in practice, you might (for instance) look at the AirBnB directory recursively:

ls -R /anvil/projects/tdm/data/airbnb

As another example, compare the output from:

ls /anvil/projects/tdm/data/airbnb/*

which shows all of the contents in the (first level of) the airbnb directory, and the contents of each such directory

as opposed to:

ls -d /anvil/projects/tdm/data/airbnb/*

which only shows the contents in the airbnb directory but does NOT show the contents in any such directories. The behavior of -d is described in the man pages for ls as: "Directories are listed as plain files (not searched recursively)"