Options for ls

Almost every Linux command has so called ‘options’; ways to instruct them how to do their work. Options are often single letters that are precede by a minus sign. More options can be combined. One of the most common options used to be the ‘-h‘ option for ‘help‘, although it has been replaced by ‘–help‘ with the introduction of more meaningful and readable options. It gives us some information about the command and which options we can use with that specific command. You might want to try ‘ls –help‘ (those are two ‘-‘ signs), but don’t get overwhelmed with the amount of information you get for such a relatively simple command. For now, I will show you, and try to explain, the two options I use most when I use the ls command, the -a and -l options.

The -l option

Let’s have a look at the output of the command ‘ls -l‘, shall we? We will take one line of the output and go along each item or field to explain them. The line we will be looking at is:

gerard@geronimo:/home$ ls -l
total 8
drwxr-xr-x 14 gerard gerard 4096 Apr 22 09:23 gerard
drwxr-xr-x 2 helper helper 4096 Mar 16 12:06 helper
gerard@geronimo:/home$

This ‘ls -l‘ command gives us more information. It tells us something about the two ‘things‘ that we saw earlier, gerard and helper. We can now see for sure that they are directories (by the letter d at the first position of their respective lines).

drwxr-xr-x 14 gerard gerard 4096 Apr 22 09:23 gerard

We have 10 fields in this line:

  • d
    • values her stand for:
    • – :: file entry
    • d :: directory entry
    • l :: link (to another file or directory)
  • Now follow three groups of three characters ; they stand for authorizations on that entry for 1) the owner of the file (rwx), 2) the group of the file (r-x) and 3) the rest of the world (r-x); each entry consists of three characters that mean the following (each of those can also be ‘-‘ meaning that that specific authorization is not granted):
    • r :: read permission
    • w :: write permission
    • x :: execute permission
  • 14 :: the number of bytes in a file or entries in a directory
  • gerard :: the owner of the file
  • gerard :: the group of the file (each owner has their own group by default)
  • 4096 :: the size in bytes
  • Apr 22 09:23 :: the date the entry was last modified
  • gerard :: finally, the name of the entry

The -a option

The ‘-a‘ option also gives us more information, but now about hidden files. In Linux, some files that do not have to be visible during normal use start their names with a dot, for example ‘.profile’. Using the directory ‘/home/helper‘ Let’s have a look at the difference between the options, first the ‘-l‘ option:

gerard@geronimo:/home/helper$ ls -l
total 4
-rw-r--r-- 1 helper helper 27 Apr 22 12:52 myfirst.txt
gerard@geronimo:/home/helper$

As you can see, there’s only one file in here, called myfirst.txt. Or is there? Now lets try adding the ‘-a‘ option:

gerard@geronimo:/home/helper$ ls -al
total 28
drwxr-xr-x 2 helper helper 4096 Apr 22 12:52 .
drwxr-xr-x 4 root root 4096 Mar 16 12:04 ..
-rw------- 1 helper helper 106 Apr 22 12:52 .bash_history
-rw-r--r-- 1 helper helper 220 Mar 16 12:04 .bash_logout
-rw-r--r-- 1 helper helper 3526 Mar 16 12:04 .bashrc
-rw-r--r-- 1 helper helper 27 Apr 22 12:52 myfirst.txt
-rw-r--r-- 1 helper helper 675 Mar 16 12:04 .profile
gerard@geronimo:/home/helper$

Wow, there’s a lot more going on here! This is what the ‘-a‘ option shows us, so called ‘hidden’ files.

First we have two strange ones, ‘.’ and ‘..’ – these are placeholders for the current directory (‘.’) and the directory above this directory (‘..’). So when you would give the command: ‘cd .‘ nothing would change because we already are, by definition, in the current directory. When we would say ‘cd ..‘ on the other hand, the current directory would change to /home, because it takes us to the directory above the current one.

The rest are ‘dot files’, hidden from the user in normal circumstances. They help govern the system. The file ‘.profile’ for example is read and executed by our command interpreter (also called a ‘shell’), in our case it is the ‘bash’ program (stands for basic shell).