ls -r Command
The ls -r command in Linux is used to list the contents of a directory in reverse order. By default, ls sorts files and directories alphabetically. The -r option reverses this sorting order, whether it's alphabetical, by time, or by size.
Syntax
Description
The -r (or --reverse) option modifies the sorting behavior of the ls command. It's particularly useful when combined with other sorting options like -t (sort by time) or -S (sort by size) to get results in ascending order instead of the default descending order.
Key aspects of ls -r:
- Reverses the default alphabetical sort order.
- Reverses the time sort order (oldest first when combined with
-t). - Reverses the size sort order (smallest first when combined with
-S). - Applicable to both files and directories.
Common Options (with -r)
| Option | Description |
|---|---|
-l |
Use a long listing format (e.g., ls -lr). |
-t |
Sort by modification time, newest first (ls -rt for oldest first). |
-S |
Sort by file size, largest first (ls -rS for smallest first). |
-h |
With -l, print sizes in human readable format (e.g., 1K, 234M, 2G). |
-a |
Do not ignore entries starting with . (hidden files). |
Examples
List files in reverse alphabetical order
# Example Output: z_file.txt y_file.txt x_file.txt
Lists files and directories in reverse alphabetical order.
List files by modification time, oldest first
Combines reverse (-r) with sort by time (-t) to show oldest files first.
List files by size, smallest first
Combines reverse (-r) with sort by size (-S) to show smallest files first.
Long listing in reverse alphabetical order
Provides detailed information in long format, sorted in reverse alphabetical order.
List all files (including hidden) in reverse alphabetical order
Shows all files, including hidden ones, sorted in reverse alphabetical order.