Find and Sort Files by Modification Date in Linux
In this article
To find and sort files by modification date in Linux, use the ls -lt command to list files sorted by modification time, newest first, or find with -printf and sort for more control. The ls command is the simplest for a quick look, while find is better for searching across directories and filtering by date.
Understanding File Timestamps in Linux
Linux tracks three distinct timestamps for each file, and it's important to know which one you're sorting by:
- Modification time (mtime): The last time the file's content was changed. This is what most people mean by "modification date."
- Change time (ctime): The last time the file's metadata (permissions, ownership, etc.) changed. It also updates when the content changes.
- Access time (atime): The last time the file was read or executed.
The ls -l command displays the modification time by default. The find command can filter by any of these using -mtime, -ctime, or -atime.
Using ls to Sort by Modification Date
The ls command is the quickest way to see files sorted by modification time. The key options are:
ls -lt: Sorts by modification time, newest first. This is the most common usage.ls -ltr: Sorts by modification time, oldest first (reverse order). Useful for seeing which files haven't been touched in a while.ls -lt --time=ctime: Sorts by change time instead of modification time.ls -lt --time=atime: Sorts by access time.
You can also add -h for human-readable sizes (e.g., ls -lth) and -a to include hidden files (e.g., ls -lta).
Example output of ls -lt:
-rw-r--r-- 1 user group 1024 Jan 5 14:32 report.txt
-rw-r--r-- 1 user group 2048 Jan 4 09:15 data.csv
drwxr-xr-x 2 user group 4096 Jan 3 18:00 backupsThe date and time shown are the modification time for each file.
Using find to Locate and Sort Files by Date
When you need to search an entire directory tree, find is more powerful. To list files sorted by modification time, you can use find with -printf to output the timestamp and filename, then pipe to sort:
find /path/to/dir -type f -printf '%T@ %p
' | sort -n%T@prints the modification time as a Unix epoch timestamp (seconds since 1970-01-01), which sorts numerically.%pprints the full path.sort -nsorts numerically, oldest first. Add-rfor newest first.
To show the date in a human-readable format, use %TY-%Tm-%Td %TH:%TM instead of %T@:
find . -type f -printf '%TY-%Tm-%Td %TH:%TM %p
' | sortTo filter by modification time, use -mtime. For example, to find files modified within the last 7 days:
find /path -type f -mtime -7The number is in 24-hour periods, so -mtime -7 means less than 7 days ago, and -mtime +7 means more than 7 days ago.
Sorting with stat and sort
Another approach is to use stat to get the modification time of each file, but it's less efficient for many files. A common pattern is:
stat -c '%Y %n' * | sort -nThis prints the epoch time and filename for all files in the current directory, then sorts numerically. The %Y is the modification time, and %n is the filename. Add -r to sort newest first.
This works well for a single directory, but find is better for recursive searches.
Sorting Directories and Files Together
By default, ls -lt sorts directories and files together by their modification time. If you want to list only files or only directories, you can use find with -type f or -type d.
For example, to list only files sorted by modification time:
find . -maxdepth 1 -type f -printf '%T@ %p
' | sort -nTo list only directories:
find . -maxdepth 1 -type d -printf '%T@ %p
' | sort -nThe -maxdepth 1 limits the search to the current directory, avoiding recursion.
Common Pitfalls and Misconceptions
One common mistake is confusing ctime with creation time. Linux does not store a file creation time on most filesystems (ext4 does have a birth time, but it's not universally accessible). The ctime is the change time, which updates when metadata changes, not when the file is created.
Another pitfall is that ls -lt sorts by the modification time, but if you use ls -l without -t, files are sorted alphabetically. Also, ls -lt shows the time in the format "Jan 5 14:32" for current-year files, but for older files it shows just the year (e.g., "Jan 5 2023"). This can be misleading if you're not aware of it.
When using find with -printf '%T@', the timestamp is a float with fractional seconds. Sorting numerically works fine, but if you want to convert to a human-readable format later, you can use date -d @timestamp.
Frequently Asked Questions
How do I list files sorted by modification date in Linux?
Use ls -lt to list files in the current directory sorted by modification time, newest first. For older files first, use ls -ltr. To include hidden files, add -a.
How do I find files modified in the last 24 hours?
Use find /path -type f -mtime -1. The -mtime -1 means modified less than 1 day (24 hours) ago. For the last 7 days, use -mtime -7.
What is the difference between mtime, ctime, and atime?
mtime is the last time the file content was modified. ctime is the last time the file's metadata (like permissions or owner) changed, and it also updates when content changes. atime is the last time the file was accessed (read or executed).
How do I sort files by modification date recursively?
Use find . -type f -printf '%T@ %p
' | sort -n. This will find all files in the current directory and subdirectories, print their modification timestamp and path, and sort them by timestamp.
Can I sort by modification date in the GUI file manager?
Yes, most Linux file managers (like Nautilus, Dolphin, Thunar) have a "Sort by" option in the view menu. Select "Date Modified" or "Modified" to sort files by modification time.
How do I find the oldest file in a directory?
Use ls -ltr | head -1 to show the oldest file (the last line of a reverse-sorted list). Or use find . -type f -printf '%T@ %p
' | sort -n | head -1 for a recursive search.