ls command tutorial in linux/unix with examples and use cases

linux ls command – list directory contents

Linux ls command is the most commonly used command in our daily use of linux. ls can not only display the name of each file and directory and its associated information, but also display a sorted list of files and directories according to the default or specified rules.

Syntax

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

Options

  • -a, –all
    do not ignore entries starting with .
  • -A, –almost-all
    do not list implied . and ..
  • –author
    with -l, print the author of each file
  • –block-size=SIZE
    with -l, scale sizes by SIZE when printing them; e.g., ‘–block-size=M’; see SIZE format below
  • -B, –ignore-backups
    do not list implied entries ending with ~
  • -c
       with -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and sort  by  name;otherwise: sort by ctime, newest first
  • -u
       with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time, newest first
  • -d, –directory
      list directories themselves, not their contents
  • -l
      use a long listing format
  • -m  
     fill width with a comma separated list of entries
  • -r, –reverse
    reverse order while sorting
  • -R, –recursive
    list subdirectories recursively
  • -s, –size
    print the allocated size of each file, in blocks
  • -S  
    sort by file size, largest first
  • –sort=WORD
    sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X)
  • -t
      sort by modification time, newest first
  • -1
      list one file per line. 

Examples & Use Cases

List files and directories in the current directory

In the following example we will list all the contents of the directory, including hidden files and directories.

 ➜   ls -al

List files and directories sorted by time

In the following example we will list all the contents of the directory in reverse order of modification time.

 ➜  ls -lrt

List only directories

In the following example, we will list only the directories in the current directory.

 ➜  ls -dl */

Return only directory names and file names

In the following example, we will return only the directory name and file name in the current directory.

 ➜  ls -1

Add a Comment

Your email address will not be published. Required fields are marked *