Search code examples
macosterminalstat

Get group ID (GID) of a file in Mac OS X via a shell command. What is the equivalent of stat -c "%g" <filename> for Mac OS X?


So, I want to quickly get the group ID (GID) of a file using the shell (more accurately, the GID of the owner of the file).

Somewhere in my shell script, I have the following code:

stat -c "%g" foo.txt

In every Linux I have tried, the above works perfectly.

However on Mac OS X, I get: stat: illegal option -- c usage: stat [-FlLnqrsx] [-f format] [-t timefmt] [file ...]

How can I get the GID of a file on MacOS X with a shell command? Preferably something that also works on Linux, but I am okay with introducing an if-else if needed.


Solution

  • The equivalent on OS X is stat -f "%g" foo.txt.