The ls command is used to display directory contents in the Linux terminal. By default, the output is sorted alphabetically. However, you might sometimes need to display the contents in reverse order. This can be done using the ls command itself or by piping the output to an external program like sort.

While ls has built-in sorting capabilities and is highly compatible with other ls options, the sort command is a dedicated external program that offers its own advantages for sorting output that you can use.

Steps to list directory content in reverse in Linux:

  1. Open the terminal.
  2. Display directory contents using the ls command.
    $ ls
    Applications Documents    Dropbox      Movies       Pictures     System       tmp
    Desktop      Downloads    Library      Music        Public       Workspace
  3. Pipe the ls command output to the 'sort' command to show the results in reverse order.
    $ ls | sort -r
    tmp
    Workspace
    System
    Public
    Pictures
    Music
    Movies
    Library
    Dropbox
    Downloads
    Documents
    Desktop
    Applications
  4. Directly display directory contents in reverse order using the ls command.
    $ ls -r
    tmp          System       Pictures     Movies       Dropbox      Documents    Applications
    Workspace    Public       Music        Library      Downloads    Desktop
  5. Show directory contents based on access time or other criteria.
    $ 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 contents according to a specific criterion in reverse order.
    $ 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.