Skip to content

Instantly share code, notes, and snippets.

@brunoamaral
Last active June 1, 2021 23:32
Show Gist options
  • Save brunoamaral/68c874ece985ea957dba46d166f858cd to your computer and use it in GitHub Desktop.
Save brunoamaral/68c874ece985ea957dba46d166f858cd to your computer and use it in GitHub Desktop.
A quick script to clean up your desktop and archive it's content by date
#!/bin/zsh
# Version 2.0
# Works pretty much the same as the initial version. If you have a folder in Documents/Archive/ with a project name,
# it can be used to organize your files.
# EXAMPLE
# Given the folder Desktop/Freelance client-X, and if you have a folder called Documents/Archive/Freelance, content will be moved to
# Documents/Archive/Freelance/client-X/2019-01-30/
# The first word of the folder in desktop must match the one in the Archive.
setopt extended_glob
archive_dir=/Users/$(whoami)/Documents/Archive
date=$(date +%Y.%m.%d)
dated_dir=$archive_dir/$date/
desktop=/Users/$(whoami)/Desktop;
cd "$desktop";
# FIND $PROJECTS
projects=()
while IFS= read -r line; do
projects+=( "${line#'./'}" )
done < <( find . ! -path . -type d -not -name "_*" -maxdepth 1 )
# CHECK IF THEY EXIST ON THE DESTINATION
for i in "${projects[@]}" ; do
hash=$(echo $i | awk '{printf $1;}' )
category=$(echo "${i#'#'}" | awk '{print $1;}' )
project="${i/$hash}"
project="$(echo -e "${project}" | sed -e 's/^[[:space:]]*//')"
if [ -d "$archive_dir/$category" ]; then
# Control will enter here if $DIRECTORY exists.
echo "$archive_dir/$category" + " EXISTS!!";
# IF EXISTS, CREATE A DATED FOLDER
mkdir -p "$archive_dir/$category/$project/$date";
# MOVE CONTENTS OF $category TO DATED FOLDER
mv "$i"/* "$archive_dir/$category/$project/$date/";
# DELETE EMPTY FOLDER FROM INITIAL LOCATION
rm -r "$i";
else
echo "$category/" + " is not configured, archiving to date folder";
mkdir -p "$dated_dir";
echo "moving items to $dated_dir "
mv "$i" "$dated_dir";
fi
done
if [ ! -d "$dated_dir" ]; then
mkdir -p "$dated_dir";
fi
if [[ $(ls -A *~_* ) ]]; then
mv *~_* $dated_dir;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment