It is useful to see what files and directories in your current directory, or another directory. For this, we can use the ls command.

$ ls
includes  post    template
index.jade  run.coffee  web

The ls command is probably the most used command, and for good reason. With it, we can see directory contents and determine a variety of important file and directory attributes.

Listing a Different Directory ( ls <Directory> )

To find out what files are in another directory. The ls command allows you to specify a path to work on.

$ ls /usr/local/
CODEOFCONDUCT.md  README.md       lib
CONTRIBUTING.md   SUPPORTERS.md   man
Cellar            bin             opt
LICENSE.txt       etc             share
Library           include

List multiple directories

to find out what fines are in home (~) and /usr

$ ls ~ /usr
/home/robin:
Desktop  Documents  Music  Pictures  Public  Templates  Videos
 
/usr: 
bin  games    kerberos  libexec  sbin   src
etc  include  lib       local    share  tmp

List options

The ls command has a large number of possible options. The most common are listed in the following table:

OptionLong OptionDescription
-a--allList all files, even those with names that begin with a period, which are normally not listed (i.e., hidden).
-A--almost-allLike the -a option above except it does not list . (current directory) and .. (parent directory).
-d--directoryOrdinarily, if a directory is specified, ls will list the contents of the directory, not the directory itself. Use this option in conjunction with the -l option to see details about the directory rather than its contents.
-F--classifyThis option will append an indicator character to the end of each listed name. For example, a “/” if the name is a directory.
-h--human-readableIn long format listings, display file sizes in human readable format rather than in bytes.
-lDisplay results in long format.
-r--reverseDisplay the results in reverse order. Normally, ls displays its results in ascending alphabetical order.
-SSort results by file size.
-tSort by modification time.

Listing All Files ( -a )

It is common on Unix and Linux based machines to have "invisible" or "hidden" files that are prefixed with "."(dot). To see both "visible" and "hidden" files and directories, you can use -a flag.

$ ls -a /usr/local/
.                 Cellar          include
..                LICENSE.txt     lib
.git              Library         man
.gitignore        README.md       opt
.yardopts         SUPPORTERS.md   share
CODEOFCONDUCT.md  bin
CONTRIBUTING.md   etc

Long Form Listing ( -l )

One of the most useful flags for ls is the -l flag. It will list out the names of the files and directories as well as give more detailed information about them.

$ ls -l
-rw-r--r--   1 robin  admin   3160 14 Apr 00:27 CODEOFCONDUCT.md
-rw-r--r--   1 robin  admin   1103 14 Apr 00:27 CONTRIBUTING.md
drwxr-xr-x   7 robin  admin    238 14 Apr 00:28 Cellar
-rw-r--r--   1 robin  admin   1241 14 Apr 00:27 LICENSE.txt
drwxr-xr-x  10 robin  admin    340 14 Apr 00:28 Library -> /Users/robin/Dropbox/Library
-rw-r--r--   1 robin  admin   2064 14 Apr 00:27 README.md
-rw-r--r--   1 robin  admin  23781 14 Apr 00:27 SUPPORTERS.md
drwxrwxr-x  23 root   admin    782 14 Apr 00:28 bin
drwxr-xr-x   3 robin  admin    102 14 Apr 00:28 etc
drwxrwxr-x  30 root   admin   1020 17 Mar 23:06 include
drwxrwxr-x  22 root   admin    748 17 Mar 23:06 lib
drwxr-xr-x   5 root   wheel    170 30 Mar  2013 man
drwxr-xr-x   7 robin  admin    238 14 Apr 00:28 opt
drwxrwxr-x  10 root   admin    340 14 Apr 00:28 share

If the -l is given, the following information is displayed for each files: file mode, number of links, owner name, group name, number of bytes in the file, abbreviated month, day-of-month file last modified, hour file last modified, minutes file last modified, and the pathname. In addition, some paths are followed by -> and another path. These paths are symlinked to the directories that follow the ->.

Human Readable Sizes ( -h )

ls -l gives us a more detailed view of files and directories in our current directory, including the number of bytes in the file.

While knowing the number of bytes can be useful, but it more useful to see the size of the file in human readable terms, such as 1.7K or 35M. To show this information, simply use the -h flag.

$ ls -lh
-rw-r--r--   1 bindi  admin   3.1K 14 Apr 00:27 CODEOFCONDUCT.md
-rw-r--r--   1 bindi  admin   1.1K 14 Apr 00:27 CONTRIBUTING.md
drwxr-xr-x   7 bindi  admin   238B 14 Apr 00:28 Cellar
-rw-r--r--   1 bindi  admin   1.2K 14 Apr 00:27 LICENSE.txt
drwxr-xr-x  10 bindi  admin   340B 14 Apr 00:28 Library
-rw-r--r--   1 bindi  admin   2.0K 14 Apr 00:27 README.md
-rw-r--r--   1 bindi  admin    23K 14 Apr 00:27 SUPPORTERS.md
drwxrwxr-x  23 root   admin   782B 14 Apr 00:28 bin
drwxr-xr-x   3 bindi  admin   102B 14 Apr 00:28 etc
drwxrwxr-x  30 root   admin   1.0K 17 Mar 23:06 include
drwxrwxr-x  22 root   admin   748B 17 Mar 23:06 lib
drwxr-xr-x   5 root   wheel   170B 30 Mar  2013 man
drwxr-xr-x   7 bindi  admin   238B 14 Apr 00:28 opt
drwxrwxr-x  10 root   admin   340B 14 Apr 00:28 share

Sorting by Size ( -S )

Another useful flag with ls is -S , which will sort the results by file size, instead of the default sorting by name

ls -lhS
-rw-r--r--   1 bindi  admin    23K 14 Apr 00:27 SUPPORTERS.md
-rw-r--r--   1 bindi  admin   3.1K 14 Apr 00:27 CODEOFCONDUCT.md
-rw-r--r--   1 bindi  admin   2.0K 14 Apr 00:27 README.md
-rw-r--r--   1 bindi  admin   1.2K 14 Apr 00:27 LICENSE.txt
-rw-r--r--   1 bindi  admin   1.1K 14 Apr 00:27 CONTRIBUTING.md
drwxrwxr-x  30 root   admin   1.0K 17 Mar 23:06 include
drwxrwxr-x  23 root   admin   782B 14 Apr 00:28 bin
drwxrwxr-x  22 root   admin   748B 17 Mar 23:06 lib
drwxr-xr-x  10 bindi  admin   340B 14 Apr 00:28 Library
drwxrwxr-x  10 root   admin   340B 14 Apr 00:28 share
drwxr-xr-x   7 bindi  admin   238B 14 Apr 00:28 Cellar
drwxr-xr-x   7 bindi  admin   238B 14 Apr 00:28 opt
drwxr-xr-x   5 root   wheel   170B 30 Mar  2013 man
drwxr-xr-x   3 bindi  admin   102B 14 Apr 00:28 etc

Sorting by Last Modified Time ( -t )

It can often be useful to sort your ls results by the last time the files were modified.

ls -lt
drwxr-xr-x   7 bindi  admin    238 14 Apr 00:28 Cellar
drwxrwxr-x  23 root   admin    782 14 Apr 00:28 bin
drwxr-xr-x   7 bindi  admin    238 14 Apr 00:28 opt
drwxrwxr-x  10 root   admin    340 14 Apr 00:28 share
drwxr-xr-x   3 bindi  admin    102 14 Apr 00:28 etc
drwxr-xr-x  10 bindi  admin    340 14 Apr 00:28 Library
-rw-r--r--   1 bindi  admin   3160 14 Apr 00:27 CODEOFCONDUCT.md
-rw-r--r--   1 bindi  admin   1103 14 Apr 00:27 CONTRIBUTING.md
-rw-r--r--   1 bindi  admin   1241 14 Apr 00:27 LICENSE.txt
-rw-r--r--   1 bindi  admin   2064 14 Apr 00:27 README.md
-rw-r--r--   1 bindi  admin  23781 14 Apr 00:27 SUPPORTERS.md
drwxrwxr-x  30 root   admin   1020 17 Mar 23:06 include
drwxrwxr-x  22 root   admin    748 17 Mar 23:06 lib
drwxr-xr-x   5 root   wheel    170 30 Mar  2013 man

Reverse Sort ( -r )

When you are listing a directory that contains many files, sorting with ls is a great way to help quickly find the files or directories you are looking for. By default, ls sorts all of its results alphabetically. Last two have shown how to sort results by size and by last modified time.

Using -r flag we are able to reverse the result of ls .

ls -lr
drwxrwxr-x  10 root   admin    340 14 Apr 00:28 share
drwxr-xr-x   7 bindi  admin    238 14 Apr 00:28 opt
drwxr-xr-x   5 root   wheel    170 30 Mar  2013 man
drwxrwxr-x  22 root   admin    748 17 Mar 23:06 lib
drwxrwxr-x  30 root   admin   1020 17 Mar 23:06 include
drwxr-xr-x   3 bindi  admin    102 14 Apr 00:28 etc
drwxrwxr-x  23 root   admin    782 14 Apr 00:28 bin
-rw-r--r--   1 bindi  admin  23781 14 Apr 00:27 SUPPORTERS.md
-rw-r--r--   1 bindi  admin   2064 14 Apr 00:27 README.md
drwxr-xr-x  10 bindi  admin    340 14 Apr 00:28 Library
-rw-r--r--   1 bindi  admin   1241 14 Apr 00:27 LICENSE.txt
drwxr-xr-x   7 bindi  admin    238 14 Apr 00:28 Cellar
-rw-r--r--   1 bindi  admin   1103 14 Apr 00:27 CONTRIBUTING.md
-rw-r--r--   1 bindi  admin   3160 14 Apr 00:27 CODEOFCONDUCT.md

We can also use -r flag with other options to reverse their sort order. For example, ls -lSr will list the files by size with smaller files listed first.