The ls command is used to display directory content in Linux terminal and by default sort the output in alphabetical order. The output could be piped to other tools for further processing including to the sort utility to further sort the output in reverse order.

ls has the sort function built in, and is actually more useful and flexible than using external tools for the same purpose.

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. List directory content in reverse using ls.
    $ ls -r
    tmp          System       Pictures     Movies       Dropbox      Documents    Applications
    Workspace    Public       Music        Library      Downloads    Desktop
  4. List directory content based on access time or other switches
    $ ls -lt
    total 0
    drwxr-xr-x  12 user  group   384 Jul 21 09:50 Workspace
    [email protected] 19 user  group   608 Jul  4 15:18 Dropbox
    drwx------+  6 user  group   192 Jul  4 11:47 Movies
    [email protected] 83 user  group  2656 Jun 27 14:11 Library
    drwx------+  6 user  group   192 Jun 25 16:13 Music
    [email protected]  1 user  group    41 Jun 25 15:22 Desktop -> Library/CloudStorage/iCloud Drive/Desktop
    [email protected]  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
    [email protected]  4 user  group   128 Jun 28  2018 Applications
    drwxr-xr-x+  4 user  group   128 Jun 28  2018 Public
  5. Sort directory content for the specific switch in reverse.
    $ ls -lrt
    total 0
    drwxr-xr-x+  4 user  group   128 Jun 28  2018 Public
    [email protected]  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
    [email protected]  1 user  group    43 Jun 25 15:22 Documents -> Library/CloudStorage/iCloud Drive/Documents
    [email protected]  1 user  group    41 Jun 25 15:22 Desktop -> Library/CloudStorage/iCloud Drive/Desktop
    drwx------+  6 user  group   192 Jun 25 16:13 Music
    [email protected] 83 user  group  2656 Jun 27 14:11 Library
    drwx------+  6 user  group   192 Jul  4 11:47 Movies
    [email protected] 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!