1. Home
  2. Docs
  3. Beginning UNIX on Raven
  4. Files and navigation
  5. Listing files

Listing files

We can view a list of files in a directory outputted in a variety of styles and with different formatting and sorting.  To start we will move to the /home/pi/files directory which has a number of example files in it for our explorations.

cd ~/files

To view the files in a directory, just issues the ls (list) command:

ls

You will see an alphabetical list of the files in the directory which will wrap across your screen in a tabular format depending on the width and number of results found.

blank1  blank2  blank3  blank4  blank5  file1  file2  file3  file4  file5  text1  text2  text3  text4  text5

An alphabetical sort is useful, but sometimes we might want to check the most recently created files.  We can do this using the –t flag:

ls –t

With our example data, this will return the files in reverse order, as they were originally created in alphabetical order.

text5  text4  text3  text2  text1  file5  file4  file3  file2  file1  blank5  blank4  blank3  blank2  blank1

If we want to list all of the files in a particular directory we can append the filename to the ls command and get a list of files elsewhere.  E.g. to get a list of files in our home directory we can enter:

ls ~

Sometimes looking in just a single directory doesn’t provide all of the information we want.  If we want to find all files and directories that have been created as child directories of our home directory we can include the -R (recursive) flag:

ls –R ~

The ls command has a few other useful tricks to help in quickly drawing your attention to the information you are after.  Using the -F flag we can append a / character to each directory and a * to any executable file (script or program).

ls –F ~

Another flag which is occasionally useful is the –a flag.  The file names of hidden files in UNIX are prefixed with a full stop.  The –a (all) flag lists all files including hidden ones.  If we have a look at all files in our home directory:

ls -a ~

You will see a few extra files including:

.      ..     .profile

The basic ls list is useful for many purposes (e.g. checking a file name, or that it has been created), but sometimes we need a bit more information about a file beyond just its name.  The –l (long) flag returns a lot of extra information about a file including the files size, the date and time and modification and information about ownership and permissions to access the file.

ls –l

At the moment, just ignore the information about ownership and permissions.  Whilst they are a fundamental concept in UNIX (and the cause of many headaches to system administrators) they are a bit complex to worry about at this point in your learning.

One final and very useful flag to the ls command is –h.  If we combine this flag with the one for a long listing, the file size that is returned is listed in a more human readable format (bytes, kilobytes, megabytes, gigabytes).

ls -lh

Finally, we should point out that all of the above flags can be comined together in one string to return a listing according to your desires, for example:

ls –ltahFR ~

Summary of ls options

-l Long listing showing permissions, ownership, file size, modification date and time
-t Sort in reverse chronological order
-a Show all files (including hidden ones)
-h Easier readable file sizes 1K instead of 1024 bytes
-R Recursively list all files and directories
-F Append  to directories and * to executables