Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


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

Rep: Reputation: 0
Get the owner of a directory


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

for D in /home/*; do
   OWNER=`gettheownersomethow $D`
   echo "The owner of $D is $OWNER"
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?

Old 08-31-2007, 01:13 PM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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):
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
Registered: May 2001
Posts: 29,353
Blog Entries: 55

Rep: Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541
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
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


bash, linux, owner, ownership, shell

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
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

All times are GMT -5. The time now is 07:16 PM.

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