Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Sort ls Output by Last Modified Date and Time

· 6 min read · Home Tech

In this article
  1. Why Sorting by Modification Time Matters
  2. Basic Syntax and Common Variations
  3. How ls Determines "Last Modified"
  4. Reversing the Order and Combining with Other Filters
  5. Common Pitfalls and Misconceptions
  6. Alternatives and More Powerful Tools
  7. Frequently Asked Questions

To sort ls output by last modified date and time, use ls -lt (newest first) or ls -ltr (oldest first). The -t flag sorts by modification time, and adding -r reverses the order.

Why Sorting by Modification Time Matters

When you work in a directory with many files, the default alphabetical listing rarely shows you what you actually need. You often want to know which files were changed most recently—maybe you just edited a config file and want to confirm it's at the top, or you're cleaning up old logs and need to see the oldest ones first. Sorting by modification time gives you a quick, at-a-glance view of recent activity without needing to open each file or check timestamps manually.

The ls command has been part of Unix-like systems for decades, and its time-sorting options are stable and consistent across Linux, macOS, and BSD systems. Understanding them saves you from piping output into sort or writing a script every time you need a chronological view.

Basic Syntax and Common Variations

The core command is simple:

  • ls -lt — lists files sorted by modification time, newest first.
  • ls -ltr — same but reversed, oldest first.
  • ls -t — just the names, sorted by time, no details.
  • ls -lt --time=atime — sort by access time instead of modification time (GNU ls).
  • ls -lt --time=ctime — sort by change time (when metadata last changed).

You can combine -t with any other flags. For example, ls -lth adds human-readable sizes (like 4.2K), and ls -lt --color keeps color coding for file types. On macOS, the --time option isn't available, but you can use -U for creation time (on some file systems) or rely on the default modification time.

How ls Determines "Last Modified"

Every file has three timestamps stored in its inode (on traditional Unix file systems):

  • mtime — last time the file's content was modified. This is what -t uses by default.
  • atime — last time the file was read. This updates when you cat or open it, but many systems disable atime updates to improve performance.
  • ctime — last time the file's metadata changed, like permissions or ownership. It also updates when mtime changes.

When you run ls -l, the date shown is the mtime by default. The -t flag simply sorts by that same field. If you want to see the exact timestamp with seconds, add --full-time (GNU) or use stat for a detailed breakdown.

Reversing the Order and Combining with Other Filters

The -r flag reverses whatever order ls would normally produce. So ls -ltr gives you the oldest file at the bottom, which is handy when you want to see the most recent files at the end of the output—useful when you're appending to a log and want to see the latest entry without scrolling up.

You can also combine time sorting with filtering:

  • ls -lt *.log — sort only .log files by time.
  • ls -lt | head -20 — show the 20 most recently modified files.
  • ls -ltr | tail -20 — show the 20 oldest files.
  • find . -maxdepth 1 -type f -printf '%T@ %p\n' | sort -n — a more powerful alternative that gives you full control over formatting.

Common Pitfalls and Misconceptions

One frequent mistake is assuming -t sorts by creation time. It does not—it sorts by modification time. If you need creation time, you'll need to use stat or ls -lU (on macOS with APFS) or ls --time=birth (on GNU coreutils 8.31+). Another gotcha: directories are included in the listing, and their modification time changes when files are added or removed inside them. So a directory might appear at the top even if you didn't touch its contents directly.

Also, ls sorts by the timestamp's numeric value, not by the formatted string. That's good—it means files from different years sort correctly. But if you're using a locale that displays dates differently, the sort is still based on the underlying numeric time, so it's consistent.

One more misconception: ls -lt does not show the time in real-time as files change. It's a snapshot. If you want to monitor a directory for changes, use watch -n 1 'ls -lt' or a tool like inotifywait.

Alternatives and More Powerful Tools

While ls is great for quick glances, sometimes you need more control:

  • find — with -printf you can format output and sort with sort.
  • stat — shows all timestamps for a single file.
  • tree -t — if you have the tree utility, it can sort directories and files by time recursively.
  • ls -lt --time-style=long-iso — gives you a clean, sortable date format like 2024-03-15 14:22.

For scripting, you might prefer find ... -printf '%T@ %p\n' | sort -n because it gives you the epoch time, which is easy to compare and convert.

Frequently Asked Questions

How do I sort by modification time in reverse order?

Use ls -ltr. The -r flag reverses the sort order, so you get oldest first. This is useful when you want to see the most recently modified file at the bottom of the list, which is handy when you're tailing a directory and want to see the latest addition without scrolling up.

Why does ls -t show directories at the top?

Directories are files too, and their modification time updates whenever you add, remove, or rename a file inside them. So if you just created a file in a directory, that directory's mtime changes, and it may appear at the top of the sort. If you want to exclude directories, use ls -lt | grep -v '^d' or find . -maxdepth 1 -type f -printf '%T@ %f\n' | sort -n.

Can I sort by access time or change time?

Yes, with GNU ls (Linux) you can use --time=atime or --time=ctime. On macOS, these flags aren't available, but you can use stat -f '%a %N' to get access time and sort externally. Note that atime is often not updated on modern systems due to relatime or noatime mount options, so it may not be reliable.

How do I see the full timestamp with seconds?

Use ls -lt --full-time on Linux. This shows the date and time down to the second, which is useful for scripts or when you need to compare exact times. On macOS, ls -lT shows a longer format that includes seconds.

Does ls -t work on all Unix-like systems?

Yes, -t is part of the POSIX standard, so it works on Linux, macOS, BSD, and most other Unix-like systems. The output format may vary slightly, but the sorting behavior is consistent. The --time option is a GNU extension, so it's only on Linux by default.

What if I want to sort by modification time recursively?

ls doesn't do recursion well. Use find . -type f -printf '%T@ %p\n' | sort -n to get a recursive list sorted by time. If you prefer a tree view, tree -t will sort directories and files by time, but it may not show timestamps by default.