How to get given users "Desktop Path"

Solution 1:

Try xdg-user-dirs.

That page has an example that should work for the current user:

test -f ${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs && \
    source ${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs
echo ${XDG_DESKTOP_DIR:-$HOME/Desktop}

Also, if you have to read /etc/passwd, it's better to use the output of getent passwd, because some user data might be stored in NIS, LDAP, or some other database.

Combining that so it works for a user called USERSNAMEHERE:

USER=USERSNAMEHERE
USERDIR=$(getent passwd $USER | cut -f 6 -d :)
USERDIRCONF=${XDG_CONFIG_HOME:-$USERDIR/.config}/user-dirs.dirs
test -f "$USERDIRCONF" && . "$USERDIRCONF"
echo "${XDG_DESKTOP_DIR:-$USERDIR/Desktop}"

Solution 2:

Your command is impossibly complex.

Here is a simplified version:

getent passwd USERNAME | awk -F: '{print $6 "/Desktop"}'

Here is an untangled version of your original:

awk -v "id=$(id -u USERNAME)" -F: '{if ($3 == id) print $6 "/Desktop"}' /etc/passwd

or

awk -v "name=USERNAME" -F: '{if ($1 == name) print $6 "/Desktop"}' /etc/passwd

Solution 3:

The xdg-user-dir utility can help you:

$ xdg-user-dir DESKTOP
/home/user/Desktop

It only works like this if you're logged as the user though. You can fool it by specifying XDG_CONFIG_HOME:

$ XDG_CONFIG_HOME=/home/user xdg-user-dir DESKTOP
/home/user/Desktop

If you look at the script itself, it roughly does the same thing as the snippet in Mikel's answer.