Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How can i get the path to the given users desktop?

I used this:

echo $(awk "NR==$(awk 'BEGIN {FS=":"} {print $3}' /etc/passwd | awk "/^$(id -u USERSNAMEHERE)$/ {print NR}")" /etc/passwd | awk 'BEGIN {FS=":"} {print $6}')/Desktop

are there any better ones?

What if the users Desktop's name isn't "Desktop"?

Thank you!

p.s.: using Fedora 14 but are there any "general" methods to find out the given users desktop?

share|improve this question
add comment

3 Answers 3

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}"
share|improve this answer
add comment

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
share|improve this answer
1  
What if the users Desktop's name isn't "Desktop"? –  quanta Oct 14 '11 at 0:40
add comment

You're making it way more complex than it has to be. ~UserName is an alias for UserName's home directory.

Use ~UserName/Desktop.

share|improve this answer
    
They specifically asked for a general and correct way, which also handles the case where the directory is not called "Desktop". –  Mikel Jan 19 '11 at 14:08
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.