You have learned a lot of different options that you can use with the ls command. Table 4 summarizes all the options we have used so far.
ls option | What it does |
-l | Long and detailed listing of files. |
-a | List the hidden files. |
-d | List directories themselves, not their contents. |
-t | Sort files by modification times. |
-u | When used with -l, it shows access times instead of modification times. When used with -lt, it will sort by, and show, access times. |
-r | Will reverse listing order. |
-R | List subdirectories recursively. |
Table 4: Popular ls Command Options
You will often be wanting to use two or more command options at a time. For example, ls -a -l is commonly used to do a long listing for all the files in a directory.
Also, ls -l -a -t -r is a very popular combination because sometimes you would want to see the listing of the files sorted by modification times (oldest first). For that reason, combining the command options is more efficient and so running the ls -latr command:
elliot@ubuntu-linux:~$ ls -latr
total 120
-rw-r--r-- 1 elliot elliot 0 Apr 11 2010 file2
-rw-r--r-- 1 elliot elliot 807 Dec 26 23:47 .profile
-rw-r--r-- 1 elliot elliot 3771 Dec 26 23:47 .bashrc
drwxr-xr-x 9 root root 4096 Jan 17 04:37 ..
-rw-r--r-- 1 elliot elliot 220 Jan 20 17:23 .bash_logout
drwxr-xr-x 6 elliot elliot 4096 Jan 25 22:13 Desktop
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:08 file1
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:27 file3
drwxr-xr-x 3 elliot elliot 4096 Jan 25 23:52 dir1
-rw------- 1 elliot elliot 3152 Jan 26 00:01 .bash_history
drwxr-xr-x 17 elliot elliot 4096 Jan 30 23:32 .
Will yield the same result as running the ls -l -a -t -r command:
elliot@ubuntu-linux:~$ ls -l -a -t -r
total 120
-rw-r--r-- 1 elliot elliot 0 Apr 11 2010 file2
-rw-r--r-- 1 elliot elliot 807 Dec 26 23:47 .profile
-rw-r--r-- 1 elliot elliot 3771 Dec 26 23:47 .bashrc
drwxr-xr-x 9 root root 4096 Jan 17 04:37 ..
-rw-r--r-- 1 elliot elliot 220 Jan 20 17:23 .bash_logout
drwxr-xr-x 6 elliot elliot 4096 Jan 25 22:13 Desktop
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:08 file1
-rw-r--r-- 1 elliot elliot 0 Jan 25 23:27 file3
drwxr-xr-x 3 elliot elliot 4096 Jan 25 23:52 dir1
-rw------- 1 elliot elliot 3152 Jan 26 00:01 .bash_history
drwxr-xr-x 17 elliot elliot 4096 Jan 30 23:32 .
Before this chapter comes to an end, I want to show you a pretty cool tip. First, let's create a directory named averylongdirectoryname:
elliot@ubuntu-linux:~$ mkdir averylongdirectoryname
elliot@ubuntu-linux:~$ ls -ld averylongdirectoryname
drwxr-xr-x 2 elliot elliot 4096 Mar 2 12:57 averylongdirectoryname
Tab Completion is one of the most useful features in the Linux command line. You can use this to feature to let the shell automatically complete (suggest) command names and file paths. To demonstrate, type (don't run) the following text on your terminal:
elliot@ubuntu-linux:~$ cd ave
Now press the Tab key on your keyboard, and the shell will automatically complete the directory name for you:
elliot@ubuntu-linux:~$ cd averylongdirectoryname/
Pretty cool! Alright, this takes us to the end of this chapter, and it's time for you to do the lovely knowledge check.