LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-31-2007, 12:59 PM   #1
frayja
LQ Newbie
 
Registered: Sep 2005
Distribution: Slackware -current
Posts: 3

Rep: Reputation: 0
Get the owner of a directory


Hello

I am writing a script that needs to know who the owner is of a file or directory.

Code:
#!/bin/bash
for D in /home/*; do
   OWNER=`gettheownersomethow $D`
   echo "The owner of $D is $OWNER"
done
The problem is, of course, the command to get the file or directory owner (in the code example `gettheownersomehow`).

Is there a command or trick that I could use to get this?

Regards,
Frayja
 
Old 08-31-2007, 01:13 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
ls -l | awk '{print $3}'
Prints the owner of every file or directory in the current directory.

The AWK syntax is simply "print the 3rd field in every line of data"

To get the owner of only one file (filename):
Code:
ls -l | grep filename | awk '{print $3}'

Last edited by pixellany; 08-31-2007 at 01:18 PM.
 
Old 08-31-2007, 01:37 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
If you want everything at once then try 'find' with "-printf "%U %f\n" ". If you want per file specs then use 'stat' ('stat -c %u'). For both 'find' and 'stat' goes you only need one binary, don't need to pipe output and munge it and whatever you want to print (numerical ID's, resolved names etc, etc) is configurable.
 
Old 08-31-2007, 01:49 PM   #4
frayja
LQ Newbie
 
Registered: Sep 2005
Distribution: Slackware -current
Posts: 3

Original Poster
Rep: Reputation: 0
Great! Thanks for the fast reply, this solves my problem completely!

Now... back to my little script
 
  


Reply

Tags
bash, linux, owner, ownership, shell



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
can't change owner of directory miner49er Linux - Networking 1 05-04-2006 03:58 PM
Owner of file in shared directory changing Gunbunny Linux - General 4 01-13-2006 07:00 PM
Changing owner of a directory recursively? fturcic Linux - General 2 02-01-2005 07:13 AM
Protecting a directory with chmod, owner, groups, others clarence1720 Linux - Newbie 12 11-06-2004 01:33 AM
How to change owner and group in a directory to include subdir and all files Lakota Linux - General 2 07-15-2004 09:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:19 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration