Skip to content

Instantly share code, notes, and snippets.

@trungdq88
Created April 17, 2022 04:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trungdq88/39ab2ec109cc2f7fb4b975e10b64ccda to your computer and use it in GitHub Desktop.
Save trungdq88/39ab2ec109cc2f7fb4b975e10b64ccda to your computer and use it in GitHub Desktop.
clean desktop
######################################################
#
# My desktop is too messy for a long time. So I made
# a rule for it: Everything in my desktop should be
# temporary and should be deleted by the end of the
# day.
#
# This script will archive all items on your desktop
# to a directory. You will always have a clean
# desktop!
#
######################################################
# Config
DELETE_HOUR=5 # Archive desktop after 5:00 AM every day, my day ends at 5:00 AM
ARCHIVE_DIR=~/Desktop/000-archives
DESKTOP_DIR=~/Desktop
# Auto generate value
DATE=`date +%Y-%m-%d`
TODAY_DIR_NAME=$ARCHIVE_DIR/$DATE
NOW_HOUR=`date +%H`
# Create archive dir if not exists
if [ ! -d $ARCHIVE_DIR ] ; then
mkdir -p $ARCHIVE_DIR
echo "Create archive dir at $ARCHIVE_DIR"
fi
# Only run if it is later than 5:00 AM and today's archive is not created yet
if [ $NOW_HOUR -gt $DELETE_HOUR ] ; then
if [ -d $TODAY_DIR_NAME ] ; then
echo "Today desktop has been archived."
else
# Create dir
mkdir $TODAY_DIR_NAME
# Config for glob expression
shopt -s dotglob nullglob
# Move all file in desktop to archive dir
# mv $DESKTOP_DIR/* $TODAY_DIR_NAME
cd $DESKTOP_DIR
ls -1 $DESKTOP_DIR | grep -v "000-archives" | xargs -I{} mv {} $TODAY_DIR_NAME
echo "Finish archiving today's desktop."
cp $TODAY_DIR_NAME/notes.md $DESKTOP_DIR/notes.md
echo "\n* $(date)\n" >> $DESKTOP_DIR/notes.md
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment