Ubuntu – Bash: find directory group owner

bashUbuntu

I am sure this is easy but i cant find it.

I am writing a bash script where i want to add a known user to a group.
The group name is determined by the directory, which is sent through as a variable to that bash script.

so:

DIR="whatever"
GROUP=DIR groupname 
usermod -a -G $GROUP userx

Best Answer

stat -c %g <file> (Linux)

stat -f %g <file> (BSD)

Will return the group ID for the given <file>

EDIT:

stat -c %G <file> (Linux)

stat -f %Sg <file> (BSD)

Will return the group name.