If you ever need to retrieve the user that owns the file, or the group, you can use the very simple stat
command. But instead of the usual grep/sed/awk dance, you can just set some additional parameters that retrieve only the user or group.
Here’s how that works:
#!/bin/bash USER=$(stat -c '%U' /path/to/your/file)
From now on, the $USER
variable holds the username of the owner of that file. Want the group? Just as easy:
GROUP=$(stat -c '%G' /path/to/your/file)
The stat
commands allows for several output formats, for handling users/groups these are the most important:
%u user ID of owner %U user name of owner %g group ID of owner %G group name of owner
Handy little tool!