Skip to main content

How to List Files by Date

• 2 min read
bash

Quick Answer: List Files by Date

To list files sorted by modification date in Bash, use ls -lt for newest first or ls -ltr for oldest first. Use find with -mtime or -mmin for more precise date filtering. For custom date formats, use stat.

Quick Comparison: Date-Based File Sorting

MethodOrderFormatBest For
ls -ltNewest firstModification timeQuick view
ls -ltrOldest firstModification timeQuick view
find -mtimeBy daysFlexibleDate filtering
find -mminBy minutesPreciseRecent files
stat -c %yFull timestampISO formatScripting

Bottom line: Use ls -lt for quick listing, use find -mtime for date-based filtering.


Sort and list files by modification date in ascending or descending order.

List Files by Date (Newest First)

# Sort by modification time, newest first
ls -lt

# With full date
ls -lht --full-time

# Show date in readable format
ls -l | head -20

List Newest First with Time

# Newest files first
ls -lt

# Output shows newest at top:
# -rw-r--r-- Feb 21 14:30 newest.txt
# -rw-r--r-- Feb 20 10:15 older.txt

Oldest Files First

# Reverse sort (oldest first)
ls -ltr

# Or with -r flag
ls -lt --reverse

Using find with Sort

# Find files and sort by modification time
find . -type f -printf '%T@ %p\n' | sort -n | tail -10

Detailed Date Format

# Fancy date format with readable dates
ls -lh | awk '{print $6, $7, $8, $9}' | sort

# Format: Month Day Time Filename

Practical Example: Recent File Report

#!/bin/bash

directory="${1:-.}"
count="${2:-20}"

echo "=== Recent Files (Last $count) ==="
echo ""

ls -lht "$directory"/* 2>/dev/null | \
  head -$count | \
  awk '{printf "%s %2d %5s  %s\n", $6, $7, $8, $9}'

Usage:

$ ./recent_files.sh /var/log 10

Find and Sort by Date

#!/bin/bash

# Find recent files
find . -type f -newermt "2 days ago" -printf '%T@ %p\n' | \
  sort -rn | \
  head -10 | \
  cut -d' ' -f2-

Common Mistakes

  1. Using -t without -l - must have -l for sorting
  2. Not specifying directory - ls needs target
  3. Forgetting to quote variables

Key Points

  • Use ls -lt for newest first
  • Use ls -ltr for oldest first
  • Use find for more complex filtering
  • Always quote directory variables

Summary

Listing files by modification date is useful for maintenance. Use ls -lt for quick sorting or find for more complex filtering based on date ranges.