Ubuntu/Debian — Sort “ls” by Last Changed Date

In Linux, you can display a list of files and directories using the ls command. By default, ls prints the contents of a directory (files and folders) alphabetically in a column-like layout:

$ ls
CHANGELOG.md      README.md         lerna-debug.log   node_modules      package.json      test  
LICENSE           jest.config.js    lerna.json        package-lock.json packages          tsconfig.json  

The listed files are the content of the Supercharge framework folder.

Ubuntu/Debian Series Overview

Sort Files in ls by Date

You can change the alphabetical sorting of ls to sort the output by modified time using the -t parameter. The -t parameter stands for “time modified” and prints the recently changed files first.

Sort by Latest Changes at the Top

This is the default behavior of ls -t: sorting the latest changes to the top of the list:

# sort files ascending by last modified date
# -> latest changes at the top of the list
ls -t  

This is usable for folders with a small number of files. In combination with the -lh parameters, you’ll retrieve a human-readable list:

$ ls -lht

total 672  
-rw-r--r--    1 marcus  staff   958B  8 Dez 16:34 package.json
drwxr-xr-x   15 marcus  staff   480B  8 Dez 16:31 packages  
-rw-r--r--    1 marcus  staff   360B 16 Nov 15:57 tsconfig.json
-rw-r--r--    1 marcus  staff   613B  6 Nov 10:21 lerna-debug.log
-rw-r--r--    1 marcus  staff   302K  6 Okt 17:46 package-lock.json
drwxr-xr-x  452 marcus  staff    14K  6 Okt 17:46 node_modules  
-rw-r--r--    1 marcus  staff   543B  5 Sep 14:53 jest.config.js
drwxr-xr-x    6 marcus  staff   192B 26 Aug 10:02 test  
-rw-r--r--    1 marcus  staff    69B 28 Mai  2020 lerna.json
-rw-r--r--    1 marcus  staff   1,1K 28 Mai  2020 LICENSE
-rw-r--r--    1 marcus  staff   2,8K 28 Mai  2020 README.md
-rw-r--r--    1 marcus  staff   1,1K 28 Mai  2020 CHANGELOG.md

The package.json was modified last in this folder and is listed at the top.

Sort by Latest Changes at the Bottom

Displaying recently changed files at the bottom is helpful for directories with a large number of files. The terminal will then show you the latest changes at the bottom.

# sort files descending by last modified date 
# -> latest changes at the bottom of the list
ls -rt  

Here’s an example of listing the latest changes at the bottom of the list:

$ ls -lhrt

total 672  
-rw-r--r--    1 marcus  staff   1,1K 28 Mai  2020 CHANGELOG.md
-rw-r--r--    1 marcus  staff   2,8K 28 Mai  2020 README.md
-rw-r--r--    1 marcus  staff   1,1K 28 Mai  2020 LICENSE
-rw-r--r--    1 marcus  staff    69B 28 Mai  2020 lerna.json
drwxr-xr-x    6 marcus  staff   192B 26 Aug 10:02 test  
-rw-r--r--    1 marcus  staff   543B  5 Sep 14:53 jest.config.js
drwxr-xr-x  452 marcus  staff    14K  6 Okt 17:46 node_modules  
-rw-r--r--    1 marcus  staff   302K  6 Okt 17:46 package-lock.json
-rw-r--r--    1 marcus  staff   613B  6 Nov 10:21 lerna-debug.log
-rw-r--r--    1 marcus  staff   360B 16 Nov 15:57 tsconfig.json
drwxr-xr-x   15 marcus  staff   480B  8 Dez 16:31 packages  
-rw-r--r--    1 marcus  staff   958B  8 Dez 16:34 package.json

The package.json was modified last in this folder and is listed at the bottom.

Explore the Library

Find interesting tutorials and solutions for your problems.