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:
- Reverse alphabetical order:
When you usels
without any sorting criteria, it lists items in alphabetical order. Adding the-r
option will reverse this order:
ls -r
- 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
![](https://www.linuxcommands.site/wp-content/uploads/2024/07/image-4-1024x562.png)
- Reverse order by file size:
Similarly, combining-r
with the-lS
options will list files starting with the smallest:
ls -lSr
- Reverse order with detailed information:
You can also use-r
with the-l
option to get a detailed listing in reverse order:
ls -lr
- Reverse order in a recursive listing:
When usingls -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
- 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.