In the article we will look at different ways of sorting results of the ls (– list directory content) command.
In all example, use the -l flag in addition to the sort flag to retrieve a long description of files and verify your results.
Sort by name
This is the default:
Mac-Book-Pro:_posts username$ ls -l -rw-r--r-- 1 klugjo staff 2290 Dec 6 09:08 Add-Disqus-comments-in-Hexo.md -rw-r--r-- 1 klugjo staff 2066 Dec 6 09:09 Add-Google-Analytics-to-your-hexo-blog.md -rw-r--r-- 1 klugjo staff 952 Dec 9 21:08 Add-an-RSS-feed-to-your-hexo-blog.md -rw-r--r-- 1 klugjo staff 4597 Dec 11 13:32 Build-and-package-your-client-side-code-using-npm.md [...]
Sort by file size
To sort by file size, use
-S Sort files by size
Example
Mac-Book-Pro:_posts username$ ls -Sl -rw-r--r-- 1 klugjo staff 8337 Dec 23 16:41 Build-your-first-React-application.md -rw-r--r-- 1 klugjo staff 4597 Dec 11 13:32 Build-and-package-your-client-side-code-using-npm.md -rw-r--r-- 1 klugjo staff 4506 Dec 6 09:14 Getting-started-with-Gulp-and-static-websites.md -rw-r--r-- 1 klugjo staff 4317 Dec 6 09:14 Refresh-webpages-automatically-during-development-using-Gulp.md [...]
Sort by modification date
To sort by modification date, use
-t Sort by time modified (most recently modified first) before sorting the operands by lexicographical order.
Example
Mac-Book-Pro:_posts username$ ls -tl -rw-r--r-- 1 klugjo staff 1634 Jan 4 17:55 Sorting-files-using-the-ls-command.md -rw-r--r-- 1 klugjo staff 2519 Dec 31 18:06 Code-snippets-in-Webstorm.md -rw-r--r-- 1 klugjo staff 4083 Dec 29 12:32 Loops-and-callbacks-in-React.md -rw-r--r-- 1 klugjo staff 3957 Dec 28 17:23 Easy-dynamic-classes-with-React.md -rw-r--r-- 1 klugjo staff 1418 Dec 24 10:13 Passing-arguments-to-the-underlying-command-in-npm-scripts.md [...]
Sort by last time of access
To sort by last time of access, use -u combined with the -t flag (to sort by date):
-u Use time of last access, instead of last modification of the file for sorting (-t) or long printing (-l).
Example
Mac-Book-Pro:_posts username$ ls -tlu -rw-r--r-- 1 klugjo staff 2188 Jan 4 17:58 Sorting-files-using-the-ls-command.md -rw-r--r-- 1 klugjo staff 2290 Dec 31 18:07 Add-Disqus-comments-in-Hexo.md -rw-r--r-- 1 klugjo staff 2066 Dec 31 18:07 Add-Google-Analytics-to-your-hexo-blog.md -rw-r--r-- 1 klugjo staff 952 Dec 31 18:07 Add-an-RSS-feed-to-your-hexo-blog.md [...]
Sort by creation date
To sort by creation date, use -U combined with the -t flag (to sort by date):
-U Use time of file creation, instead of last modification for sorting (-t) or long output (-l).
Example
Mac-Book-Pro:_posts username$ ls -tlU -rw-r--r-- 1 klugjo staff 3032 Jan 4 18:04 Sorting-files-using-the-ls-command.md -rw-r--r-- 1 klugjo staff 2519 Dec 31 18:06 Code-snippets-in-Webstorm.md -rw-r--r-- 1 klugjo staff 4083 Dec 29 12:32 Loops-and-callbacks-in-React.md -rw-r--r-- 1 klugjo staff 3957 Dec 28 17:23 Easy-dynamic-classes-with-React.md [...]
Reverse order
To reverse the order of any of the sort above, add the -r flag:
-r Reverse the order of the sort to get reverse lexicographical order or the oldest entries first (or largest files last, if combined with sort by size
Example (sort by creation date starting with the oldest first)
Mac-Book-Pro:_posts username$ ls -tlrU -rw-r--r-- 1 klugjo staff 2290 Dec 6 09:08 Add-Disqus-comments-in-Hexo.md -rw-r--r-- 1 klugjo staff 2066 Dec 6 09:09 Add-Google-Analytics-to-your-hexo-blog.md -rw-r--r-- 1 klugjo staff 1428 Dec 6 09:13 round-images-in-css.md -rw-r--r-- 1 klugjo staff 4317 Dec 6 09:14 Refresh-webpages-automatically-during-development-using-Gulp.md [...]
My name is Jonathan Klughertz and this is my blog. I am a full stack software engineer with a strong front-end focus. I currently live and work in Singapore, hit me up if you are in the region.