Mastering how to sort and organize directory contents by time is an essential skill for any Linux administrator. Instead of digging through an endless, alphabetically ordered file list, you can instantly pinpoint newly updated configuration files, fresh logs, or misplaced archives using powerful Linux commands. Whether you are debugging a broken service or managing automated backups, understanding file timestamps transforms your workflow.
Understanding Linux File Timestamps
Before sorting files by time, you must understand how Linux stores temporal metadata inside an inode. Every file features specific timestamps that dictate how and when it interacts with the system:
- mtime (Modification Time): Updates when file contents change (the default view for
ls -l). - atime (Access Time): Changes when the file is read, though modern systems use
relatimeto optimize disk writes. - ctime (Change Time): Updates when metadata (permissions, ownership, or name) changes, or when contents are edited. (Note: This is not the creation time!).
- btime (Birth Time): Records file creation on supported filesystems like ext4, XFS, and Btrfs.
To inspect all four timestamps simultaneously for a specific file, run the stat command:
stat /etc/hosts
Practical Methods to Sort Files by Time in Linux
Leveraging native command-line flags allows you to quickly sort directory outputs to surface the exact information you need.
Sort Files by Modification Time Using ls -lt
The -t flag sorts files by time (newest first). Combining it with -l provides a detailed breakdown. Piping the output into head limits the results to the most recent changes:
ls -lt /etc | head -n 6
List Newest Files at the Bottom Using ls -ltr
When reviewing sprawling log directories, having the newest entries scroll off the screen is inefficient. Adding the -r flag reverses the sort order, placing the latest files right above your command prompt:
ls -ltr /var/log
Display Exact Timestamps with --full-time
Standard listings hide the time of day for older files and omit seconds entirely. For high-precision administrative tasks, use the --full-time option:
ls -lt --full-time /etc/nginx
Sorting Across Subdirectories with the Find Command
While ls excels within single directories, finding the most recently modified file across an entire directory tree requires a robust Linux terminal approach combining find and sort:
sudo find /var/log -type f -printf '%T+ %p\n' | sort -r | head -n 5





