LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash command/script to return group of file (https://www.linuxquestions.org/questions/programming-9/bash-command-script-to-return-group-of-file-536043/)

IHateFriction 03-09-2007 12:12 PM

Bash command/script to return group of file
 
I'm in need of a Bash command or script that will return the group of a file, so that I can store it in a variable.

I'm aware of "ls -g" but that returned:

Code:

-rw-r--r--  1 group  167 Mar  5 11:48 file.txt
A Linux-savvy friend of mine suggested "find file -printf %g" but that command wouldn't work, possibly because I'm using OS X's terminal.

Should I be using Perl, or I am I stuck with some ugly string concatenation?

Thanks for your help!

kilgoretrout 03-09-2007 12:22 PM

If you have awk installed you can do that easily with:

ls -g <filename> | awk '{print $3}'

The above will just print out the third field which is always the group with ls -g <filename>.

IHateFriction 03-09-2007 12:30 PM

Thank you thank you thank you thank you thank you, Mr. Trout.

cfaj 03-09-2007 12:31 PM

Quote:

Originally Posted by IHateFriction
I'm in need of a Bash command or script that will return the group of a file, so that I can store it in a variable.

I'm aware of "ls -g" but that returned:

Code:

-rw-r--r--  1 group  167 Mar  5 11:48 file.txt
A Linux-savvy friend of mine suggested "find file -printf %g" but that command wouldn't work, possibly because I'm using OS X's terminal.

Should I be using Perl, or I am I stuck with some ugly string concatenation?

Use stat:

Code:

group=$( stat -c %g FILE )


All times are GMT -5. The time now is 10:38 AM.