The ls command in Linux is the primary tool for listing directory contents. By default, it organizes files and directories in alphabetical order. In some cases, you may need to reverse this order, such as when viewing files from newest to oldest or in reverse alphabetical order.

The ls command includes built-in options to reverse the listing directly. This method is simple and efficient for most tasks, allowing you to control the output without external tools. Options like reversing based on time, size, or even just the standard alphabetical order are available.

These features are useful when managing large numbers of files or when a specific order is needed for clarity. Understanding how to reverse listings using ls helps you efficiently manage directory contents on Linux.

Steps to reverse directory listing in Linux:

  1. Open the terminal.
  2. List the contents of a directory.
    $ ls
    Applications Documents    Dropbox      Movies       Pictures     System       tmp
    Desktop      Downloads    Library      Music        Public       Workspace
  3. Pipe the output of ls to the sort command for reverse sorting.
    $ ls | sort -r
    tmp
    Workspace
    System
    Public
    Pictures
    Music
    Movies
    Library
    Dropbox
    Downloads
    Documents
    Desktop
    Applications

    The sort -r command allows you to reverse the order based on specific sorting criteria if necessary.

  4. Reverse the order of the directory listing.
    $ ls -r
    tmp          System       Pictures     Movies       Dropbox      Documents    Applications
    Workspace    Public       Music        Library      Downloads    Desktop
  5. Display directory contents sorted by modification time or other criterion.
    $ 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. Display directory contents sorted by modification time or other 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

    The -t option sorts files by modification time, and combining it with -r shows them in reverse order.

Discuss the article:

Comment anonymously. Login not required.