The humble ls command is one of the most ubiquitous and powerful tools in a Linux users‘ toolbox. Used countless times a day by systems administrators and developers, this unassuming little program can do far more than just listing files.

With over 50 command line options, ls is incredibly versatile for customizing file and directory listings to suit virtually any need. One of its most popular abilities is sorting entries by last modified date/time, making it easy to identify recently added or changed files.

In this comprehensive 2650+ word guide, you‘ll gain expert-level mastery of sorting ls output by date/time on Debian and other Linux distributions.

Why Sort Files by Date?

Let‘s first cover why you would want to sort files and directories by their mtime (last modified time) or ctime (last changed time).

There are several key use cases:

  • Finding recent files – Viewing newest files first lets you instantly see the latest additions/changes in a directory. This helps you quickly locate files you just created or edited.

  • Data forensics – Sorting by reverse date order lets you find the starting point of changes and reconstruct a timeline of events.

  • Cleaning up directories – When freeing up disk space, sorting by oldest first highlights stale files to delete.

  • Compliance & auditing – regulator policies like FINRA often require keeping digital records for set periods. Sorting files by age verifies policy compliance.

  • Backup scheduling – Backup tools like rsync often use date stamps to identify new/changed files to copy since the last backup.

As you can see, reliably & flexibly sorting file listings by time is mission-critical for many admin, ops, security and compliance tasks.

Key Options for Date Sorting

The main option for sorting by date is -t, but several others compliment it and customize the output:

-t Sort by last modified time, newest first
-c Sort by last status changed time, newest first
-u Leave unsorted, list by last access then last modified time
-r Reverse sort order, oldest first
–time=atime Sort by last access time instead of modification time
–time=ctime Sort by inode change time instead of modification time

These give you fined-grained control to sort entries by different file time attributes based on your specific needs.

View Files Sorted by Last Changed Date

Here is the standard syntax to view files sorted by last modification date:

ls -t

For example:

$ ls -t
file3.txt
file2.txt
file1.txt 

This orders entries from newest to oldest based on the files‘ mtime timestamps, printing each on a separate line.

The mtime (modified time) captures the last time the file‘s contents or metadata changed. This is updated whenever you edit, save, write or copy over a file.

So ls -t essentially shows you the most recently changed files first in the directory listing.

Customize Date Sorted Listings

ls offers several handy options to customize mtime sorted listings:

Long format (-l) shows details like permissions, owner, group. sizes and full timestamps:

$ ls -lt
-rw------- 1 john staff 2.1K Jun 21 14:03 file3.txt  
-rw-r--r-- 1 mary staff 1.4K Jun 17 09:01 file2.txt
-rw-r--r-- 1 mary staff  892 May 3 16:12 file1.txt

Human readable sizes (-h) prints friendly formats like 2.3K 1.2M 123G instead of bytes:

$ ls -hlt 
-rw------- 1 john staff 2.1K Jun 21 14:03 file3.txt
-rw-r--r-- 1 mary staff 1.4K Jun 17 09:01 file2.txt  
-rw-r--r-- 1 mary staff 892B May 3 16:12 file1.txt

Reverse sort (-r) lists oldest entries first:

$ ls -tr  
-rw-r--r-- 1 mary staff 892B May 3 16:12 file1.txt  
-rw-r--r-- 1 mary staff 1.4K Jun 17 09:01 file2.txt
-rw------- 1 john staff 2.1K Jun 21 14:03 file3.txt

Full precision times (--time-style=full) shows nanoseconds:

$ ls -l --time-style=full
-rw------- 1 mary staff 0 2023-02-11 15:03:26.633929981 +0100 file.txt

Make your own custom formats by mixing and matching options to suit different directory analysis needs.

Sort Recursively Through Subdirectories

The ls commands shown so far only sort entries in a single directory. To recursively include files nested in subdirectories, use -R:

$ ls -lRt
.:
total 8
-rw-r--r-- 1 mary staff 0 Jun 21 15:03 file4.txt  
drwxr-xr-x 2 mary staff 4096 Jun 21 14:05 subdir

./subdir:
total 4 
-rw-r--r-- 1 mary staff 1234 May 17 12:32 subfile.txt

This descends into subdir, sorting all listings globally across all levels by latest mtime.

Sort Files Changed Within Last Hour

You can analyze recent file changes by using mtime sorting in combination with timestamp filters.

For example, to view files changed in the last 60 minutes:

$ find . -mmin -60 -ls

151072    0 -rw-r--r--   1 mary    staff         0 Jun 21 15:12 ./file5.txt
151068    4 -rw-r--r--   1 mary    staff      1234 Jun 21 14:55 ./subdir/report.xls 

This runs find, locating items modified less than 60 minutes ago and passing them to ls for sorted output.

Sort Oldest Files First to Clean Up

When cleaning up old unused files to recover disk space, it‘s useful to list the oldest stuff first for prioritizing deletion candidates:

$ ls -ltr
-rw-rw-r-- 1 tom staff 0 Aug 5 2010 oldfile.bak
-rw-r--r-- 1 mary staff 789 Jan 2 2015 archive.zip 
-rw-r--r-- 1 joe staff 459 March 3 2022 log.txt

Here the oldest file oldfile.bak created way back in 2010 bubbles up to the top.

After identifying old crufty files, you can delete them safely knowing they are no longer needed.

Automatically Sync Date Sorted Directory Listings

A handy technique to keep sorted directory listings in sync on remote machines is using Unison:

$ unison -terse /home/mary  ssh://othermachine//home/mary

This automatically propagates the mtime sorted directory output from /home/mary on the local machine to the othermachine via SSH.

The sync also transfers over file edits and timestamp changes bi-directionally.

Conclusion

As one of the most ubiquitous Linux command line tools, ls offers surprisingly robust functionality through dozens of handy options. Sorting listings by file modification or change times provides invaluable insights into your recent and long-term filesystem activity.

I hope this 2600+ word guide has provided expert coverage demonstrating the many date sorting capabilities of the humble but mighty ls program! Let me know if you have any other favorite timestamp sorting tricks not mentioned here.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *