ls -r reverse order in linux/unix

The ls -r command in Linux reverses the order of the listing, showing the oldest files first when sorted by modification time or the alphabetically last files when sorted by name. Here’s what the -r option does in different contexts:

  1. Reverse alphabetical order:
    When you use ls without any sorting criteria, it lists items in alphabetical order. Adding the -r option will reverse this order:
   ls -r
  1. Reverse order by modification time:
    If you combine -r with the -lt options, the listing will be sorted by modification time in descending order (oldest files first):
   ls -lrt
  1. Reverse order by file size:
    Similarly, combining -r with the -lS options will list files starting with the smallest:
   ls -lSr
  1. Reverse order with detailed information:
    You can also use -r with the -l option to get a detailed listing in reverse order:
   ls -lr
  1. Reverse order in a recursive listing:
    When using ls -R to list all files in a directory and its subdirectories, adding -r will reverse the order of the output for each subdirectory:
   ls -Rr
  1. Reverse order with human-readable sizes:
    Combining -r with the -lh options will list files with human-readable sizes in reverse order:
   ls -lhr

Remember that the -r option only reverses the order of the listing as determined by other options given to ls. If no sorting option is provided, -r will simply reverse the alphabetical order of the files and directories.

This entry was posted in File & Directory, Ls Command and tagged , , , . Bookmark the permalink.