Skip to content

Lesson 3: Listing directories

To list the contents of a directory, we need to use the ls command. Below is a sample output when running ls:

[learner ~]$ ls
directory1 directory2 file1
[learner ~]$

If there are no files or directories, we can expect to see no output. The default sorting order is alphabetical then numerical if the directory is not empty.

Directory shortcuts

For reference, the below shortcuts can be used by ls and other commands to reference directories without using the whole (absolute) path:

Shortcut Literal   Interpretation
------------------ ------------------------------------------------------------------------------
`.`                The current directory
`..`               The parent directory - may be chained to navigate up the directory hierarchy
`~`                The current user's home directory

The ls command can be used to list the contents of any directory, not necessarily the one that you are currently in. Try the following:

[learner ~]$ ls /
bin   dev          etc   lib    lost+found  mnt  proc  run   srv  tmp  var
boot  environment  home  lib64  media       opt  root  sbin  sys  usr

[learner ~]$ ls /etc/bashrc
/etc/bashrc

[learner ~]$ ls ..
learner

[learner ~]$ ls ../..
bin   dev          etc   lib    lost+found  mnt  proc  run   srv  tmp  var
boot  environment  home  lib64  media       opt  root  sbin  sys  usr

Notice how the output of ls / and ls ../.. are identical? That is because the ls ../.. command used a relative path to reference two directories up from your home directory (the root directory).

Hidden Files and Directories

Like other operating systems, Linux also uses hidden files (beginning with a dot), which are not displayed in the output of ls by default. To also list hidden entries, add the -a flag to ls, for example:

[learner learning_linux]$ ls -a ~
.  ..  .bashrc  .hiddenfile  directory1  directory2  file1

You can see that '.bashrc' and '.hiddenfile' are hidden files.

Many applications use configuration files allowing the user to customise how the application works; Configuration files are often stored as a hidden file in the user's home directory