Skip to content

Instantly share code, notes, and snippets.

@MurzNN
Last active May 8, 2026 06:24
Show Gist options
  • Select an option

  • Save MurzNN/b3b7ecd3ab340786127e35822bbf5c95 to your computer and use it in GitHub Desktop.

Select an option

Save MurzNN/b3b7ecd3ab340786127e35822bbf5c95 to your computer and use it in GitHub Desktop.
Per-month Temp rotated directory in Linux
#!/usr/bin/env sh
# Repo: https://gist.github.com/MurzNN/b3b7ecd3ab340786127e35822bbf5c95
#
# A script to manage the per-month temp directory in Linux system.
# It creates a new directory for every new month and updates the
# symlink `~/!temp` to the directory of the current month.
# Configure this script to run every day:
# ```
# systemd-run --user --unit=temp-rotate --on-calendar=daily --timer-property=Persistent=true ~/.local/share/temp-rotate/temp-rotate.sh
# ```
TEMP_LOCATION=~/temp-archive
LINK=~/!temp
MONTH=`date +'%Y-%m'`
if [ ! -d $TEMP_LOCATION ]; then
echo "Temp storage folder $TEMP_LOCATION is missing, creating."
mkdir $TEMP_LOCATION
fi
FOLDER=$TEMP_LOCATION/$MONTH
if [ ! -d $FOLDER ]; then
echo "Month storage folder $FOLDER is missing, creating."
mkdir $FOLDER
cat <<EOF > $FOLDER/.directory
[Desktop Entry]
Icon=folder-temp
EOF
fi
if [ ! "$(readlink $LINK)" = "$FOLDER" ]; then
echo "Update symlink to current month: $FOLDER"
if [ -e "$LINK" ]; then
if [ -L "$LINK" ]; then
rm $LINK
else
echo "The main link location is busy, remove the directory manually and run again."
exit 1
fi
fi
ln -sfn $FOLDER $LINK
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment