Monday, October 09, 2006

Using ls Command By Examples

The ls command is a Linux command that is used to list directory contents. In fact, it is a standard shell command that exists in all Unix/Linux variants.

Using ls command by examples

  • To list all files in ascending order of file name detailed with file modification time

    ls -la

  • To list all files in descending order of file creation time in full or customized date-time format

    ls -lact --full-time
    ls -lact --time-style="+%d %m %Y"

  • To list all files in descending order of file creation time

    ls -lact

  • To list all files in descending order of file size in kilobytes

    ls -laSh

  • To list only directories

    ls -ap | grep /
    ls -al | grep ^d

    Alternatively, use the find command as

    find . -type d -print

  • To list only directories initial with rpt filename

    ls -al rpt* | grep :$
    find . -type d -name "rpt*" -print

Definition of ls file listing command option switch
  • option switch l used to list files in long listing detailed format.

  • option switch a used to list all files including hidden files which filename prefix with a dot.

  • option switch lct used together to list file in descending order of file creation time.

    option switch lc list file creation time and sorted by filename.

    option switch lt list file in descending order of file modification time.

  • option switch h used to list files size information in kilobytes (KB).

  • option switch S used to list files in descending order of file size in byte.

  • option switch r used to reverse the default of descending listing order to ascending listing order.

  • option switch --full-time or --time-style used to display time-related info in full or customized format.

Notes! If the ls command colour scheme is not easy to read, turn it off by either:
  • On ad-hoc basis by adding option switch. For example

    ls /etc --color=none

    to list /etc directory contents without color scheme turning on. OR

  • On selective login session only. Use the alias command to save typing efforts. For example, execute the command

    alias ls="ls --color=none"

    will cause subsequent ls command executing as if it is supplied with --color=none option switch and automatically turn off the color scheme.

  • On each login session of individual login acount only. Copy the file /etc/DIR_COLORS to the user home directory as $HOME/.dir_colors and edit the variable COLOR to become COLOR=none to do the job.

    From the next user login onwards, type the command set | grep COLOR will notice that the COLORS variable changed as specified in the $HOME/.dir_colors setting file. Typing the ls command as usual which will disable the file listing color scheme automatically.

    To turn on the color scheme again, simply rewrite the line COLOR=none to COLOR=tty or COLOR=auto will do.

    The file /etc/DIR_COLORS is used to control file listing color scheme globally, meaning that all users account will be affected.
Related information: