The ls command displays directory content in the Linux terminal and, by default, sorts the output in alphabetical order. Sometimes you'll need to change the sort order to reverse, and you can do that both from ls itself or by piping the output to an external program.

ls has the sort function built-in, is very flexible, and works well with other ls options. On the other hand, sort is a dedicated external program to sorting output with its advantages.

Steps to list directory content in reverse in Linux:

  1. Launch terminal.
  2. List directory content using ls.
    $ ls
    Applications Documents    Dropbox      Movies       Pictures     System       tmp
    Desktop      Downloads    Library      Music        Public       Workspace
  3. Pipe ls program to sort to display result in reverse.
    $ ls | sort -r
    tmp
    Workspace
    System
    Public
    Pictures
    Music
    Movies
    Library
    Dropbox
    Downloads
    Documents
    Desktop
    Applications
  4. List directory content in reverse using ls.
    $ ls -r
    tmp          System       Pictures     Movies       Dropbox      Documents    Applications
    Workspace    Public       Music        Library      Downloads    Desktop
  5. List directory content based on access time or other options.
    $ ls -lt
    total 0
    drwxr-xr-x  12 user  group   384 Jul 21 09:50 Workspace
    drwx------@ 19 user  group   608 Jul  4 15:18 Dropbox
    drwx------+  6 user  group   192 Jul  4 11:47 Movies
    drwx------@ 83 user  group  2656 Jun 27 14:11 Library
    drwx------+  6 user  group   192 Jun 25 16:13 Music
    lrwx------@  1 user  group    41 Jun 25 15:22 Desktop -> Library/CloudStorage/iCloud Drive/Desktop
    lrwx------@  1 user  group    43 Jun 25 15:22 Documents -> Library/CloudStorage/iCloud Drive/Documents
    drwx------+  6 user  group   192 Jun 25 15:18 Pictures
    drwxr-xr-x   3 user  group    96 Aug  4  2018 System
    lrwxr-xr-x   1 user  group    28 Jul 10  2018 Downloads -> /Volumes/SD/user/Downloads
    lrwxr-xr-x   1 user  group    22 Jul 10  2018 tmp -> /Volumes/SD/user/tmp
    drwx------@  4 user  group   128 Jun 28  2018 Applications
    drwxr-xr-x+  4 user  group   128 Jun 28  2018 Public
  6. Sort directory content for the specific option in reverse.
    $ ls -lrt
    total 0
    drwxr-xr-x+  4 user  group   128 Jun 28  2018 Public
    drwx------@  4 user  group   128 Jun 28  2018 Applications
    lrwxr-xr-x   1 user  group    22 Jul 10  2018 tmp -> /Volumes/SD/user/tmp
    lrwxr-xr-x   1 user  group    28 Jul 10  2018 Downloads -> /Volumes/SD/user/Downloads
    drwxr-xr-x   3 user  group    96 Aug  4  2018 System
    drwx------+  6 user  group   192 Jun 25 15:18 Pictures
    lrwx------@  1 user  group    43 Jun 25 15:22 Documents -> Library/CloudStorage/iCloud Drive/Documents
    lrwx------@  1 user  group    41 Jun 25 15:22 Desktop -> Library/CloudStorage/iCloud Drive/Desktop
    drwx------+  6 user  group   192 Jun 25 16:13 Music
    drwx------@ 83 user  group  2656 Jun 27 14:11 Library
    drwx------+  6 user  group   192 Jul  4 11:47 Movies
    drwx------@ 19 user  group   608 Jul  4 15:18 Dropbox
    drwxr-xr-x  12 user  group   384 Jul 21 09:50 Workspace
Discuss the article:

Comment anonymously. Login not required.

Share!