Home > Enterprise >  bash/shell - How do I get the group name of a directory on RHEL and save it to a variable?
bash/shell - How do I get the group name of a directory on RHEL and save it to a variable?

Time:10-12

I'm working on a script to create users and grant them access to a directory, but they have to be added to the group that has permissions on the directory. I noticed that if I do a stat on the directory I can list out the group name:

[root@pizzaServer myuser]# stat /ftp/PizzaFolder
  File: ‘/ftp/PizzaFolder’
  Size: 22              Blocks: 0          IO Block: 4096   directory
Device: fd03h/64771d    Inode: 2108605     Links: 3
Access: (0750/drwxr-x---)  Uid: (    0/    root)   **Gid: (1934800276/PizzaGroup)**

And then I can grep it:

[root@pizzaServer myuser]# stat /ftp/PizzaFolder | grep Gid
Access: (0750/drwxr-x---)  Uid: (    0/    root)   Gid: (1934800276/PizzaGroup)

And then possibly use sed to crop out the group name? Is that the best method or is there another way? I only want the group name "PizzaGroup" as an output.

CodePudding user response:

Get group name of owner:

stat -c "%G" /ftp/PizzaFolder

See: man 1 stat

  • Related