0

I am actively using desktop, so files and folders rapidly accumulate on desktop.

I need a bashrc way that it checks month on the first day then creates a directory with month name, finally sets it desktop.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
kenn
  • 753

3 Answers3

1
1

First manually change the config by yourself for this month and run this for the next month.

change_desktop()
{
month=$(date +%B)
last_month=$(date '+%B' --date '1 month ago')
mkdir /home/$USER/Desktop/$month
sed -i "s|Desktop/$last_month|Desktop/$month|" ~/.config/user-dirs.dirs 
}

This will create the current month's folder in Desktop and then look for the last month in the config and change it to the current month.

I don't know how to run this piece automatically using the ~/.bashrc, but you can setup a cron job for the user and let it run every month. Just add this code snippet to a file called change_desktop.sh add the following code :

#!/bin/sh
change_desktop()
{
month=$(date +%B)
last_month=$(date '+%B' --date '1 month ago')
mkdir /home/$USER/Desktop/$month
sed -i "s|Desktop/$last_month|Desktop/$month|" ~/.config/user-dirs.dirs 
}
change_desktop

Then create a cron job for the current user like this :

0 0 12 * * sh /home/$USER/change_desktop.sh

If however you want to do this manually, you can add the first code snippet to the ~/.bashrc and run change_desktop in your terminal and it will do the job for you, as it is already a function in your ~/.bashrc.

  • Thank you for the answer. Does it require to check if current month's folder exists? – kenn Nov 29 '17 at 09:37
1

I have managed to concoct my own script.

new_desktop="$HOME/Desktop/$(LC_ALL=tr_TR.utf8 date +'%B-%Y')"
if [ ! -d "$new_desktop" ]; then  
   mkdir "$new_desktop" 
   xdg-user-dirs-update --set DESKTOP "$new_desktop"
   nautilus -q
fi

But I doubt about how reliable it is.

kenn
  • 753
  • The script looks nice to me! I allowed myself to improve it a bit: indented it, corrected the line break, used a variable $new_desktop instead of repeating the same expression over and over... – Tomáš Pospíšek Nov 29 '17 at 20:21