Shell Scripting

Master Shell Sort by Timestamp: 5 Pro Commands for 2025

Tired of messy file lists? Master shell sort by timestamp with these 5 pro commands for 2025. Go beyond `ls -t` with `find`, `zsh`, `stat`, and more.

D

David Chen

A senior DevOps engineer and command-line enthusiast passionate about automating everything.

7 min read22 views

Ever found yourself drowning in a digital sea of log files, Git commits, or downloaded assets, desperately trying to find that one file from last Tuesday afternoon? We've all been there. You type ls, and a chaotic, alphabetically-sorted list mocks your attempt at order. It's a common frustration that can grind productivity to a halt.

In the world of the command line, time is more than just a number on a clock—it's a fundamental piece of metadata that can unlock powerful workflows. Sorting files by when they were last touched, created, or modified isn't just a neat trick; it's an essential skill for debugging, auditing, and making sense of complex systems. While most of us know ls -t, the real power lies in the commands that let you slice, dice, and query files based on their temporal footprint. In 2025, with files scattered across local machines, Docker containers, and cloud buckets, mastering timestamp sorting is non-negotiable.

Command 1: The ls Power-Up for Reverse Chronological Order

Let's start with the classic. You probably know ls -t sorts by modification time, with the newest files first. But the real workhorse for sysadmins and developers is its reverse-order sibling: ls -ltr.

Why is this combination so beloved? When you're watching a log directory or checking a folder for recent changes, you often want to see the most recent activity at the bottom of your screen, where your cursor naturally lands. This is exactly what ls -ltr does.

  • l: Use a long listing format (shows permissions, owner, size, and modification timestamp).
  • t: Sort by modification time.
  • r: Reverse the order of the sort.

Here’s how it looks in action:

$ ls -ltr /var/log

-rw-r--r--   1 root      wheel           0 Jan 14 10:00 old_log.log
-rw-r--r--   1 root      wheel        1024 Jan 14 15:30 another_log.log
-rw-r-----   1 _logd     _logd      256000 Jan 15 09:25 system.log
-rw-r-----   1 davidchen staff       12288 Jan 15 09:28 app_debug.log

Instantly, the most recently modified file, app_debug.log, is at the bottom. It's simple, fast, and should be muscle memory for anyone working in the shell.

Command 2: find - Your Time-Traveling Detective

While ls is great for viewing a single directory, find is your go-to for searching recursively through a directory tree based on time. It's incredibly powerful for forensics, cleanup scripts, and tracking down recent changes across an entire project.

The key flags are -mtime (modification time in days) and -mmin (modification time in minutes).

Finding Recently Modified Files

To find all files in the current directory and its subdirectories modified in the last 60 minutes:

# Find files modified in the last 60 minutes
$ find . -type f -mmin -60

To find files modified in the last 7 days:

# Find files modified in the last 7 days
$ find . -type f -mtime -7

The magic is in the numbers: -7 means *less than* 7 days ago, +7 means *more than* 7 days ago, and 7 means *exactly* 7 days ago.

Combining find with other commands

Advertisement

The real power comes from piping find's output. What if you want to find all Python files modified in the last 24 hours and then list them in sorted order?

# Find .py files modified in last day, then list sorted by time
$ find . -name "*.py" -mtime -1 -print0 | xargs -0 ls -lt

This is a pro-level pattern. Here's the breakdown:

  • find . -name "*.py" -mtime -1: Finds all files ending in .py modified less than one day ago.
  • -print0: This is crucial. It separates filenames with a null character instead of a newline, safely handling files with spaces or other weird characters.
  • | xargs -0: This reads the null-separated list from find and passes it securely to the next command.
  • ls -lt: Our old friend, which takes the file list and displays it in a long, time-sorted format.

Command 3: zsh's Glob Qualifiers - The Shell's Hidden Gem

If you're using Zsh (the default shell on macOS and a popular choice on Linux), you have a superpower: glob qualifiers. These let you modify the behavior of wildcards (globs) directly in your command, without needing find or xargs.

The syntax is *(qualifier). For time sorting, the most useful qualifier is om (order by modification time) or Om (reverse order by modification time).

Want to list the 5 most recently modified files in the current directory and all subdirectories?

# List the 5 most recently modified files recursively
$ ls -ld **/*(om[1,5])

Let's dissect this elegant command:

  • **/*: A recursive glob that matches all files and directories in the current location and below.
  • (...): The parentheses signal that we're adding glob qualifiers.
  • om: order by modification time (newest first). Use Om for oldest first.
  • [1,5]: This is a selector that picks the first 5 results from the sorted list.

This is significantly faster and easier to type than the find | xargs equivalent. It's a perfect example of how choosing the right shell can elevate your command-line game.

Command 4: stat + sort - The Ultimate Custom Sort

What if you need to sort by something other than modification time? Maybe you need to sort by access time (when the file was last read) or creation time (birth time). ls can't do this easily on all systems, but the combination of stat and sort gives you complete control.

stat is a command that provides detailed metadata about a file, and its output is highly customizable.

The goal is to have stat print the Unix epoch timestamp (a simple integer) followed by the filename, and then use sort's numeric sort capability.

Example for macOS / BSD Systems

# List files sorted by creation (birth) time, newest first
$ stat -f "%B %N" * | sort -rn
  • stat -f "%B %N" *: For every file (*), print its birth time as a Unix timestamp (%B) followed by its name (%N).
  • | sort -rn: Pipe the output to sort. -r reverses the sort (so higher numbers/newer times are first), and -n performs a numeric sort.

Example for GNU/Linux Systems

The flags for GNU stat are different but the principle is the same.

# List files sorted by modification time, newest first
$ stat -c "%Y %n" * | sort -rn
  • stat -c "%Y %n" *: Here, %Y is the modification time as a Unix timestamp, and %n is the filename. To get birth time on filesystems that support it, you would use %W.

This method is the most flexible. You can sort by access time (%A on macOS, %X on Linux) or any other time attribute the stat command on your OS exposes.

Command 5: rclone / aws s3 ls - Taming Cloud Timestamps

In 2025, many of our files don't live on a local disk. They're in an S3 bucket, a Google Drive, or some other form of cloud storage. Your local shell commands can't see them, but powerful tools like rclone and cloud-specific CLIs can.

rclone is the swiss-army knife for cloud storage. It can interact with dozens of backends, and it has powerful sorting and filtering capabilities.

To list files in an S3 bucket and sort them by modification time, you can't just use ls. Instead, you can use rclone's own listing command and pipe it to sort.

# List all files in a bucket/folder, sorted by mod time (newest first)
$ rclone lsf --format "tp" my-s3-remote:my-bucket/logs/ | sort -r
  • rclone lsf: A command to list files with formatting.
  • --format "tp": Tells rclone to output the modification time (as an ISO 8601 string) and the path.
  • | sort -r: Since the date is in a standard format (YYYY-MM-DD...), a standard reverse alphabetical sort (-r) works perfectly to sort by time.

If you're deep in the AWS ecosystem, the aws-cli can do this too, though it often requires a bit more wrangling with tools like jq to parse the JSON output. The principle remains the same: get the timestamp and filename, then sort.

Putting It All Together: A Comparison Table

Here’s a quick-reference table to help you choose the right tool for the job.

Command Best Use Case Pros Cons
ls -ltr Quickly viewing recent files in a single directory. Simple, fast, available everywhere. Not recursive, limited sorting options.
find ... | xargs ls Complex, recursive searches across a filesystem. Extremely powerful filtering (name, size, permissions, etc.). Verbose syntax, can be slow on large filesystems.
ls **/*(om) (Zsh) Elegant, recursive sorting for Zsh users. Concise, fast, and easy to read. Zsh-specific; not available in Bash or other shells.
stat ... | sort Sorting by non-standard timestamps like creation or access time. Ultimate flexibility, precise control. Syntax differs between macOS/BSD and Linux.
rclone lsf ... | sort Sorting files in cloud storage (S3, Google Drive, etc.). Works across dozens of cloud providers. Requires rclone setup and configuration.

Conclusion: Become a Timestamp Master

Navigating filesystems efficiently is a hallmark of a skilled developer or sysadmin. By moving beyond a simple ls and embracing the power of tools like find, Zsh glob qualifiers, stat, and cloud-aware CLIs, you transform the command line from a simple file lister into a powerful temporal database. You can answer questions like “What config files were changed right before the crash?” or “Which assets were added to the project this week?” in seconds.

These five commands are more than just shortcuts; they are different lenses through which to view your data. Start by integrating ls -ltr into your daily routine, experiment with find for your next debugging session, and if you're on Zsh, give those glob qualifiers a try. Mastering them will save you countless hours and make you a far more effective operator in any shell environment.

Tags

You May Also Like