Using the ls Command
ls is one of the most commonly used Unix commands.. (FreeBSD)
Posted 20.02.2006 | Updated 23.05.2006 | Contributed by Andy Mallett


The ls command is used to list files. ``ls'' on its own lists all files in the current directory except for hidden files. There are a number of options which can be combined, such as ls -la which gives a long listing of all files.
  • ls -a will list all files including hidden files (files with names beginning with a dot).
  • ls -F gives a full listing, indicating what type files are by putting a slash after directories and a star after executable files (programs you can run).
  • ls -l gives a long listing of all files
Here is an example section of the output of ls -l :
drwxr-xr-x   4 andym        users         1034 Jun  8 16:46 stuff
-rw-------   1 andym        users         1234 Jul 28 14:35 file
-rw-------   1 andym        users         2349 Apr 28 16:00 thing
-rw-r--r--   1 andym        users         2354 Oct 22 11:14 spud
-rw-r--r--   1 andym        users         4131 Sep 21 15:23 todo
-rw-r--r--   1 andym        users        44123 Sep  1 16:07 nottodo

Explanation of layout

The first column gives the type of the file (i.e. directory or file) and the file permissions

The second column is the number of links to the file i.e., (more or less) the number of names there are for the file. Generally an ordinary file will only have one link, but a directory will have more, because you can refer to it as ``dirname'', ``dirname/.'' where the dot means ``current directory'', and if it has a subdirectory named ``subdir'', ``dirname/subdir/..'' (the ``..'' means ``parent directory'')

The third and fourth columns are the user who owns the file and the Unix group of users to whom the file belongs. Most users belong to the default group users

The fifth column is the size of the file in bytes

The next three columns are the time at which the file was last changed (for a directory, this is the time at which a file in that directory was last created or deleted)

The last column is the name of the file

Further Examples

ls -R gives a recursive listing, including the contents of all subdirectories, their subdirectories, etc.

ls -t lists the files in order of the time when they were last modified (newest first), rather than in alphabetical order

ls -r lists the files in the reverse of the order that they would otherwise have been listed in. Thus, ls -lrt will give a long listing, oldest first, which is handy for seeing which files in a large directory have recently been changed