For most Unix-like systems, like macOS the default sorting order when the ls command is used is alphabetical by file name, i.e. the files are listed in ascending order, from A to Z, and directories are listed first, followed by files.
At times you would like to sort the list by date, especially to know if some files were recently added or modified.
To sort the list by date make use of the -t flag/option.
Example:
% ls -lt
total 16
-rw-r--r-- 1 c2ctech staff 0 May 7 15:56 myfile.txt
drwx------@ 10 c2ctech staff 320 May 7 15:42 Desktop
drwx------+ 120 c2ctech staff 3840 May 6 17:15 Downloads
drwx------@ 93 c2ctech staff 2976 May 6 00:46 Library
drwx------+ 6 c2ctech staff 192 May 5 22:59 Pictures
-rw-r--r--@ 1 c2ctech staff 433 May 3 17:33 gif-gen.py
drwxr-xr-x@ 3 c2ctech staff 96 Apr 25 14:02 Postman
drwx------+ 7 c2ctech staff 224 Apr 22 20:16 Documents
drwxr-xr-x@ 3 c2ctech staff 96 Apr 19 20:09 AndroidStudioProjects
-rw-r--r-- 1 c2ctech staff 29 Apr 18 19:33 vimrc
drwxr-xr-x 5 c2ctech staff 160 Apr 16 10:31 AI
drwx------@ 3 c2ctech staff 96 Apr 13 21:51 Applications
drwxr-xr-x 25 c2ctech staff 800 Apr 5 19:34 anaconda3
drwxr-xr-x@ 5 c2ctech staff 160 Mar 24 14:09 IdeaProjects
drwx------ 4 c2ctech staff 128 Mar 16 13:09 Movies
drwx------ 5 c2ctech staff 160 Mar 16 13:07 iCloud Drive (Archive)
drwx------+ 4 c2ctech staff 128 Mar 16 13:06 Music
drwxr-xr-x+ 4 c2ctech staff 128 Mar 16 13:03 Public
As you may see when I ran the ls -lt command in my current directory which is home (~) gets sorted by modification time, with the most recently modified files and directories first
The next question which is obvious, is how to sort in the reverse order, all you need to do is add -r flag to the -t
% ls -ltr
total 16
drwxr-xr-x+ 4 c2ctech staff 128 Mar 16 13:03 Public
drwx------+ 4 c2ctech staff 128 Mar 16 13:06 Music
drwx------ 5 c2ctech staff 160 Mar 16 13:07 iCloud Drive (Archive)
drwx------ 4 c2ctech staff 128 Mar 16 13:09 Movies
lrwxr-xr-x 1 c2ctech staff 54 Mar 16 13:36 OneDrive - Code2care
drwxr-xr-x@ 5 c2ctech staff 160 Mar 24 14:09 IdeaProjects
drwxr-xr-x 25 c2ctech staff 800 Apr 5 19:34 anaconda3
drwx------@ 3 c2ctech staff 96 Apr 13 21:51 Applications
drwxr-xr-x 5 c2ctech staff 160 Apr 16 10:31 AI
-rw-r--r-- 1 c2ctech staff 29 Apr 18 19:33 vimrc
drwxr-xr-x@ 3 c2ctech staff 96 Apr 19 20:09 AndroidStudioProjects
drwx------+ 7 c2ctech staff 224 Apr 22 20:16 Documents
drwxr-xr-x@ 3 c2ctech staff 96 Apr 25 14:02 Postman
-rw-r--r--@ 1 c2ctech staff 433 May 3 17:33 gif-gen.py
drwx------+ 6 c2ctech staff 192 May 5 22:59 Pictures
drwx------@ 93 c2ctech staff 2976 May 6 00:46 Library
drwx------+ 120 c2ctech staff 3840 May 6 17:15 Downloads
drwx------@ 10 c2ctech staff 320 May 7 15:42 Desktop
-rw-r--r-- 1 c2ctech staff 0 May 7 15:56 myfile.txt
Facing issues? Have Questions? Post them here! I am happy to answer!
- Command to know the Available Memory on Linux System
- How to install curl on Alpine Linux
- How to backup a file in Linux/Unix
- Install Java Runtime Environment (Oracle or open JRE) on Ubuntu
- What is the Default Admin user and Password for Jenkins
- How to tar.gz a directory or folder Command
- Copy entire directory using Terminal Command [Linux, Mac, Bash]
- Fix: bash: ipconfig: command not found on Linux
- Command to check Last Login or Reboot History of Users and TTYs
- Linux: Create a New User and Password and Login Example
- ls command to list only directories
- bash: cls: command not found
- How to exit from nano command
- Installing and using unzip Command to unzip a zip file using Terminal
- What does apt-get update command does?
- ls command: sort files by name alphabetically A-Z or Z-A [Linux/Unix/macOS/Bash]
- How to remove or uninstall Java from Ubuntu using apt-get
- scp: ssh: connect to host xxxx port 22: Connection refused Error
- Sort ls command by last modified date and time
- Create Nested Directories using mkdir Command
- How to Exit a File in Terminal (Bash/Zsh)
- Command to know the installed Debian version?
- How to connect to SSH port other than default 22
- How to save a file in Nano Editor and Exit
- Install OpenSSL on Linux/Ubuntu
- How to check uptime of Linux/Unix/macOS system/server using console command? - Linux
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat - Java
- How to Stream Photos and Videos iPhone to your TV - iOS
- How to Turn off "Terminal" would like to access files message on Mac? - MacOS
- macOS Big Sur compatible Macs List - MacOS
- SharePoint workflow Canceled - Coercion Failed: Unable to transform the input lookup data into the requested type - SharePoint
- Maven : java.lang.ClassNotFoundException: Xmx512m - Android
- Text Case Converter - Convert to Upper/Lower/Proper/Random Case - Tools