Find the owner of a file in unix

Use the stat command, if available on your version of UNIX:

    $ stat -c "%U %G" /etc/passwd
    root root

or, to do this operation for all files in a directory and print the name of each file too:

    $ stat -c "%n %U %G" *

ls -l | cut -f3,4 -d" " | tail -n +2

GNU find has the -printf option which will do this for you:

# if you want just the files in the directory, no recursion
find "$dir" -maxdepth 1 -type f -printf "%u %g\n"

# if you want all the files from here down 
find "$dir" -type f -printf "%u %g\n"

# if you need the filename as well for disambiguation, stick a %f in there
find "$dir" -maxdepth 1 -type f -printf "%u %g %f\n"

Other systems might have this as gfind.


ls -l | awk '{print $3, $4 }'

That'll do it

Tags:

Unix