How to display directory contents sorted by modification time
Use the ls command with -t option to sort files by its modification time. The newest files or directories will be shown at the top. To show the a sorted list with the oldest first, use ls -lrt
. This reverses the output, starting with the oldest files and ending with the most recently changed ones.
Changing ls output when sorting
Linux uses the GNU version of ls with additional options to sort and display directory contents.i
Reverse sorting
To see the oldest files first, use the -r option.
ls -lrt
Format of modification time (mtime)
The ls command has different ways of showing the modification time, depending on the age of the file. When sorting files it may be easier to see normalize this date and time, by using the ISO 8601 format.
# ls -lt --time-style=long-iso
total 117
-rwxrwxr-x 1 michael michael 32 2024-07-08 09:47 file1.yaml
-rwxrwxr-x 1 michael michael 43 2024-06-27 17:42 file2.yaml
-rwxrwxr-x 1 michael michael 41 2024-06-27 17:40 file3.yaml
-rwxrwxr-x 1 michael michael 46 2024-06-27 17:40 file4.yaml
-rwxrwxr-x 1 michael michael 27 2024-06-21 13:00 file5.yaml
-rwxrwxr-x 1 michael michael 62 2024-06-14 18:07 file6.yaml
<snip>
To see even more detailed output in the time field, use ‘full-iso’ as the time style.
# ls -lt --time-style=full-iso
total 117
-rwxrwxr-x 1 michael michael 32 2024-07-08 09:47:18.374452164 +0000 file1.yaml
-rwxrwxr-x 1 michael michael 43 2024-06-27 17:42:09.686911617 +0000 file2.yaml
-rwxrwxr-x 1 michael michael 41 2024-06-27 17:40:28.984909993 +0000 file3.yaml
-rwxrwxr-x 1 michael michael 46 2024-06-27 17:40:15.844909781 +0000 file4.yaml
-rwxrwxr-x 1 michael michael 27 2024-06-21 13:00:00.348638620 +0000 file5.yaml
-rwxrwxr-x 1 michael michael 62 2024-06-14 18:07:11.121935846 +0000 file6.yaml
<snip>
This output itself can also be reversed as before.