Reversing directory listings in Linux helps quickly spot items at the end of an alphabetical list or flip the usual view of files. This is useful when scanning long directory listings, checking tail entries, or comparing output with scripts or other tools that assume a particular order.
The ls command controls how directory entries are displayed, including sorting by name, modification time, or size. Options such as -r for reverse order and -t for time-based sorting change only the presentation of entries, without modifying the files themselves. Combining these options produces reverse alphabetical listings, newest-to-oldest views, or other useful layouts.
Choosing between built-in ls options and external tools such as sort depends on how much control is needed over the ordering. Large directories, locale-specific collation, and scripts that parse ls output can be affected by sort options, so ordering should be chosen carefully to keep command behavior predictable.
Steps to reverse directory listing in Linux:
- Open a terminal on the Linux system.
$ whoami root
- Display directory contents in the default alphabetical order.
$ ls /root/sg-work/reverse-demo first.log second.log third.log
- Reverse the alphabetical order using the -r option.
$ ls -r /root/sg-work/reverse-demo third.log second.log first.log
The -r option reverses the current sort order without changing the primary sort criterion.
- List files sorted by modification time, newest first, using -t.
$ ls -lt /root/sg-work/reverse-demo total 12 -rw-r--r-- 1 root root 6 Dec 22 2025 third.log -rw-r--r-- 1 root root 7 Dec 22 2025 second.log -rw-r--r-- 1 root root 6 Dec 22 2025 first.log
- Show files sorted by modification time in reverse (oldest first) using -lrt.
$ ls -lrt /root/sg-work/reverse-demo total 12 -rw-r--r-- 1 root root 6 Dec 22 2025 first.log -rw-r--r-- 1 root root 7 Dec 22 2025 second.log -rw-r--r-- 1 root root 6 Dec 22 2025 third.log
The combination -l, -t, and -r shows detailed information, sorts by modification time, and reverses that order.
- Use sort -r in a pipeline when a reverse ordering is needed after additional processing.
$ ls -1 /root/sg-work/reverse-demo first.log second.log third.log $ ls -1 /root/sg-work/reverse-demo | sort -r third.log second.log first.log
Using ls -1 with sort provides one entry per line, which is convenient for further text processing and scripting.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.
