Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

I am looking for a command that will return the owner of a directory and only that--such as a regex parsing the ls -lat command or something similar? I want to use the result in another script.

share|improve this question
up vote 51 down vote accepted

stat from GNU coreutils can do this:

stat -c '%U' /path/of/file/or/directory

Unfortunately, there are a number of versions of stat, and there's not a lot of consistency in their syntax. For example, on FreeBSD, it would be

stat -f '%Su' /path/of/file/or/directory

If portability is a concern, you're probably better off using Gilles's suggestion of combining ls and awk. It has to start two processes instead of one, but it has the advantage of using only POSIX-standard functionality:

ls -ld /path/of/file/or/directory | awk '{print $3}'
share|improve this answer
stat -c %U /path, if brevity is a bonus. – tsvallender Feb 20 '11 at 23:18
That assumes GNU stat, which is not the case on older Linux systems (even on newer systems I'd be wary, there might be a different stat (a site-wide standard) in /usr/local/bin or somewhere in the user's home), and is rarely available on other unices. – Gilles Feb 20 '11 at 23:18
stat -c %U has the advantage of also working with BusyBox, if the stat command is compiled in. – Gilles Feb 21 '11 at 0:23
Nice, the last example (ls) works both on Unix/OSX and Linux – kenorb Nov 28 '13 at 15:52
Note that ls will print the uid if there's no local user matching the owner (i.e., on a network share), but stat -c '%U' DIR will print UNKNOWN, which is less helpful or more appropriate, depending on how you look at it. – basic6 May 14 '14 at 8:59

Parsing the output of ls is rarely a good idea, but obtaining the first few fields is an exception, it actually works on all “traditional” unices (it doesn't work on platforms such as some Windows implementations that allow spaces in user names).

ls -ld /path/to/directory | awk 'NR==1 {print $3}'

Another option is to use a stat command, but the problem with stat from the shell is that there are multiple commands with different syntax, so stat in a shell script is unportable (even across Linux installations).

Note that testing whether a given user is the owner is a different proposition.

if [ -n "$(find . -user "$username" -print -prune -o -prune)" ]; then
  echo "The current directory is owned by $username."
if [ -n "$(find . -user "$(id -u)" -print -prune -o -prune)" ]; then
  echo "The current directory is owned by the current user."
share|improve this answer
There are some caveats with the ls | awk approach too unfortunately, as I've noted here. I haven't yet come up with a solution for the, "target file/dir is a symlink with a different name," issue I mentioned in my recent comment. – beporter Jul 15 '14 at 17:16

One can also do this with GNU find:

find $directoryname -maxdepth 0 -printf '%u\n'

This isn't portable outside of the GNU system, but I'd be surprised to find a Linux distribution where it doesn't work.

share|improve this answer
This works on every non-embedded Linux system and a few others (e.g. Cygwin). Embedded systems are likely to have Busybox, whose find doesn't have -printf. – Gilles Feb 21 '11 at 0:22
Like I said, GNU system. – mattdm Feb 21 '11 at 1:24
Systems without GNU coreutils (such as FreeBSD) doesn't have -printf :-(. – pevik Mar 15 at 21:38
@pevik Yeah, like I said, still. But you can install GNU find if you need it. :) – mattdm Mar 15 at 21:44
@mattdm: not always :-(. most importantly when you write scripts which should be portable. – pevik Mar 15 at 22:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.