LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-26-2006, 10:59 AM   #1
DHoody
LQ Newbie
 
Registered: Jan 2006
Posts: 2

Rep: Reputation: 0
s there a way to determine the owner of a file by using a script?


Example:

-rw-rw---- 1 xxx01 CGroup 20992 Nov 3 15:42 File001.doc
-rw-rw-r-- 1 xxx02 CGroup 19456 Aug 19 2002 File002.doc
-rw-rw---- 1 xxx03 CGroup 18432 Jul 13 2005 File003.xls
12345678901234567

I will know the file name, such as File002.doc. What I need to get is the owner xxx02. I could use cut to (ls -l testp | cut -b 17-21) in this example, but that would assume a 5 character owner name. Maybe there is another command that will get the file's owner? Any other ideas on determining the owner of a file by using a script?
 
Old 01-26-2006, 11:40 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by DHoody
I will know the file name, such as File002.doc. What I need to get is the owner xxx02. I could use cut to (ls -l testp | cut -b 17-21) in this example, but that would assume a 5 character owner name.
This could be solved by cutting by field number (option: -f4). For this, you need to specify spaces as seperator (option: -d' '). Example:
Code:
ls -l File002.doc | cut -d' ' -f4
But because "cut" sees each space as a seperator, it may not be very reliable. A solution using "sed", "awk", "python", "perl" would better, but slower.

Another way by setting shell arguments:
Code:
set -- $(ls -l File002.doc)
# Now $1, $2, $3,.. are set to the different fields in
# the output of "ls":
echo "Owner: $3"
echo "Group: $4"
echo "Size:  $5"
But probably the most reliable, shortest and fastest way is to use "stat" instead of "ls":
Code:
stat -c%U File002.doc
 
Old 01-26-2006, 11:41 AM   #3
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 281Reputation: 281Reputation: 281
Try using

Code:
ls -l filename | awk '{ print $3 }'
 
Old 01-26-2006, 12:09 PM   #4
DHoody
LQ Newbie
 
Registered: Jan 2006
Posts: 2

Original Poster
Rep: Reputation: 0
Wow...Not just one but, 3 great ways to get the job done. Thanks Hko and pljvaldez. stat will work best for my current script, but I see uses for the other options too. Excellent.
 
Old 01-27-2006, 10:01 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
make that four

Code:
billym.>set `ls -l ~/.profile`
billym.>echo $3,$4
billym,prima
 
  


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
How to determine who the process' owner* is? balteo Linux - General 3 04-20-2013 05:40 AM
File owner and File group locked - root deiphage Linux - Hardware 5 02-05-2005 01:30 PM
Getting the owner name of a file in C akin81 Programming 5 04-02-2004 03:36 PM
file owner on an NFS shishir Linux - Networking 4 11-30-2003 11:09 PM
file permissions and file owner won't change Nadim Slackware 5 11-29-2003 06:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:34 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