shell script - How to output the owner of a file

3

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.

BenjiWiebe

Posted 2012-12-20T20:22:53.323

Reputation: 7 672

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-- – 2012-12-20T20:48:10.743

@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 – 2012-12-20T20:54:40.057

@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) – user1686 – 2012-12-20T22:48:27.073

@grawity Perl is not installed everywhere... but show me a Linux system that does not have stat and bash/sh installed. – BenjiWiebe – 2012-12-20T23:35:58.503

@BenjiWiebe: I can show you a few BSD systems that use completely different options for stat.

– user1686 – 2012-12-20T23:48:56.543

@grawity As long as it works on Fedora. I am... uh... fluent? in bash scripting, and I know very very little Perl. However, if I was distributing this software I would consider learning and using Perl. – BenjiWiebe – 2012-12-20T23:53:36.477

@BenjiWiebe: when you look for a reply applying to a specific OS (eg Fedora), or a range of OSes (eg Gnu/Linuxes), better not to tag it Unix. Unix is a standard and imply standard compliant answers when available. – jlliagre – 2012-12-21T09:28:22.427

Answers

6

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

Gilles Quenot

Posted 2012-12-20T20:22:53.323

Reputation: 3 111

Thanks a lot. I like jilliagre's function, but I wanted a command to do it without parsing the ouput of another command. – BenjiWiebe – 2012-12-20T20:53:09.807

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 – 2012-12-21T01:03:47.057

1

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.

jlliagre

Posted 2012-12-20T20:22:53.323

Reputation: 12 469

2Might work great until you have a username with perhaps a space in it... – a CVn – 2012-12-20T20:33:29.193

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 – 2012-12-20T20:40:23.583

Where in the question does it even specify Linux? I see a unix tag as well... – a CVn – 2012-12-20T21:04:04.683

@MichaelKjörling The title starts out "linux - shell script -...". And then of course, there is the linux tag. – BenjiWiebe – 2012-12-20T22:04:02.903

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. – user1686 – 2012-12-20T22:38:13.270

@grawity OK. I did not know that. – BenjiWiebe – 2012-12-20T22:39:47.290

(I'm pretty sure the only actually invalid character in usernames is :. In a way, this is really similar to filenames, which allow everything except /.) – user1686 – 2012-12-20T22:42:30.800

@grawity: Unix/Linux are quite restrictive and certainly do not allow local usernames with a space (see my updated reply). Windows is quite relaxed with what it accepts but is however more restrictive than allowing anything but a colon. The backslash for example is used to separate a domain from a username so cannot be used in the latter. There are likely several other restrictions. – jlliagre – 2012-12-21T00:09:06.483

@jlliagre: Linux glibc certainly does not filter usernames in getpwnam() or getpwuid() at all. Whether it's That Guy:*:1000:1000::/home/That Guy:/bin/sh in one's /etc/passwd, or an account returned by nss-ldap or nss-sss or nss-winbind, there are no such restrictions. – user1686 – 2012-12-21T00:19:08.790

@grawity, granted you can hack your password/shadow file that way but that username is still invalid and breaking the Unix standard. Linux pwck will complain: # pwck --> invalid user name 'That Guy' . I agree remote usernames can be almost anything but then they might lead to problems. – jlliagre – 2012-12-21T00:36:18.813

@BenjiWiebe Yes, there is a linux tag. There is also a unix tag. Unix is not Linux, and the same rules certainly need not apply to both (let alone specific variants of Unix; what's to say that Mac OS X and Solaris do things the same way?). – a CVn – 2012-12-21T08:55:37.067