Home > Software design >  How to generate a NULL-delimited stream of timestamped filenames with BSD `stat` command
How to generate a NULL-delimited stream of timestamped filenames with BSD `stat` command

Time:09-14

Let's suppose that you need to generate a NULL-delimited stream of timestamped filenames.

On Linux & Solaris I can do it with:

stat --printf '%.9Y %n\0' -- *

On BSD, I can get the same info, albeit delimited by newlines, with:

stat -f '%.9Fm %N' -- *

The man talks about a few escape sequences but the NULL byte doesn't seem supported:

If the % is immediately followed by one of n, t, %, or @, then a newline character, a tab character, a percent character, or the current file number is printed.

Is there a way to work around that?

CodePudding user response:

Unfortunately, stat out of the box does not offer this option, and so what you ask is not directly achievable.

However, you can easily implement the required functionality in a scripting language like Perl or Python.

#!/usr/bin/env python3

from pathlib import Path
from sys import argv

for arg in argv[1:]:
    print(
        Path(arg).stat().st_mtime,
        arg, end="\0")

Demo: https://ideone.com/vXiSPY

The demo exhibits a small discrepancy in the mtime which does not seem to be a rounding error, but the result could be different on MacOS (the demo platform is Debian Linux, apparently). If you want to force the result to a particular number of decimal places, Python has formatting facilities similar to those of stat and printf.

CodePudding user response:

Is there a way to work around that?

If all you need is to just replace all newlines with NULLs, then following tr should suffice

stat -f '%.9Fm %N' * | tr '\n' '\000'

Explanation: 000 is NULL expressed as octal value.

  • Related