Skip to content
shellhacks.dev
Command Line

List files sorted by modification time

List files newest first with ls -t and oldest first with ls -tr. The sort uses the modification time, and the long listing shows the times it sorted by.

1 min read
Summarize in AI

You want the most recently changed file at the top of a listing, or at the bottom, where it stays on screen.

Shell
touch -d '2026-01-01 10:00' old
touch -d '2026-03-01 10:00' middle
touch -d '2026-06-01 10:00' new
ls -t
echo ---
ls -tr
echo ---
ls -lt --time-style=long-iso
Text
new
middle
old
---
old
middle
new
---
total 0
-rw-r--r-- 1 root root 0 2026-06-01 10:00 new
-rw-r--r-- 1 root root 0 2026-03-01 10:00 middle
-rw-r--r-- 1 root root 0 2026-01-01 10:00 old

-t sorts by modification time, newest first, and -r reverses whatever order is selected, so -tr puts the newest last, as the coreutils manual describes. The long format with --time-style=long-iso prints the exact date the sort used, which makes it easy to see that the order and the dates agree.

A directory's modification time changes when an entry is added or removed, not when a file inside it is edited, so sorting directories this way answers a different question than sorting files. Files with the same time are ordered by name. -u sorts by access time and -c by change time. Time styles are a GNU option; BSD ls uses -D.

Share

More snippets

Command Line

List hidden files only

List hidden files only with ls -d .[!.]* in GNU coreutils 9 or any POSIX shell. The glob skips the dot and double dot entries that .* would match.

1 min read
Command Line

Show the full timestamp of files

Show the full timestamp of every file with ls -l --full-time in GNU coreutils. It prints nanosecond precision and the zone, which ls -l hides.

1 min read
Command Line

Count the files in a directory

Count the files in a directory with ls -A | wc -l, or with find when names may contain newlines. A plain ls -1 skips every hidden file.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.