3

Coreutils stat have --format= switch which report different info about file (owner, size, etc) in easy form for readers.

POSIX ls utility provide most of this info, but its output is hard to parse. Compare with one-liner:

[ `stat -c '%U' $f` = $USER ] && echo "You own $f" || echo Error!

Are there stat utility analog in POSIX?

gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • Is there a specific part of what stat can output that you are trying to emulate or are you actually looking for a tool that is actually a full `stat` analog? – Etan Reisner Jan 07 '15 at 21:06
  • 1
    No. It was added by GNU and BSD (different versions) because `ls` is not as tractable. Other people have no doubt written similar code (I know I have). – Jonathan Leffler Jan 07 '15 at 21:07
  • 1
    This is not possible, unfortunately :-( The closest you'll get is use a Perl or Python one-liner and call the `stat` function from those languages... Obviously, this is not something you want to do if performance is an issue... Other than that: `ls` ... – Martin Tournoij Jan 07 '15 at 21:13
  • If you're limited to pure POSIX, it does specify the `c99` command (a C compiler). Since the `stat` *function* is specified by POSIX, you could compile and execute as program that calls it. I'm not clear on the exact conditions in which `c99` is required. – Keith Thompson Jan 07 '15 at 22:28

2 Answers2

6

It is not possible :-(

Your options are:

  • Use ls and parse that with awk; the output of ls -l is in POSIX, so you can rely on that. This works okay for some fields (such as the owner in your example), and not so good for others (such as the mtime).

  • Detect the stat version and switch parameters; GNU stat has -c, BSD stat has -f, other versions perhaps something else. stat is not in POSIX at all, and I don't know how widespread it is beyond Linux, BSD, and OSX, though.

  • Use a Perl or Python one-liner; this is not even remotely POSIX of course, but making the assumption that at least one of these languages is present is a fairly reasonable one in 2015, and it is easily detectable at startup if they are indeed present. It's also not an option if performance is any concern.

    Example, I've used mtime in all these examples, as this is difficult to get with ls:

    #!/bin/sh
    
    file="/etc/passwd"
    
    perl -e "print((stat(\"$file\"))[9])"
    echo
    
    echo "$file" | perl -e '$i = <STDIN>; chomp($i); print((stat($i))[9])'
    echo
    
    python -c "import os; print(os.stat(\"$file\").st_mtime, end='')"
    echo
    
    echo "$file" | python -c "import os, sys; print(os.stat(sys.stdin.readline()[:-1]).st_mtime, end='')"
    echo
    

    I would recommend the Perl version; not because I like Perl, but because this Python example only work correctly with Python 3 (specifically, the end='' bit to prevent printing newlines. A version to work with both Python 2 & 3 gets rather long:

     python2 -c "from __future__ import print_function; import os; print(os.stat('/etc/passwd') .st_mtime, end='')"
    

    You could also expand this with other languages (Ruby, PHP, Tcl, etc.), but Perl & Python are the most widespread by far.

    Documentation for: Perl stat(), Perl lstat() Python os.stat() .

umi
  • 3,782
  • 2
  • 15
  • 12
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • it is sad that POSIX so pure. Even GNU Awk have built-in `stat` (( – gavenkoa Jan 07 '15 at 22:07
  • 2
    @gavenkoa POSIX is very conservative; it's good, because even the definition of "being POSIX compliant" doesn't change that fast, but it's bad because it's missing a lot (such a `stat`)... – Martin Tournoij Jan 07 '15 at 22:17
4

The short answer is no, POSIX does not provide a simple way to get the same output as stat. However, you can usually get relevant bits using other tools. To get the owner specifically:

ls -ld /path/of/file/or/directory | awk '{print $3}'
Community
  • 1
  • 1
l0b0
  • 55,365
  • 30
  • 138
  • 223