31

I want to make ls display time in ISO format (military format). How can I change this behavior?

On Linux I knew that I could force ls to display time in long format by usin --full-time but this doesn't work on OS X.

Update: I know that the format used by the command is based on the locale settings. The problem is that on OS X I was not able to use the trick of setting LC_TIME=en_DK.

3
  • 1
    A non-answer, but still possibly useful: The stat(1) command is quite flexible and can be made to display time stamps in any desired format. Commented Jun 2, 2011 at 14:45
  • gls (gnu ls) I mentioned in my answer can take a --style= flag where you can specify a +FORMAT string, so you can make the date appear whatever way you wish.
    – barryj
    Commented Jun 3, 2011 at 8:24
  • I wish I knew a way. Apple's BSD manual for ls seems to offer nothing for formatting datetimes, other than changing timezone and sorting by datetime rather than alpha. developer.apple.com/library/mac/documentation/Darwin/Reference/…
    – 2540625
    Commented Aug 12, 2014 at 5:24

5 Answers 5

20

If you install gnu coreutils then gnu ls is available, which will do as required. If you use brew as a package manager, it's as simple as:

brew install coreutils
gls -l --time-style=full-iso
5
  • Thanks, this is useful but does not solve the problem. The full format used is still not ISO-8601.
    – sorin
    Commented Jun 2, 2011 at 14:00
  • OK - you can install gnu coreutils which includes gnu ls, which will do what you want. Easiest way is one of brew, MacPorts or Fink. I use brew - so just running 'brew install coreutils' works. You'll then have gls available.
    – barryj
    Commented Jun 2, 2011 at 14:17
  • 2
    Notably, brew install coreutils results in /usr/local/bin/gls being installed, so you need to run gls instead of ls. Commented Aug 16, 2016 at 0:52
  • You need to specify the time format with gls -l --time-syle="+%I" (or whatever the correct gdate-style format is for ISO-8601). Commented Sep 29, 2017 at 14:04
  • Correct call is thus; gls -l --time-style=full-iso Commented Jul 4, 2018 at 13:32
19

OS X's built-in ls command does not take time formatting arguments, but the stat command takes strftime format strings so you can get an approximation of what you want by doing:

stat -l -t '%FT%T' *

The %FT%T produces an ISO8601 local timestamp. Add a %z if you want a UTC offset.

But while the timestamp is right, the rest only approximates what you'd get from ls. For instance, ls -l properly aligns fields into columns, can colorize output, and of course it lists directory contents rather than requiring you to pass all filenames as arguments. You can at least reproduce the proper alignment by piping the output through tr to convert all spaces into tabs:

stat -l -t '%F%T' * | tr ' ' \\t

Alternatively, I think it should be possible to get ls -l to produce an ISO8601 timestamp by defining a custom locale, but I have not seen it done.

2

Using macOS Monterey 12 the following works for me:

ls -lD '%FT%T'

I'm using an alias that also preserves colored output:

alias ls="ls -D '%FT%T' --color=auto"
1
stat -l -t '%FT%T' *

That worked fine for me. I needed to see both file size and time stamp. Wonder why Apple saw fit to discard the pretty-much-standard time mod o/p from ls -l. I even hauled out my UNIX in a Nutshell!

-2

ls -l --time-style=iso works fine for me.

5
  • 1
    Doesn't. Mountain Lion.
    – Nakilon
    Commented Sep 17, 2013 at 22:40
  • 2
    Not on Mavericks either. Commented Dec 10, 2013 at 23:48
  • 5
    It probably works fine for the OP, because he or she has installed a non-system ls without realizing it, via a package manager like brew.
    – algal
    Commented Mar 8, 2014 at 17:49
  • 1
    Agree with @algal. I have /usr/local/bin/gls installed via brew install coreutils, and that supports the --time-style option. The OP may have run brew install coreutils --with-default-names which would result in /usr/local/bin/ls being installed (amongst other utilities), or maybe the OP set up a manual symlink to gls. Commented Aug 16, 2016 at 0:49
  • What do you see when you enter which ls?
    – mschaef
    Commented Sep 2, 2016 at 11:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .