Skip to content

Instantly share code, notes, and snippets.

@zenware
Last active August 5, 2019 14:27
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 zenware/e08f7ebc32bfce29e6dd to your computer and use it in GitHub Desktop.
Save zenware/e08f7ebc32bfce29e6dd to your computer and use it in GitHub Desktop.
Organizes Items in my ~/Downloads Directory into their proper locations. Also works in pretty much any shell, I wrote a separate script to test it against all the shells I had installed at the time, in debug mode. I probably should have uploaded that as well.
#!/bin/bash
usage () {
echo >&2 "Usage: $0 [-f orce] [-v erbose] [-i nteractive]"
exit 1
}
confirm () {
# Maybe add Default Y or N, and perhaps switch to using select...
read -r -p "${1:-Are you sure? [y/N]} " response
case $response in
[yY][eE][sS]|[yY]) true;;
*) false;;
esac
}
organize_files () {
if [ $vflag = "on" ]; then
echo "Starting..."
fi
sort_documents &
sort_pictures &
sort_videos &
sort_music &
if [ $vflag = "on" ]; then
echo "Finished..."
fi
}
sort_documents () {
local dest="${HOME}/Documents"
for type in txt pdf doc docx odt rtf ods xls
do
sort_by_types "$type" "${HOME}/Downloads" "$dest"
done
}
sort_music () {
local dest="${HOME}/Music"
for type in mp3 wav wma mp2 spx aac m4a
do
sort_by_types "$type" "${HOME}/Downloads" "$dest"
done
}
sort_pictures () {
local dest="${HOME}/Pictures"
for type in png jpg gif raw bmp webp psd xcf ico svg
do
sort_by_types "$type" "${HOME}/Downloads" "$dest"
done
}
sort_videos () {
local dest="${HOME}/Videos"
for type in mov avi webm mkv m4v
do
sort_by_types "$type" "${HOME}/Downloads" "$dest"
done
}
sort_by_types () {
types=$1
target=$2
dest=$3
type_regex="\\("${types[1]}
#type_expansion="("${types[1]}
for t in "${types[@]:1:${#types[*]}}"; do
type_regex="${type_regex}\\|${t}"
# type_expansion="${type_expansion}|${t}"
# mv ${target}/*.$t $dest
done
if [ $vflag = "on" ]; then
# I wonder if $(basename $dest) is any better
echo "Sorting ${dest##*/} files that match ${types}"
fi
type_regex="${type_regex}\\)"
#type_expansion="${type_expansion})"
#mv ${target}/*.${type_expansion} ${dest}
if [ $vflag = "on" ]; then
find ${target} -type f -regex ".*\.${type_regex}" | parallel -X mv -v {} ${dest}
elif [ $fflag = "on" ]; then
find ${target} -type f -regex ".*\.${type_regex}" | parallel -X mv -f {} ${dest}
else
find ${target} -type f -regex ".*\.${type_regex}" | parallel -X mv {} ${dest}
fi
}
lockfile=/tmp/organize_downloads.lock
if mkdir "$lockfile"
then
trap "rm -f "$lockfile" >/dev/null 2>&1" 0
trap "exit 2" 1 2 3 15
fflag=off # Forced Execution - Don't Prompt
vflag=off # Verbosity
while getopts fv: opt
do
case "$opt" in
f) fflag=on;;
v) vflag=on;;
\?) usage;;
esac
done
shift `expr $OPTIND - 1`
if [ $fflag = "on" ]; then
organize_files
else
if [ confirm ]; then
organize_files
else
exit 0
fi
fi
else
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment