DEV Community

dpm_bush
dpm_bush

Posted on Originally published at sshflow.com

Sort Linux Files by Date with ls (and Know Which Date You’re Seeing)

When you run ls -lt, the newest files appear first. Add -r to put the oldest first. The important detail: this sorts by modification time, not necessarily the date a file was created.

ls -lt    # newest modified first
ls -ltr   # oldest modified first
Enter fullscreen mode Exit fullscreen mode

That distinction matters when you’re checking logs, comparing files, or trying to work out when something changed. Here’s how to choose the right timestamp and read the results.

Start with modification time

Modification time, or mtime, records when a file’s contents were last changed. It’s the timestamp most people mean when they ask to sort files by date, and it’s the one ls -l normally displays.

The -t option sorts by mtime, newest first. The -r option reverses the active order; it doesn’t mean “reverse chronological” on its own. Together, -tr puts the oldest entries first.

For example, when checking a directory of logs, ls -ltr places the newest log at the bottom of the output. That can be easier to spot in a long listing than the default newest-first order.

To include dotfiles, add -a:

ls -lat    # newest modified first, including hidden files
ls -latr   # oldest modified first, including hidden files
Enter fullscreen mode Exit fullscreen mode

For a refresher on what long listings and hidden-file output show, see this explanation of the columns in ls -al.

Choose the timestamp you actually need

“Date” can refer to several different file timestamps. ls can sort on more than one, but the options don’t all mean what their names might suggest.

  • Modification time (mtime): when file contents last changed. Use -t.
  • Access time (atime): when the file was last read. Use -u with -t, as in ls -ltu.
  • Status-change time (ctime): when file metadata changed, such as permissions or ownership. Use -c with -t, as in ls -ltc.
  • Birth or creation time: when the file was created, if the system records it and ls supports displaying it.

A common source of confusion is ctime. It is not creation time. Running chmod or chown can change ctime even if the file’s contents stay exactly the same.

Creation-time options vary by platform. With GNU ls on Linux, you can request birth time like this:

ls -lt --time=birth
Enter fullscreen mode Exit fullscreen mode

GNU ls also accepts --time=creation as a name for the same timestamp. Whether a birth time is available depends on the filesystem and platform; it isn’t guaranteed for every file.

On macOS/BSD, the corresponding option is different:

ls -ltU
Enter fullscreen mode Exit fullscreen mode

Add -r to reverse the order on either platform. Don’t assume GNU-specific options such as --time=birth work with the macOS/BSD version of ls.

Make long listings easier to read

You can combine the common options for a more useful view:

ls -lhat
Enter fullscreen mode Exit fullscreen mode

This shows a long listing, includes hidden files, uses human-readable sizes, and sorts newest modified first. To put the oldest first, add -r:

ls -lhatr
Enter fullscreen mode Exit fullscreen mode

Long listings may show a time of day for some files and a year for others. On GNU/Linux, use --time-style=full-iso to display a full timestamp:

ls -lt --time-style=full-iso
Enter fullscreen mode Exit fullscreen mode

On macOS/BSD, use -T with the long format:

ls -ltT
Enter fullscreen mode Exit fullscreen mode

The exact output format differs across implementations, so use the option supported by your system.

What ls -t does not do

ls -R lists nested directories recursively, but it sorts each directory’s entries independently. Adding -t does not turn the whole directory tree into one globally date-sorted list.

Also, don’t depend on a particular order when two files have identical timestamps. For ordinary interactive browsing, ls is useful; for scripts that need a deterministic order, define the sorting behavior explicitly rather than relying on how ls orders ties.

If you’re comparing another common listing sort, this guide to sorting files by size with ls covers the -S option and its limitations.

Quick reference

Goal Command
Newest modified first ls -lt
Oldest modified first ls -ltr
Include hidden files, newest first ls -lat
Include hidden files, oldest first ls -latr
Sort by access time ls -ltu
Sort by status-change time ls -ltc
Sort by birth time with GNU ls ls -lt --time=birth
Sort by creation time with macOS/BSD ls ls -ltU

For the everyday case, remember ls -lt for newest modified first and ls -ltr for oldest modified first. If you specifically need creation time, check which ls implementation you’re using—and whether that timestamp is available on your filesystem.

I originally published a more detailed version of this guide on the SSHFlow blog.

I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.

Top comments (0)