Sort Files By Last Modification 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 of content as instructed in article Sort Files By Date in Linux.

However, last modification time of content may be different than last modification time in general. For example, if file ownership or name changes, it does not affect to the content modification time. To sort by last modification time, you must use c option as follows:

Sort Files By Modification Time (Descending Order)

$ ls -ltc
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 time.

Sort Files By Modification Time (Ascending Order)

$ ls -ltcr
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.