The ls command - Unleashed!


Nitin Venkatesh's Gravatar

Nitin Venkatesh
published Dec. 30, 2011, 10:04 p.m.


The ls command is used to list the files present in a particular directory/directories. By default it lists the files present in the current directory by their names providing no extra information. The output might sometimes be color-coded (depending on the way your .bashrc and .bash_profile are configured). The point of this blogpost is that the ls command is much more useful than just colouring up your terminal. It’s awesome and we are going to use it to glean information about the files/directories present in our system.

Here is the output of the ls command without any parameters.

nits@nits-excalibur:~$ ls
Aptana_Studio_3            Documents                  Music      Testing
Aptana Studio 3 Workspace  Downloads                  Pictures   Testing2
Audiobooks                 Dropbox                    Podcasts   test.sh
bashlink                   examples.desktop           projects   Ubuntu One
cloudsn-0.8.12             Firefox_wallpaper.png      Public     Videos
Desktop                    gcc-defaults-1.107ubuntu5  Templates

A better view of the files with more information about them can be gotten by the l parameter (Just for clarity, that’s a lower-case L, just like the l in linux)

nits@nits-excalibur:~$ ls -l
total 1320
drwxrwxr-x 8 nits nits    4096 2011-11-28 13:19 Aptana_Studio_3
drwxrwxr-x 4 nits nits    4096 2011-11-27 20:50 Aptana Studio 3 Workspace
drwxrwxr-x 2 nits nits    4096 2011-10-15 23:33 Audiobooks
lrwxrwxrwx 1 nits nits       9 2011-12-26 00:22 bashlink -> /bin/bash
drwxr-xr-x 9 root root    4096 2011-10-20 03:10 cloudsn-0.8.12
drwxr-xr-x 4 nits nits    4096 2011-12-26 00:24 Desktop
drwxr-xr-x 4 nits nits    4096 2011-11-27 20:48 Documents
drwxr-xr-x 7 nits nits    4096 2011-12-25 22:13 Downloads
drwx------ 9 nits nits    4096 2011-12-25 18:48 Dropbox
-rw-r--r-- 1 nits nits     179 2011-10-15 09:26 examples.desktop
-rw-rw-r-- 1 nits nits 1265446 2011-10-17 21:52 Firefox_wallpaper.png
drwxr-xr-x 3 root root    4096 2011-08-14 12:33 gcc-defaults-1.107ubuntu5
-rwxrw-r-- 1 nits nits       9 2011-12-25 21:56 test.sh
drwxrwxr-x 6 nits nits    4096 2011-12-23 09:34 Ubuntu One
drwxr-xr-x 5 nits nits    4096 2011-12-06 10:51 Videos

Here, the total size of all the files/directories is given on the first line followed by a listing of all the files and directories. The first column shows permissions. The second column shows the number of incoming/outgoing links to that particular file/directory. The third and fourth column display the names of the user and the group the file/directory belongs to. The fifth column displays the size of the file in bytes. The sixth column displays the last modified time of the file/directory and finally the seventh column displays the name of the file/directory.

Want to know more about permissions and deciphering them? Take a look at this blogpost here.

That was a bit too much talking right? Here’s an explanatory image if you are not into yapping or reading yappy-content.

The command ls -l explained

Now let’s try a bunch of additional parameters. Here’s what you get if you run a ls -halF (This is just a partial output)

nits@nits-excalibur:~$ ls -halF
total 1.9M
drwxr-xr-x 61 nits nits 4.0K 2011-12-26 00:25 ./
drwxr-xr-x  6 root root 4.0K 2011-12-25 21:51 ../
drwx------  3 nits nits 4.0K 2011-10-15 22:51 .adobe/
drwxrwxr-x  8 nits nits 4.0K 2011-11-28 13:19 Aptana_Studio_3/
drwxrwxr-x  4 nits nits 4.0K 2011-11-27 20:50 Aptana Studio 3 Workspace/
drwx------  2 nits nits 4.0K 2011-12-11 01:39 .aptitude/
drwxrwxr-x  2 nits nits 4.0K 2011-10-15 23:33 Audiobooks/
-rw-------  1 nits nits  41K 2011-12-26 01:37 .bash_history
lrwxrwxrwx  1 nits nits    9 2011-12-26 00:22 bashlink -> /bin/bash*
-rw-r--r--  1 nits nits  220 2011-10-15 09:26 .bash_logout
-rw-r--r--  1 nits nits 3.3K 2011-10-15 09:26 .bashrc
drwx------ 28 nits nits 4.0K 2011-12-23 19:22 .cache/
-rw-r--r--  1 nits nits 2.8K 2011-12-20 04:12 .clicompanion2
drwxr-xr-x  9 root root 4.0K 2011-10-20 03:10 cloudsn-0.8.12/
drwxrwxr-x  3 nits nits 4.0K 2011-10-15 21:12 .compiz-1/
drwx------ 38 nits nits 4.0K 2011-12-20 20:13 .config/
-rw-rw-r--  1 nits nits 2.1K 2011-10-17 11:26 .conkyrc
  • h parameter displays the size of the files/directories in a human-readable format. K = kilobyte, M = megabyte, G = gigabyte
  • a parameter includes the hidden files and directories.
  • F parameter comes in handy when there are no colours in your terminal. It helps differentiate between folders and files. Folders have a / (forward slash) after them.

Did you see some backups of files, that is, files starting with ~ ? And were you thinking why do I have a . and .. at the beginning of every listing? Let’s kick them out with ls -lAB

nits@nits-excalibur:~$ ls -lAB
total 1916
drwx------  3 nits nits    4096 2011-10-15 22:51 .adobe
drwxrwxr-x  8 nits nits    4096 2011-11-28 13:19 Aptana_Studio_3
drwxrwxr-x  4 nits nits    4096 2011-11-27 20:50 Aptana Studio 3 Workspace
drwx------  2 nits nits    4096 2011-12-11 01:39 .aptitude
drwxrwxr-x  2 nits nits    4096 2011-10-15 23:33 Audiobooks
-rw-------  1 nits nits   41038 2011-12-26 01:37 .bash_history
lrwxrwxrwx  1 nits nits       9 2011-12-26 00:22 bashlink -> /bin/bash
-rw-r--r--  1 nits nits     220 2011-10-15 09:26 .bash_logout
-rw-r--r--  1 nits nits    3353 2011-10-15 09:26 .bashrc
drwx------ 28 nits nits    4096 2011-12-23 19:22 .cache
-rw-r--r--  1 nits nits    2776 2011-12-20 04:12 .clicompanion2
drwxr-xr-x  9 root root    4096 2011-10-20 03:10 cloudsn-0.8.12
drwxrwxr-x  3 nits nits    4096 2011-10-15 21:12 .compiz-1
drwx------ 38 nits nits    4096 2011-12-20 20:13 .config
-rw-rw-r--  1 nits nits    2116 2011-10-17 11:26 .conkyrc
  • A parameter removes the implied . and .. (the first two listings)
  • B parameter removes the backup-files (files starting with the ~ symbol)

We can also sort the listing, let’s try ls -lSr

nits@nits-excalibur:~$ ls -lSr
total 1320
-rwxrw-r-- 1 nits nits       9 2011-12-25 21:56 test.sh
lrwxrwxrwx 1 nits nits       9 2011-12-26 00:22 bashlink -> /bin/bash
-rw-r--r-- 1 nits nits     179 2011-10-15 09:26 examples.desktop
drwxr-xr-x 5 nits nits    4096 2011-12-06 10:51 Videos
drwxrwxr-x 6 nits nits    4096 2011-12-23 09:34 Ubuntu One
drwxrwxr-x 2 nits nits    4096 2011-12-23 09:37 Testing2
drwxrwxr-x 2 nits nits    4096 2011-12-25 22:29 Testing
drwxr-xr-x 2 nits nits    4096 2011-10-15 09:31 Templates
drwxr-xr-x 2 nits nits    4096 2011-10-15 09:31 Public
drwx------ 4 nits nits    4096 2011-11-18 20:35 projects
drwxrwxr-x 2 nits nits    4096 2011-10-15 23:33 Podcasts
drwxr-xr-x 4 nits nits    4096 2011-12-26 01:14 Pictures
drwxr-xr-x 2 nits nits    4096 2011-10-15 09:31 Music
drwxr-xr-x 3 root root    4096 2011-08-14 12:33 gcc-defaults-1.107ubuntu5
drwx------ 9 nits nits    4096 2011-12-25 18:48 Dropbox
drwxr-xr-x 7 nits nits    4096 2011-12-25 22:13 Downloads
drwxr-xr-x 4 nits nits    4096 2011-11-27 20:48 Documents
drwxr-xr-x 4 nits nits    4096 2011-12-26 01:55 Desktop
drwxr-xr-x 9 root root    4096 2011-10-20 03:10 cloudsn-0.8.12
drwxrwxr-x 2 nits nits    4096 2011-10-15 23:33 Audiobooks
drwxrwxr-x 4 nits nits    4096 2011-11-27 20:50 Aptana Studio 3 Workspace
drwxrwxr-x 8 nits nits    4096 2011-11-28 13:19 Aptana_Studio_3
-rw-rw-r-- 1 nits nits 1265446 2011-10-17 21:52 Firefox_wallpaper.png
  • S parameter sorts the listing by file size, from the biggest to the smallest.
  • r parameter reverses the order of the listing

Together with these two parameters, the files/directories get listed in ascending order of file size (smallest to biggest).

There is also a t parameter that lists the files/directories according to the time last modified. The files/directories modified latest are displayed first and the ones that weren’t modified recently are decked in the bottom.

nits@nits-excalibur:~$ ls -tl
total 1320
lrwxrwxrwx 1 nits nits       1 2011-12-26 02:43 a_softlink -> a
-rw-rw-r-- 2 nits nits       0 2011-12-26 02:42 a
-rw-rw-r-- 2 nits nits       0 2011-12-26 02:42 a_hardlink
drwxrwxr-x 3 nits nits    4096 2011-12-26 02:27 Testing
drwxr-xr-x 4 nits nits    4096 2011-12-26 01:55 Desktop
drwxr-xr-x 4 nits nits    4096 2011-12-26 01:14 Pictures
drwxr-xr-x 7 nits nits    4096 2011-12-25 22:13 Downloads
-rwxrw-r-- 1 nits nits       9 2011-12-25 21:56 test.sh
drwx------ 9 nits nits    4096 2011-12-25 18:48 Dropbox
drwxrwxr-x 2 nits nits    4096 2011-12-23 09:37 Testing2
drwxrwxr-x 6 nits nits    4096 2011-12-23 09:34 Ubuntu One
drwxr-xr-x 5 nits nits    4096 2011-12-06 10:51 Videos
drwxrwxr-x 8 nits nits    4096 2011-11-28 13:19 Aptana_Studio_3
drwxrwxr-x 4 nits nits    4096 2011-11-27 20:50 Aptana Studio 3 Workspace
drwxr-xr-x 4 nits nits    4096 2011-11-27 20:48 Documents
drwx------ 4 nits nits    4096 2011-11-18 20:35 projects
drwxr-xr-x 9 root root    4096 2011-10-20 03:10 cloudsn-0.8.12
-rw-rw-r-- 1 nits nits 1265446 2011-10-17 21:52 Firefox_wallpaper.png
drwxrwxr-x 2 nits nits    4096 2011-10-15 23:33 Podcasts
drwxrwxr-x 2 nits nits    4096 2011-10-15 23:33 Audiobooks
drwxr-xr-x 2 nits nits    4096 2011-10-15 09:31 Music
drwxr-xr-x 2 nits nits    4096 2011-10-15 09:31 Public
drwxr-xr-x 2 nits nits    4096 2011-10-15 09:31 Templates
-rw-r--r-- 1 nits nits     179 2011-10-15 09:26 examples.desktop
drwxr-xr-x 3 root root    4096 2011-08-14 12:33 gcc-defaults-1.107ubuntu5

Another cool thing you can do with ls is list all the files in the sub-directories in the current directory recursively. This is done with the R parameter.

nits@nits-excalibur:~/Testing$ ls -Rl
.:
total 16
-rw-rw-r-- 1 nits nits   47 2011-12-19 08:49 numericalfile
drwxrwxr-x 3 nits nits 4096 2011-12-26 02:28 test
-rwxrwxr-x 1 nits nits  281 2011-12-25 23:21 test.sh
-rw-rw-r-- 1 nits nits   84 2011-12-19 08:47 textfile

./test:
total 4
-rw-rw-r-- 1 nits nits    0 2011-12-26 02:28 file1
-rw-rw-r-- 1 nits nits    0 2011-12-26 02:28 file2
drwxrwxr-x 3 nits nits 4096 2011-12-26 02:28 test2

./test/test2:
total 4
-rw-rw-r-- 1 nits nits    0 2011-12-26 02:28 file2_1
-rw-rw-r-- 1 nits nits    0 2011-12-26 02:28 file2_2
drwxrwxr-x 2 nits nits 4096 2011-12-26 02:28 test3

./test/test2/test3:
total 0
-rw-rw-r-- 1 nits nits 0 2011-12-26 02:28 file3_1
-rw-rw-r-- 1 nits nits 0 2011-12-26 02:28 file3_2
  • R parameter recurses through all the directories present inside the current working directory and lists all the files.

The Testing directory which is my current working directory holds 3 files and a directory called test which in turn has 2 files and a sub-directory called test2 which in turn has 2 files and a sub-directory called test3 which holds 2 files. But with just one command (ls -Rl) we were easily able to view the files present in all the directories under the Testing directory. How awesome was that!

Let’s look at one last super powerful parameter before we end this. That’s the i parameter. Here’s the output of ls -li

nits@nits-excalibur:~$ ls -li
total 1320
1707361 -rw-rw-r-- 2 nits nits       0 2011-12-26 02:42 a
1707361 -rw-rw-r-- 2 nits nits       0 2011-12-26 02:42 a_hardlink
 266042 drwxrwxr-x 8 nits nits    4096 2011-11-28 13:19 Aptana_Studio_3
 266046 drwxrwxr-x 4 nits nits    4096 2011-11-27 20:50 Aptana Studio 3 Workspace
1707635 lrwxrwxrwx 1 nits nits       1 2011-12-26 02:43 a_softlink -> a
    372 drwxrwxr-x 2 nits nits    4096 2011-10-15 23:33 Audiobooks
 266520 drwxr-xr-x 9 root root    4096 2011-10-20 03:10 cloudsn-0.8.12
1703949 drwxr-xr-x 4 nits nits    4096 2011-12-26 01:55 Desktop
1703953 drwxr-xr-x 4 nits nits    4096 2011-11-27 20:48 Documents
1703950 drwxr-xr-x 7 nits nits    4096 2011-12-25 22:13 Downloads
 131474 drwx------ 9 nits nits    4096 2011-12-25 18:48 Dropbox
1703939 -rw-r--r-- 1 nits nits     179 2011-10-15 09:26 examples.desktop
1705457 -rw-rw-r-- 1 nits nits 1265446 2011-10-17 21:52 Firefox_wallpaper.png
 396361 drwxr-xr-x 3 root root    4096 2011-08-14 12:33 gcc-defaults-1.107ubuntu5
1703954 drwxr-xr-x 2 nits nits    4096 2011-10-15 09:31 Music
1703955 drwxr-xr-x 4 nits nits    4096 2011-12-26 01:14 Pictures
    373 drwxrwxr-x 2 nits nits    4096 2011-10-15 23:33 Podcasts
1707825 drwx------ 4 nits nits    4096 2011-11-18 20:35 projects
1703952 drwxr-xr-x 2 nits nits    4096 2011-10-15 09:31 Public
1703951 drwxr-xr-x 2 nits nits    4096 2011-10-15 09:31 Templates
 524705 drwxrwxr-x 3 nits nits    4096 2011-12-26 02:27 Testing
 396475 drwxrwxr-x 2 nits nits    4096 2011-12-23 09:37 Testing2
1708264 -rwxrw-r-- 1 nits nits       9 2011-12-25 21:56 test.sh
    387 drwxrwxr-x 6 nits nits    4096 2011-12-23 09:34 Ubuntu One
1703956 drwxr-xr-x 5 nits nits    4096 2011-12-06 10:51 Videos

Notice the extra column that got added before the permission listings? Now that’s called the inode number. The inode number is unique for every file or directory on the system except for hard links. The original file and the hard-link to it share the same inode number.

Well, see how interesting the ls command is and how much you can do with it. You can find cooler usage of ls command by viewing its man page with man ls or you could also view it online here.