Linux ls command - Listing directory content

Linux ls command is used to list directory content in Linux command line terminal. In other words, the Linux ls command can be used to view the content of a directory in Linux system. If you are familiar with windows command prompt, or dos, then the equivalent command to the Linux ls is the dir command.

Here is a part of the Linux ls command manual page:

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information  about  the FILEs (the current directory by default).
       Sort entries alphabetically if none of -cftuvSUX nor --sort.

Linux ls command examples

Linux ls command can be used without any option to view a directory content. When ls command invoked without option, it prints all files and directories except hidden files. Here is an example of ls command used to view / (root) directory:

luzar@hitam:~$ ls / 
bin/   etc/    fat-f/  home/        media/  proc/  srv/  usr/
boot/  fat-d/  fat-g/  lib/         mnt/    root/  sys/  var/
dev/   fat-e/  fat-h/  lost+found/  opt/    sbin/  tmp/
luzar@hitam:~$

To view all files and directories including hidden files, we can use ls command with -a option. See the example below:

luzar@hitam:~$ ls -a 
./                     .gimp-2.4/        .qt/                 drupal/
../                    .googleearth/     .recently-used.xbel  dwhelper/
.DCOPserver_hitam_:0@  .gqview/          .screenrc            etc/
.DCOPserver_hitam__0   .gstreamer-0.10/  .serverauth.3581     movies/
.ICEauthority          .java/            .ssh/                musics/
.VirtualBox/           .kde/             .themes/             permission.txt
.Xauthority            .kderc            .thumbnails/         screenshots/

The files and directories start with . such as .Xauthority, .kderc and .thumbnails is hidden. The only way to view hidden files is to use -a option, alone or combine with other ls command options.

If we want to see details information about directory contents, we can use -l option. See the example of ls -l option below:

luzar@hitam:~$ ls -l /etc 
total 2056
-rw-r--r--  1 root root     3458 2008-09-21 11:18 DIR_COLORS
-rw-r--r--  1 root root       18 2009-01-07 15:13 HOSTNAME
drwxr-xr-x 18 root root     4096 2009-01-27 15:15 X11/
-rw-r--r--  1 root root     2561 2002-02-25 04:37 a2ps-site.cfg
-rw-r--r--  1 root root    15067 2002-02-25 04:37 a2ps.cfg
drwxr-xr-x  3 root root     4096 2008-12-31 22:25 acpi/
-rw-r--r--  1 root root       47 2009-02-24 02:22 adjtime
-rw-r--r--  1 root root      265 2008-02-17 03:35 anthy-conf
drwxr-xr-x  4 root root     4096 2008-12-31 22:27 asciidoc/

Linux ls -l command list details of contents such as file or directory permission, ownership, size, creation date and time. For more information about permission, you can check Linux file and directory permissions. The Linux file and directory ownership discuss about Linux ownership. These tutorials also explains more about Linux files and directory.

If you want to view directories only, you can used -d option. The example below shows how to use ls -d option to list only directories:

Using Linux ls command to view directories only in current directory:

luzar@hitam:~$ ls -d */ 
Desktop//  dwhelper//  movies//  screenshots//  sony//       
anime//    drupal//   etc//       musics//  software//     tutorials//
luzar@hitam:~$

We can also view directories only in other directory. The example below shows how to view directories only in /var/log directory using Linux ls command:

luzar@hitam:~$ ls -d /var/log/*/ 
/var/log/cups//      /var/log/removed_packages//  /var/log/setup//
/var/log/httpd//     /var/log/removed_scripts//   /var/log/uucp//
/var/log/iptraf//    /var/log/sa//                /var/log/vmware//
/var/log/nfsd//      /var/log/samba//
/var/log/packages//  /var/log/scripts//
luzar@hitam:~$

Another useful option that we can use with Linux ls command is the ls -R option. The ls -R option listing directory content recursively. See the example below:

luzar@hitam:~$ ls -R /var/log/vmware/ 
/var/log/vmware/:
hostd-0.log  hostd-3.log  hostd-6.log  hostd-9.log      hostd.log@
hostd-1.log  hostd-4.log  hostd-7.log  hostd-index      webAccess/
hostd-2.log  hostd-5.log  hostd-8.log  hostd-trace.log

/var/log/vmware/webAccess:
client.log         proxy.log  unitTest.log      viewhelper.log
objectMonitor.log  timer.log  updateThread.log  work/

/var/log/vmware/webAccess/work:
Catalina/

/var/log/vmware/webAccess/work/Catalina:
localhost/

/var/log/vmware/webAccess/work/Catalina/localhost:
ui/

/var/log/vmware/webAccess/work/Catalina/localhost/ui:
luzar@hitam:~$

The example above shows ls -R listing the /var/log/vmware/ directory content and all sub-directories in it.

There are many more options that we can use with the Linux ls command. All the ls examples above are regular options used everyday by Linux users. Check the Linux ls manual page and try what other options can do.

Back to Linux basic commands main page.