Getting the Owner of a File in Linux

Ran across this problem where I needed root to execute a script as whoever owns it. The solution is simple and should work most everywhere. Basically it just uses the standard “stat” program and uses the print formatting option to only output the name of the owner. Here I use bash to get the owner:

#!/bin/bash
username=`stat ${1} --printf %U`
echo "The owner of ${1} is ${username}";
exit 0;

And a sample run looks like:

$ ./whoOwnsIt.sh testfile.txt
The owner of testfile.txt is ben.

Making root run it as the user’s owner is then trivially done with:

su ${username} ${cmd}

This entry was posted on Monday, February 25th, 2008 at 5:40 pm and is filed under Shell Scripting. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Getting the Owner of a File in Linux”

  1. Tyler Says:

    How about some malicious shell scripts :)

Leave a Reply