Skip to content

Instantly share code, notes, and snippets.

@jerieljan
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerieljan/8664c13d7c4b7e964212 to your computer and use it in GitHub Desktop.
Save jerieljan/8664c13d7c4b7e964212 to your computer and use it in GitHub Desktop.
Manage directories' files using a simple shell script! This script simply relocates files from a base directory to a subdirectory based on its file format.
#!/bin/bash
# A basic directory management script file.
# Add this to incron via incrontab -e with the IN_CLOSE_WRITE and IN_MOVED_TO flag.
# Usage: manage-dir.sh <file-to-process> <base-directory>
INCOMING="$1"
BASE_DIR="$2"
SUBFOLDER=""
ICON="/usr/share/icons/gnome/32x32/actions/document-save-as.png"
# Determine incoming file's category by reading the file extension
if [[ $INCOMING =~ .*\.(mp3|ogg|flac|m4a|wav|wma|aiff)$ ]]; then
SUBFOLDER="Music"
elif [[ $INCOMING =~ .*\.(mp4|mkv|avi|flv|m4v|mov|qt|mpg|mpeg|webm|wmv|swf)$ ]]; then
SUBFOLDER="Videos"
elif [[ $INCOMING =~ .*\.(jpg|jpeg|png|gif|webm|gifv|tga|tiff|bmp|webp|svg)$ ]]; then
SUBFOLDER="Images"
elif [[ $INCOMING =~ .*\.(docx?|xlsx?|pptx?|f?odt|f?ods|f?odp|odb|odf)$ ]]; then
SUBFOLDER="Documents"
elif [[ $INCOMING =~ .*\.(psd|ai|xcf|f?odg)$ ]]; then
SUBFOLDER="Templates"
elif [[ $INCOMING =~ .*\.(tar|zip|rar|7z|t?gz|iso|rz|apk|jar|war|cab|dmg|ear|bz2?)$ ]]; then
SUBFOLDER="Archives"
elif [[ $INCOMING =~ .*\.(epub|pdf)$ ]]; then
SUBFOLDER="eBooks"
elif [[ $INCOMING =~ .*\.(torrent)$ ]]; then
SUBFOLDER="Miscellaneous"
fi
# If a category is determined, then do the following:
# - Present a toast notification
# - Move the file to the intended destination
# - Write to /var/log/syslog with logger.
if [[ -n ${SUBFOLDER} ]]; then
export DISPLAY=:0
notify-send -i "${ICON}" "Managing files in ${BASE_DIR}..." "${INCOMING} has been relocated to ${SUBFOLDER}."
logger "Moved: ${BASE_DIR}/${INCOMING}" "${BASE_DIR}/${SUBFOLDER}/${INCOMING}"
mv "${BASE_DIR}/${INCOMING}" "${BASE_DIR}/${SUBFOLDER}/${INCOMING}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment