LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-09-2007, 12:12 PM   #1
IHateFriction
LQ Newbie
 
Registered: Mar 2007
Posts: 2

Rep: Reputation: 0
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!
 
Old 03-09-2007, 12:22 PM   #2
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,309

Rep: Reputation: 139Reputation: 139
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>.
 
Old 03-09-2007, 12:30 PM   #3
IHateFriction
LQ Newbie
 
Registered: Mar 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you thank you thank you thank you thank you, Mr. Trout.
 
Old 03-09-2007, 12:31 PM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
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 )

Last edited by cfaj; 03-09-2007 at 12:34 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
nohup command in script doesn't return to system console prompt beammeup Linux - Software 4 02-05-2007 02:01 PM
bash, starting a command in script activeq Programming 9 07-27-2006 08:42 AM
bash script Want to capture return key and assign a value procfs Programming 9 07-07-2006 01:38 AM
bash command that lists all members of a group by their real names quadmore Linux - Newbie 4 11-07-2005 10:46 AM
Bash Script; command not found twintornado Programming 2 06-01-2005 09:59 AM


All times are GMT -5. The time now is 03:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration