4

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.

user1942541
  • 297
  • 1
  • 3
  • 13

3 Answers3

6

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

anthony
  • 40,424
  • 5
  • 55
  • 128
4

I use the following command:

ls -n my-file.txt

This would output:

-rw-r--r--  1  502  20  578  Dec 3 20:44  my-file.txt

Here, 502 is the user-id (uid), 20 is the group-id (gid), and 578 is the file size in bytes.

Ashwin
  • 7,277
  • 1
  • 48
  • 70
1

For my Mac OS Monterey ( 12.4 ), the above didn't work. I'd to pass in the string formatting option S along with g as below stat -f "%Sg" foo.txt

deCodeIt
  • 37
  • 1
  • 4