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.
3 Answers
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.
- 126
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.
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.
- 69,112