3

I am very new to linux and am going through and learning all the basics. While I was doing so I realized how odd it was that the OS treats certain files differently--namingly by displaying them on the GUI desktop enviroment if they happen to be the desktop file associated with the currently logged in user. How does the OS know the path variable to the current user's desktop directory? For hacker amusment can it be changed such that any arbitrary directory will be displayed on the GUI desktop?

1
  • Typically ~/Desktop
    – ivanivan
    Sep 13 '17 at 2:55
6

The default path for the desktop directory varies between different languages. Hence a better guess than $HOME/Desktop could be fetched from xdg-user-dirs in case that exists in your system:

$ xdg-user-dirs DESKTOP
/home/arnie97/桌面

There's also another way from xdg-user-dirs home page:

Here is a shellscript example of how to find the desktop and the download directory:

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

For application code the hope is that the various desktops will integrate this and have a nice API to find these directories.

1

Generally, in GNU/Linux (as in Unix), the user's Desktop directory can be specified with ~/Desktop. The shorthand ~/ will expand to whatever the home directory is, such as /path/to/home/username.

I should clarify that most shells (e.g. bash, ksh, csh, etc.) will expand ~ to the home directory, but not all of them (e.g. rc). The tilde is a literal character, so you can have a file ~filename; therefore, the shell must be designed to expand the tilde under certain conditions.

1

The path for most distributions (I say most and not all because Linux (or GNU/Linux), is nothing if not mutable, for good or ill), is going to be /home/<user name>/Desktop You can go to it through the command line like this: cd /home/<username>/Desktop Viewing files is a simple as using the command "ls" without the quotation marks.

1

The desktop directory is different according to language of system. Adding to options given for other person, you can find the file ~/.config/users-dirs.dirs

Example you can read the content:

cat ~/.config/users-dirs.dirs

In my machine shows this:

# This file is written by xdg-user-dirs-update
# If you want to change or add directories, just edit the line you're
# interested in. All local changes will be retained on the next run.
# Format is XDG_xxx_DIR="$HOME/yyy", where yyy is a shell-escaped
# homedir-relative path, or XDG_xxx_DIR="/yyy", where /yyy is an
# absolute path. No other format is supported.
# 
XDG_DESKTOP_DIR="$HOME/Escritorio"
XDG_DOWNLOAD_DIR="$HOME/Descargas"
XDG_TEMPLATES_DIR="$HOME/Plantillas"
XDG_PUBLICSHARE_DIR="$HOME/Público"
XDG_DOCUMENTS_DIR="$HOME/Documentos"
XDG_MUSIC_DIR="$HOME/Música"
XDG_PICTURES_DIR="$HOME/Imágenes"
XDG_VIDEOS_DIR="$HOME/Vídeos"

How you can see my desktop directory is: "$HOME/Escritorio" where $HOME is an environment variable that refers to your home folder. In practice:

cd ~/Escritorio 
# is the same 
cd "$HOME/Escritorio"

If you are interested in getting desktop dir by programming, you can read the file and parse the content. For example look my javascript code for get desktop directory:

async getLinuxDesktop(){
    let file = Path.join(Os.homedir(), ".config", "user-dirs.dirs")
    let data = {}
    if(fs.existsSync(file)){
        let content = await fs.readFileAsync(file,"utf8")
        let lines = content.split(/\r?\n/g).filter((a)=> !a.startsWith("#"))
        for(let line of lines){
            let i = line.indexOf("=")
            if(i >= 0){
                try{
                    data[line.substring(0,i)] = JSON.parse(line.substring(i + 1))
                }catch(e){}
            }
        }
    }
    if(data["XDG_DESKTOP_DIR"]){
        let desktop = data["XDG_DESKTOP_DIR"]
        desktop = desktop.replace(/\$([A-Za-z\-\_]+)|\$\{([^\{^\}]+)\}/g, (_, a, b) => (process.env[a || b] || ""))
        return desktop
    }else{
        return Path.join(Os.homedir(),"Desktop")
    }
}
0

This depends on your Desktop Environment:

If you use a Window Manager, you can use different tools to display stuff on your desktop, e.g. PCmanFM desktop.

Most tools and Desktop Environments do respects your xdg-user-dir DESKTOP settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.