Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
Verify file ownership in unix shell script

Posted by MarkTChristy (MarkTChristy), 16 September 2005
Does anybody have a quick and dirty command that would verify the ownership of a file on Unix? I need to have a script that would be able to compare the owner of a file to a given owner. I have done it with a small script that long lists the filename, pipes it into 'grep' and checks the exit status of the command. It uses ls -al, tr, and grep. I am wondering if anyone knows a cryptic or different way to actually find this out.

- mark

Posted by admin (Graham Ellis), 16 September 2005
Code:
perl -e 'print ((stat("radio.php4"))[4])'


will print out the uid of the file owner.   Any better, or just substituting one odd set of commands for another odd command?

Posted by MarkTChristy (MarkTChristy), 16 September 2005
That is a good one. The need is actually from another language (an Oracle DB based language). The 'other' language can spin off CLI commands, and then inquire as to the exit status. I did it like this...

ls -l <FILENAME> | tr "    " ":" | cut -d: -f5 | grep <USERID>

If the command is succesful (or exit status == 0), then the file <FILENAME> is owned by <USERID>. The 'tr' and 'cut' commands were hang-overs for when I actually wanted to 'see' the owner only.

ls -l <FILENAME> | grep <USERID> would suffice also...

I was thinking that there may be a standard single unix command that would do this, but I can't find one.

-Mark

Posted by admin (Graham Ellis), 17 September 2005
Mark, your latter solution worries me slightly.   If you have users called - say sam and samantha and a file called sample.txt .... then any file owned by samantha and the sample.txt file, whoever the owner is, will be reported as being sam's.   This may NOT be a problem if all your user names are (say) employee codes of a very particular format whihc would never occur as part of a file name ....

Posted by Custard (Custard), 17 September 2005
The <filename> in ls would catch the sample.txt. sam and samantha could still be a problem. Perhaps investigate egrep and anchor the start and end of the username.

But if you know that perl is installed, the perl solution (whilst not the only one) is only running 1 process instead of 4 which ought to be more efficient.

If not, how about using find...

Code:
find <filename> -user <username>


Returns
'<filename>' if it matched or..

'cannot find <username> name'  if it can't

these probably translate into return codes, I didn't check..

B



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho