macosawksedzshls

ls to show only date modified, time modified, and name


I try to remove all the columns from the output of ls -l command except date modified, time modified, and name.

drwxr-xr-x  5 john  staff     160 May 16 01:59 some-folder
-rw-r--r--@ 1 john  staff  759704 Apr 14 04:05 some-file.png

The following command is almost what I want, except that it leaves the second column, with integers 5 and 1.

ls -goh | sed -re 's/^[^ ]* //'

I already tried to fix it myself, here is one of those attempts,

ls -goh | sed -re 's/^[^ ]* *\d* //'

but it seems sed regular expressions are somewhat different from what I always used before.

How do I need to change the regex to show only date modified, time modified, and name columns?

--

See comments, current awk output:

28M Mar 29 12:03 foo.dmg
96B Feb 9 15:33 bar.app
128B May 16 01:59 aaa
160B May 16 01:59 bbb

Would be even better to preserve columns:

 28M Mar 29 12:03 foo.dmg
 96B Feb  9 15:33 bar.app
128B May 16 01:59 aaa
160B May 16 01:59 bbb

Solution

  • See https://mywiki.wooledge.org/ParsingLs for why what you're asking for help to implement (piping ls to any command to parse it's output) is an anti-pattern. You don't need to do that to get the output you want. MacOs has a version of the stat command so you can just do something like (I don't have a Mac to test this on):

    stat -t '%z %m %d $H:%M' -f '%m %N\n' *
    

    Change \n to \0 if your file names can contain newlines and that stat version supports it, otherwise stat each file in a loop.

    If you want columnar output or convert bytes to MB or do anything else you CAN pipe the above output to awk since the output of stat is consistent and well-formatted for parsing, unlike the output of ls.

    You may have GNUs version of stat too which has similar functionality but implemented by different options than above - just check the stat man page on your machine for how to do whatever you want to do.