Ls Sort Files by Modification Date & Time
In this article
The ls command sorts files by modification date and time when you use the -t flag, which lists newest first. Add the -l flag to see the actual timestamps, and -r to reverse the order to oldest first.
What the ls Command Does
The ls command is one of the most basic and frequently used utilities in Unix-like systems (Linux, macOS, BSD). Its primary job is to list directory contents. By default, ls sorts entries alphabetically, which is convenient when you're looking for a file by name but useless when you want to know what was changed most recently. The command reads directory entries and, depending on the flags you pass, displays metadata like permissions, owner, size, and timestamps. The modification time (often abbreviated mtime) is the timestamp that records when the file's content was last changed. This is different from the change time (ctime), which updates when the file's metadata (like permissions or ownership) changes, and from the access time (atime), which updates when the file is read.
Sorting by Modification Time: The -t Flag
The -t flag tells ls to sort by modification time, newest first. This is the core answer to the question. For example, running ls -t in a directory will show the most recently modified file at the top and the oldest at the bottom. If you want the opposite order (oldest first), combine it with -r (reverse): ls -tr. This is a common pattern for looking at the oldest files in a directory, perhaps to clean up old logs or temporary files.
To see the actual modification date and time next to each file, add the long format flag: ls -lt. This gives you a detailed listing with the modification timestamp in the sixth column. If you want the reverse order with the long format, use ls -ltr. This combination is probably the most used form, as it shows a chronological list of files with their timestamps.
Human-Readable Timestamps and Other Useful Flags
By default, ls -l shows the modification time in a format like Jun 12 14:33. For files modified more than six months ago (or in the future), it shows just the year, like Jun 12 2023. This can be ambiguous. To always see the full date and time, use the --full-time flag (on GNU coreutils, which is standard on Linux). On macOS/BSD, you can use -T to get a full timestamp. If you want a more human-friendly relative format like “2 hours ago”, you can use --time-style=relative on Linux. For a format that includes seconds, --time-style=+%Y-%m-%d %H:%M:%S is very useful. The -h flag (human-readable) makes file sizes easier to read (e.g., 4K instead of 4096), which is often combined with -l and -t: ls -lth.
Sorting by Other Timestamps
Sometimes you need to sort by the access time or the change time, not the modification time. The -t flag always sorts by modification time, but you can override this with --time= (on GNU ls). For example:
ls -lt --time=atimesorts by access time.ls -lt --time=ctimesorts by change time.ls -lt --time=birthsorts by file creation time (if the filesystem supports it).
On macOS/BSD, the equivalent flags are -u (sort by access time) and -c (sort by change time). For example, ls -lu shows files sorted by last access. Note that these are not the same as the modification time, and many users confuse them.
Recursive Sorting and Directories
The ls command does not recurse into subdirectories by default. If you want to list files in subdirectories as well, you need the -R flag. However, ls -Rt will sort each directory separately, not globally. For a true recursive sort by modification time across all files, you need a tool like find with -printf or a command like find . -type f -printf '%T@ %p
' | sort -n. This is a common need when you want to find the most recently changed file in a whole project tree. Also, note that ls sorts directories themselves by their modification time, which is when entries were added or removed from the directory, not when the files inside were modified. This often confuses users who expect a directory's timestamp to reflect its contents.
Common Problems and Misconceptions
One common issue is that the default ls output does not show the modification time at all. Without -l, you only see file names, so ls -t might seem to do nothing except change the order. Always use -l to see the timestamps. Another misconception is that ls -lt shows the file creation time. It does not; it shows the modification time. The creation time is often not even stored by the filesystem (on ext4, it is not stored by default). Also, when you copy a file, the modification time is usually preserved, but the creation time is set to the copy time. If you are scripting, be aware that ls output can contain spaces and special characters, so parsing it is error-prone. For robust scripting, use find or stat instead.
Alternatives to ls for Sorting by Date
While ls is the go-to command, other tools offer more flexibility:
findwith-printf(GNU) or-exec statcan list files with timestamps and sort them.statshows detailed timestamps for a single file.- Graphical file managers (like Nautilus, Finder, or Dolphin) have a “Sort by Date” option in their views.
- The
treecommand can show a directory tree, and with the-toption, it sorts by modification time.
For most interactive use, ls -lt is sufficient. For scripting, find -printf is more reliable. For example, to list the 10 most recently modified files in a directory tree, you could use: find . -type f -printf '%T@ %p
' | sort -rn | head -10. This prints the epoch time and filename, sorts by the number, and takes the top 10.
Frequently Asked Questions
How do I sort files by modification time in reverse order?
Use ls -ltr. The -t sorts by time (newest first), and -r reverses the order, so the oldest files appear at the top. This is useful for finding the oldest files in a directory.
Why does ls -lt show the same time for files I just edited?
If you edit a file and the modification time does not change, it could be because the filesystem's timestamp resolution is coarse (e.g., 1 second on some older filesystems) or because the file was edited within the same second as the previous edit. Modern filesystems like ext4 have nanosecond resolution, so this is rare. Also, if you are looking at a symbolic link, ls shows the link's timestamp, not the target's, unless you use -L.
How do I see the full date and time instead of “Jun 12 14:33”?
On Linux, use ls -l --full-time to see the complete timestamp with seconds and timezone. On macOS, use ls -lT. You can also customize the format with --time-style=+%Y-%m-%d %H:%M:%S on Linux.
What is the difference between mtime, ctime, and atime?
mtime is the modification time, when the file's content was last changed. ctime is the change time, when the file's metadata (like permissions or ownership) was last changed. atime is the access time, when the file was last read. ls -lt uses mtime by default. ctime cannot be set manually and is used for backup and synchronization tools.
How do I sort by modification time recursively?
ls -Rt sorts each directory separately. For a global sort, use find . -type f -printf '%T@ %p
' | sort -n. This prints the epoch time (a number) and the filename, then sorts by that number. To see the time in a human-readable format, you can use find . -type f -printf '%TY-%Tm-%Td %TH:%TM %p
' | sort.
Why does ls -lt show directories with old dates even though I just changed files inside?
Directories have their own modification time, which changes only when files are created, deleted, or renamed inside them. Changing the content of an existing file does not update the directory's mtime. This is normal and often surprises users who expect the directory to reflect its contents.