Home / Showing seconds with ls on Linux and OSX

Showing seconds with ls on Linux and OSX

Using the “ls” command to list directory contents from the command line with the -l flag will include the date and time down to the minute, but not show the seconds. This post shows how to display the seconds as well with bash on Linux and OSX.

On Linux

Use one of the following, the second one effectively being a shorter version / synonym of the first one:

ls -l --time-style=full-iso
ls --full-time

The output of either of the above will be this:

total 0
-rw-r--r-- 1 chris chris 0 2016-03-31 12:01:10.000000000 +1300 foo.txt

If you don’t want the less than a second and timezone offset part, you can format the output yourself, e.g. like this:

ls -l --time-style=+"%Y-%m-%d %H:%M:%S"

The output of this is:

total 0
-rw-r--r-- 1 chris chris 0 2016-03-31 12:01:10 foo.txt

On OSX

None of the above will work on OSX, but there’s a flag available on OSX which isn’t on Linux which makes it all a lot easier: -T which you need to use with -l, for example:

ls -lT

And example output:

total 0
-rw-r--r--  1 chris  staff  0 31 Mar 12:01:10 2016 foo.txt

Versions used

The above examples were using:

Bash 4.2.37 on Debian 7.9 Wheezy

Bash 3.2.57 on OSX 10.10.5 Yosemite