How does one add milliseconds to the time output of the unix "ls" command in Terminal?

Did some "Googling". Found that %3N should do the trick. But alas, it doesn't work on the Mac. Is there something that will work? Yes, I checked 'man ls' and 'man strftime', but found nothing about milliseconds. Tried on both bash and zsh, but again, it doesn't work. (Actually, it appears it's the same executable, /bin/ls for both, but perhaps some variable could be different.)


Is there some way to do this, either on Monterey or Sequoia? (I am presently on macOS 12.7.6, but still would like to know if something would work for this in Sequoia.)


BONUS TIP: It appears that the comment character for zsh is actually two characters: a colon followed by a space.


TIA!

iMac 24″, macOS 12.6

Posted on Apr 7, 2025 10:31 AM

Reply
12 replies

Apr 7, 2025 02:35 PM in response to VikingOSX

What I meant to post was this Zsh script (milstat.zsh) that outputs milliseconds on the (in this case) creation time column of the requested files. No need for GNU CoreUtils gls utility.


#!/bin/zsh

for f in "${@}";
do
    mil=$(ruby -e "require 'time';puts File.birthtime(ARGF)
        .strftime('%H:%M:%S.%L');" "${f}" | cut -d '.' -f2)
    /usr/bin/stat -l -t "%b %d %T.${mil} %Y" "${f}"
done | column -c9 -t
exit 0


In the Terminal:

cd ~/Desktop
chmod +x ./milstat.zsh
./milstat.zsh *.pages

Output:

Apr 7, 2025 12:46 PM in response to betaneptune

Apple's command line UNIX utilities are not written to show the millisecond granularity of file date/time stamps. Makes no difference which shell is in use. The majority of the shell-related stuff that you Google will inherently be Linux examples using GNU-specific tools. These are different in their implementation than the BSD UNIX tools of the same name on macOS.


The comment character for Bash, Zsh, and ksh93 is the '#' character which is usually found at the beginning of the line[s] it is to comment. A block comment that works in Bash or Zsh is this:


: <<"COMMENT"

something

something else

COMMENT

Apr 7, 2025 12:50 PM in response to betaneptune

I'm not aware of %3N as a standard option. It seems to be GNU-specific, which may be why it doesn't work


In any case, while APFS does use NSDate for file timestamps, there's nothing (that I'm aware of) that exposes that level of detail.

If you're familiar with C, you can write a quick C app to extract the nanosecond level timestamp, but it's not happening within ls


#include <stdio.h>
#include <sys/stat.h>


int main() {
    struct stat attr;
    stat("/path/to/file", &attr);
    printf("Last modified time: %ld", (long)attr.st_mtimespec.tv_nsec);
}


I haven't (yet) tried downloading GNU coreutils to see if GNU ls does what you want. Maybe if I have time later...


Apr 7, 2025 01:07 PM in response to Camelot

Yeah, installing gnutils and running GNU ls (with the appropriate switches) does, indeed, seem to show files with nanosecond-level precision, and the %3N truncates this to three digits:


(base) me@My-MBP bin % /usr/local/bin/ls -l --time-style=+'%b %d %H:%M:%S.%3N'
total 33556
-rwxr-xr-x 1 root    wheel   89312 Apr 07 12:55:37.885 '['
-rwxr-xr-x 1 root    wheel  112824 Apr 07 12:55:37.885  b2sum
-rwxr-xr-x 1 root    wheel   91464 Apr 07 12:55:37.885  base32
-rwxr-xr-x 1 root    wheel   91384 Apr 07 12:55:37.885  base64
-rwxr-xr-x 1 root    wheel   88664 Apr 07 12:55:37.886  basename
-rwxr-xr-x 1 root    wheel  115320 Apr 07 12:55:37.886  basenc
-rwxr-xr-x 1 root    wheel   93008 Apr 07 12:55:37.886  cat
-rwxr-xr-x 1 root    wheel  123848 Apr 07 12:55:37.887  chcon
-rwxr-xr-x 1 root    wheel  142552 Apr 07 12:55:37.887  chgrp
-rwxr-xr-x 1 root    wheel  119784 Apr 07 12:55:37.887  chmod
-rwxr-xr-x 1 root    wheel  142552 Apr 07 12:55:37.887  chown
-rwxr-xr-x 1 root    wheel  116168 Apr 07 12:55:37.883  chroot
...


Installing GNUUtils requires XCode and associated compilers.

Apr 8, 2025 03:12 AM in response to betaneptune

The Zsh script that I wrote, whether on macOS Monterey 12.7.6 or Sequoia v15.4, does not produce the output that you have shown beyond the first 10 columns. Hexdump shows that my script pads the displayed columns with as many spaces ( hex 20) as necessary for columnar alignment.


So, I don't know what you mean by the following as the column command (should be -c10) does the column organizing.


Is there any way to turn the spaces into spaces instead of column filler?

Apr 18, 2025 02:26 AM in response to VikingOSX

The problem is that some of my filenames contain spaces. So instead of


TGR_6 s 23 cc 500 export blue.mov


I would like to see


TGR s 23 cc 500 export blue.mov


IOW, I'd like each of those big gaps in the filenames to be compressed to a single space. That's what I meant. Sorry, I phrased it poorly.


I normally hate spaces in file names (for a number of reasons), but I also hate pressing the shift key several times per filename. (Maybe if I can find a decent keyboard, it would help. The best keyboard I ever had was for a Gateway computer from about 1996.) I often use underscores anyway, if the situation calls for it. [Makes copying filenames in Unix in the Terminal app much easier for copy-and-paste operations (no dragging needed). But that doesn't work if you use hyphens (easy to type) instead of underscores (hard to type). Strangely, a number followed by a period also screws-up the "double-click to select the entire filename" trick. So I'll put a letter in-between them.]


These files are clips I use in Final Cut Pro, and didn't expect to need to manipulate them in this manner.


Also, can this work for the 'find' command? It would be great to have the 'path name' in the output on each line for each file.


Thx! (^_^)

Apr 18, 2025 05:54 AM in response to betaneptune

In the previous script I provided, the ruby and stat commands expect to find a file in the filesystem, not a variable whose name contains single space separators. It is further complicated because that column command organizes its result based on spaces in the stat command's output and a file with spaces in its name would mess up the output appearance substantially. So, unless you are prepared to rename files with spaces in them, it is a no-go.


This is a UNIX operating system and the UNIX command line abhors filenames with spaces — unlike the macOS Finder. Your filenames should be punctuated with a single '_' or '-' and conceivably much shorter than your example.

Apr 18, 2025 06:55 PM in response to VikingOSX

Well, the 'ls' command can handle filenames with spaces in them, as can the 'find' command.


Yes, I know. Spaces are hard to deal with programming-wise (or should I say coding-wise?). They also make copy-and-paste more difficult, as filenames with spaces in them require dragging, and I need to avoid dragging so precisely, as I find it difficult and unpleasant.


These files were clips in Final Cut, and you can rename them as needed in Final Cut. It would have been very painful to be typing the underscores all the time. Hyphens mess up the double-click-to-select a file name, so those are out, too. I do avoid spaces for the names of FCP libraries and .mov files.


Yes, I know it's Unix. I used to be a Jr. systems administrator in Unix, Stratus, and a full-blown admin for OpenVMS years ago. Most people here refer to Unix as "Terminal", so I thought I'd go along with it. In fact, I mentioned "Unix in Terminal" in my OP. Yes, I know Unix (the Darwin kernel) is the engine under macOS.


Filenames containing spaces entered on the command line are indeed a pain. But I'm not asking for that. The 'ls' and 'find' commands don't seem to have a problem with that.


As for using spaces in filenames: Unix allows it, for whatever reason. I think the only illegal character for a filename in Unix is the slash (/), for obvious reasons! Apple coders also use spaces in filenames a lot.


The macOS Finder has its own problems.


Anyway, thanks for the script! As for compressing each column into a single space, I just thought I'd ask in case it were easy to do. Hmmm. Maybe I can write an 'awk' command to do it, or another script!


OK! (^_^)

Apr 19, 2025 03:45 AM in response to VikingOSX

Shorter file names? Apple boasted about 255 (or so) character filenames using almost any characters (except the colon, I think, and a slash would be internally represented by a slash. Or something like that) when Windows was still 8.3, IIRC. OK, yours, in some cases, are a little longer than that.


I need all that info to edit my movies, at least the TGR movie. The movie was made with Super 8 1976-1977 and there were numerous technical problems. I needed file names (they were clip names in Final Cut) that long to keep track of all the work I was doing. There were 22 shots (clips) and I wanted to keep track of all the variations I tried. This was my first FCP project!


I don't want to live in the world of old DOS 8.3 file names. Remember: These clip files are all in the FCP library files that only see in the Finder by running Show Package Contents.


Actually, the vast majority of the files I need this for do not have spaces. I do need those s 23 cc 500 export files, but there are only a few of them. Three, actually. So it's only a problem for those three files. No biggie! (Sorry, I jumped the gun.)


I'm hoping the SSS trick works in Unix, Finder, and Find Any File sessions. But if not, I can at least get milliseconds from 'ls' output with your script, but it won't include the paths. I would need 'find' for that.


I never expected to need this, so I didn't use underscores. But I did use them for filenames of the FCP library files and the .mov files, as I knew I would probably have to move them around.


Anyway, thanks for your help!


OK (^_^)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How does one add milliseconds to the time output of the unix "ls" command in Terminal?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.