Sort Files By Last Access Time in Linux

Last updated on Dec 28, 2019 in Linux

One of the most used Linux commands is ls which lists files in a folder. By default, the sort is done in alphabetical order. By using -t option, you can sort the files by last modification time as instructed in article Sort Files By Date in Linux.

However, last modification time is different than last access time, if the file is not changed. To sort by last access time, you must use u option as follows:

Sort Files By Modification Time (Descending Order)

$ ls -ltu
total 44
-rw-r--r-- 1 theuser apache    15 Dec 28 14:54 file1.php
-rw-r--r-- 1 theuser apache    20 Dec 18 14:57 file2.php
-rwxr-xr-x 1 theuser apache 29615 Dec 16 14:57 booking.php
-rw-r--r-- 1 theuser apache    15 Nov 15 14:57 README.md

Descending order is the default, meaning that the newest file is on top. The l stands for list mode. Without it, only file names are viewed without dates. However, without dates visible, you can still sort by date.

Sort Files By Modification Time (Ascending Order)

$ ls -ltur
total 44
-rw-r--r-- 1 theuser apache    15 Nov 15 14:57 README.md
-rwxr-xr-x 1 theuser apache 29615 Dec 16 14:57 booking.php
-rw-r--r-- 1 theuser apache    20 Dec 18 14:57 file2.php
-rw-r--r-- 1 theuser apache    15 Dec 28 14:54 file1.php

The r stands for reverse and it can be used in general in any listing.