Tuesday 23 June 2015

Use "-alt" to instruct the "ls" command to list folder contents by modified date/time in descending order

Oftentimes I'm in "Git Bash" console, Cygwin's full-blown Bash, or suchlike console using the "ls" command with the "-l" option to get output like this for example:

$ ls -l
total 12
drwxr-xr-x    4 Mark New Administ        0 Apr 16 14:18 Atlassian
drwxr-xr-x   50 Mark New Administ    12288 Jun 22 17:32 GphcSvnRepos
drwxr-xr-x   18 Mark New Administ     4096 May 22 14:12 MyGphc
drwxr-xr-x   13 Mark New Administ     4096 Jun  4 17:00 gphc
drwxr-xr-x   18 Mark New Administ     4096 May 15 08:42 nDumbster
drwxr-xr-x    5 Mark New Administ        0 Jun 10 10:21 tmp

...you can include the year "--full-time":

$ ls -alt --full-time
total 18
drwxr-xr-x   50 Mark New Administ    12288 Mon Jun 22 17:32:08 2015 GphcSvnRepos
drwxr-xr-x    1 Mark New Administ     8192 Mon Jun 22 16:11:47 2015 ..
drwxr-xr-x    1 Mark New Administ     4096 Mon Jun 22 16:11:38 2015 .
drwxr-xr-x    5 Mark New Administ        0 Wed Jun 10 10:21:34 2015 tmp
drwxr-xr-x   13 Mark New Administ     4096 Thu Jun 04 17:00:29 2015 gphc
drwxr-xr-x   18 Mark New Administ     4096 Fri May 22 14:12:11 2015 MyGphc
drwxr-xr-x   18 Mark New Administ     4096 Fri May 15 08:42:46 2015 nDumbster
drwxr-xr-x    4 Mark New Administ        0 Thu Apr 16 14:18:45 2015 Atlassian

...but I also want to sort by date in descending order like I often do in Windows Explorer:


Using the "ls" command, use arguments "-alt". This is from ls's help:

$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuSUX nor --sort.

  -a, --all                  do not hide entries starting with .
  -l                         use a long listing format
  -t                         sort by modification time

$ ls -alt
total 18
drwxr-xr-x   50 Mark New Administ    12288 Jun 22 17:32 GphcSvnRepos
drwxr-xr-x    1 Mark New Administ     8192 Jun 22 16:11 ..
drwxr-xr-x    1 Mark New Administ     4096 Jun 22 16:11 .
drwxr-xr-x    5 Mark New Administ        0 Jun 10 10:21 tmp
drwxr-xr-x   13 Mark New Administ     4096 Jun  4 17:00 gphc
drwxr-xr-x   18 Mark New Administ     4096 May 22 14:12 MyGphc
drwxr-xr-x   18 Mark New Administ     4096 May 15 08:42 nDumbster
drwxr-xr-x    4 Mark New Administ        0 Apr 16 14:18 Atlassian

To make it easier and so you don't have to remember the options, create a command shortcut (alias):

Edit (create one if it's not already there) your personal Bash customisation file $HOME/.bash_profile, eg:

$ cat ~/.bash_profile
alias hg='history | grep -i'
alias lst='ls -alt'

As you can see, I've included my most used & time saving alias "hg" to search my recent command history (saved across sessions unlike Windows 'cmd' console).  Remember you can then use "!" to tell Bash to re-run the numbered command from history's output to save re-typing it over and over, eg:

$ hg checkout
  333  git checkout -b crm
  350  git checkout -b crm
  353  git checkout -b crm
  361  git checkout origin/master
  363  git checkout origin
  365  git checkout master
  488  git checkout -b crm
  489  git checkout crm
  493  git checkout master
  565  hg checkout

$ !363
git checkout origin

As you can see Bash expands it to the full command and echoes it to the screen whilst running it.

You can also use 'beginning with abc...' eg:

$ !git check
git checkout origin check

I hope this is useful to someone in the world at some point in ever enduring time!