Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a command to output the owner of a file, and nothing else? I suppose I could use ls and run it through sed, but if there is a better way, I would definitely use it.
Thanks in advance.

share|improve this question
    
Do you need that more often? You could make a lttle perl script or even a little c program to return a specific info on a given filename. – ott-- Dec 20 '12 at 20:48
    
@ott I could... but that is less portable(?). I would prefer to use programs already on a computer, than to have to compile my own. – BenjiWiebe Dec 20 '12 at 20:54
    
@BenjiWiebe: Perl tends to be very portable. You could use something like for my $file (@ARGV) {$uid = (stat $file)[4]; $name = (getpwuid $uid)[0]; print "$name\n"} or print map {"$_\n"} map {(getpwuid $_)[0]} map {(stat $_)[4]} @ARGV (whichever looks nicer to you) – grawity Dec 20 '12 at 22:48
    
@grawity Perl is not installed everywhere... but show me a Linux system that does not have stat and bash/sh installed. – BenjiWiebe Dec 20 '12 at 23:35
    
@BenjiWiebe: I can show you a few BSD systems that use completely different options for stat. – grawity Dec 20 '12 at 23:48
up vote 6 down vote accepted
stat -c %U file.txt

ls is a tool for interactively looking at file information. Its output is formatted for humans and will cause bugs in scripts. Use globs or find instead. Understand why: http://mywiki.wooledge.org/ParsingLs

share|improve this answer
    
Thanks a lot. I like jilliagre's function, but I wanted a command to do it without parsing the ouput of another command. – BenjiWiebe Dec 20 '12 at 20:53
    
Sweet, a new terminal command to play with! I never knew about the stat command, but I will now abuse it in every way I can think of. – Llamanerds Dec 21 '12 at 1:03

I would use that function:

lso() { ls -dl ${1:?usage: lso file} | awk '{print $3;exit}'; }

Edit:

  • I thought about stat but I try to avoid using anything non standard when possible. I sticked with something portable (i.e. POSIX) as your question is tagged linux and unix, not just linux with which stat is quite standard..

  • As this question triggered a discussion about valid usernames, these are also defined by a Unix standard to be a string composed exclusively of characters from this list:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

a b c d e f g h i j k l m n o p q r s t u v w x y z

0 1 2 3 4 5 6 7 8 9 . _ -

with the additional restriction for the hyphen not to be the first character.

I assumed no space was allowed. Just like anything else which is non-portable this can lead to unexpected results not only with my small function but with many Unix/Linux CLI utilities.

share|improve this answer
2  
Might work great until you have a username with perhaps a space in it... – Michael Kjörling Dec 20 '12 at 20:33
1  
@MichaelKjörling On Fedora 17: sudo adduser "foo bar" outputs adduser: invalid user name 'foo bar' but it works fine if you try "foobar" (without a space). Therefore, spaces are not valid in usernames. – BenjiWiebe Dec 20 '12 at 20:40
    
Where in the question does it even specify Linux? I see a unix tag as well... – Michael Kjörling Dec 20 '12 at 21:04
    
@MichaelKjörling The title starts out "linux - shell script -...". And then of course, there is the linux tag. – BenjiWiebe Dec 20 '12 at 22:04
1  
@BenjiWiebe: Spaces are valid in usernames, regardless of what adduser says about them. In fact, they're commonly used on Linux systems joined to MS Active Directory domains. – grawity Dec 20 '12 at 22:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .