Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

3 Answers

up vote 20 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
1  
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
add comment

Parsing the output of ls is rarely a good idea, but obtaining the first few fields is an exception, it actually works.

ls -ld /path/to/directory | awk '{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."
fi
if [ -n "$(find . -user "$(id -u)" -print -prune -o -prune)" ]; then
  echo "The current directory is owned by the current user."
fi
share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.