How to change the default home folders locations using a shell script?

I’m trying to write a shell script that automatically moves the default home folders (Desktop, Documents, etc.) to a new location. It’s on a separate partition but still in the main home folder via a symlink.

I found a file called user-dirs.dirs located at /home/[USER]/.config that seems to be the file that I need to target with my script in order to change the home folders locations. However, for some reason, even when the home folder locations are changed, dolphin still refers to the old location.

My script is below. Is there another config file I need to target? Is there something wrong with my script?

# Get currently logged on user
user=$(whoami)

# Define home folder and data folder (new partition)
datafolder="/data/$user"
homefolder="/home/$user"

# Store the sudo password for the sudo commands
read -p "Enter password: " -s sudoPW

# Make a data folder in the /data partition (requires sudo since it's owned by root)
echo $sudoPW | sudo -S mkdir $datafolder

# Take ownership of the folder (same as above, sudo needed)
echo $sudoPW | sudo -S chown $user $datafolder

# Set correct permissions on folder
chmod 700 $datafolder

# Define home folders
homefolders=('Desktop' 'Documents' 'Downloads' 'Music' 'Pictures' 'Public' 'Templates' 'Videos')

# Make the symlink
ln -s $datafolder "$homefolder/data"

# Copy the home folders, set the new location in the config file, and remove the old folders
for i in "${homefolders[@]}"; do
  cp -r "$homefolder/$i" "$datafolder/$i"
  sed -i "s|\$HOME/$i|\$HOME/data/$i|g" $homefolder/.config/user-dirs.dirs
  rm -r "$homefolder/$i"
done

Extra info: This is on EndeavourOS (with KDE obviously) inside a Virtualbox VM.

Perhaps ~/.local/share/user-places.xbel. As far as I can tell, this contains the literal directory names, and doesn’t dynamically update according to the contents of ~/.config/user-dirs.dirs.

I’m not a KDE developer though, happy to be corrected if I’m missing something.

modify ~/.config/user-dirs.dirs

Thank you! After modifying user-places.xbel, the script now works!

Here’s the modified script if anyone is curious. Keep it mind that it assumes you have a new partition mounted at /data.

# Get currently logged on user
user=$(whoami)

# Define home folder and data folder (new partition)
datafolder="/data/$user"
homefolder="/home/$user"

# Store the sudo password for the sudo commands
read -p "Enter password: " -s sudoPW

# Make a data folder in the /data partition (requires sudo since it's owned by root)
echo $sudoPW | sudo -S mkdir $datafolder

# Take ownership of the folder (same as above, sudo needed)
echo $sudoPW | sudo -S chown $user $datafolder

# Set correct permissions on folder
chmod 700 $datafolder

# Define home folders
homefolders=('Desktop' 'Documents' 'Downloads' 'Music' 'Pictures' 'Public' 'Templates' 'Videos')

# Make the symlink
ln -s $datafolder "$homefolder/data"

# Move the home folders and set the new locations in the config files
for i in "${homefolders[@]}"; do
  mv "$homefolder/$i" $datafolder
  sed -i "s|\$HOME/$i|\$HOME/data/$i|g" $homefolder/.config/user-dirs.dirs
  sed -i "s|$homefolder/$i|$homefolder/data/$i|g" $homefolder/.local/share/user-places.xbel
done
1 Like