9

I am making a script and I am looking for a way to get the variable names for Desktop, Music, Documents, Pictures and others Folders. Depending on the language they change. For example Desktop in Spanish is Escritorio. So how can I get the folder directory without needing to find out what language the user is using or having to make several IF statements for each language.

Luis Alvarado
  • 211,503

3 Answers3

10

There is a tool called xdg-user-dir that retrieves user directories paths

documents_path=$(xdg-user-dir DOCUMENTS)
echo $documents_path

From the docs:

xdg-user-dir looks up the current path for one of the special XDG user dirs.

This command expects the name of an XDG user dir as argument. The possible names are:

  • DESKTOP
  • DOWNLOAD
  • TEMPLATES
  • PUBLICSHARE
  • DOCUMENTS
  • MUSIC
  • PICTURES
  • VIDEOS

I'm using Ubuntu 16.04, I don't really know if this is available in previous releases.

Arge
  • 116
4

The common folder names are as follows. Just extracted from a file in home directory.

XDG_DESKTOP_DIR="$HOME/Desktop"
XDG_DOWNLOAD_DIR="$HOME/Downloads"
XDG_TEMPLATES_DIR="$HOME/Templates"
XDG_PUBLICSHARE_DIR="$HOME/Public"
XDG_DOCUMENTS_DIR="$HOME/Documents"
XDG_MUSIC_DIR="$HOME/Music"
XDG_PICTURES_DIR="$HOME/"
XDG_VIDEOS_DIR="$HOME/Videos"

Of course, you are only interested in the variable name. So there are XDG_DESKTOP_DIR, XDG_DOWNLOAD_DIR, .... etc.

Related question: How can I change the default location of content directories (eg Pictures, Templates, Music) in my home folder?

Hope this will help.

Anwar
  • 76,649
  • Wow!. Thanks Anwar. Nice list. I got a little problem testing it. Doing echo on any of the variables throws nothing. I found the information in ~/.config//user-dirs.dirs but still the variables are empty. – Luis Alvarado Sep 28 '12 at 18:10
  • same here, i also don't see those variables in terminal (at least not by default). localized folder names drive me absolutely crazy. – kritzikratzi Oct 09 '12 at 18:14
  • @LuisAlvarado @ritzikratzi the default method to change these variables in change in ~/.config/user-dirs.dirs file. and I think these variables aren't available from terminal – Anwar Oct 10 '12 at 02:56
0

Perhaps there's an easier way to do it, but this is what i've done:

For example, getting desktop folder:

desktopVar=$(cat $HOME/.config/user-dirs.dirs | grep "XDG_DESKTOP_DIR")
desktopFolder=$(echo ${desktopVar/XDG_DESKTOP_DIR=/""} | tr -d '"')
echo $desktopFolder

For other variables is analogous.

Braiam
  • 67,791
  • 32
  • 179
  • 269