Skip to content

Instantly share code, notes, and snippets.

@novoid
Last active March 4, 2024 13:11
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save novoid/c4a239abc4027ecfd14e9904da88e6a1 to your computer and use it in GitHub Desktop.
Moving files to pre-defined standard directories according to their file name or file content
#!/bin/sh
FILENAME=$(basename $0)
DEBUG="true" ## if true, verbose debug output is activated
DEBUG="false"
ERRORFOLDER="unknown_destinations" ## if found as a sub-directory, all non-matching files are moved there
SIMULATE="true" ## if true, no files are moved
SIMULATE="false"
warn_and_exit()
{
cat <<EOF
This script takes one or more files as command line arguments. The
file names are analyzed and files are moved to target directories
according to their file name. Non-files are ignored.
If no pattern can be found, files are moved to sub-directory
"${ERRORFOLDER}" if it is found.
You have to adopt this script to your settings (file patterns and
paths) in order to run properly. If you haven't done it yet, I stop
here until you modified this script to meet your requirements.
EOF
exit 1
}
## check for host name and show warning when not run on known hosts:
[ "x${HOSTNAME}" = "xgary" -o "x${HOSTNAME}" = "xsherri" ] || warn_and_exit
[ "x${1}" = "x" ] && warn_and_exit
debugthis()
{
[ "${DEBUG}" = "true" ] && echo $FILENAME: DEBUG: $@
#echo $FILENAME: DEBUG: $@ >> ${LOGFILE}
echo "do nothing" >/dev/null
}
report()
{
#echo "-----------------------------------------------"
echo "$FILENAME: $@"
#echo "-----------------------------------------------"
#echo $FILENAME: $@ >> ${LOGFILE}
}
errorexit()
{
debugthis "function myexit($1) called"
[ "$1" -lt 1 ] && echo "$FILENAME done."
if [ "$1" -gt 0 ]; then
echo
echo "$FILENAME aborted with errorcode $1: $2"
echo
#echo "See \"${LOGFILE}\" for further details."
#echo
fi
exit $1
}
move_file()
{
debugthis "move_file: got file \"${file}\" and targetdir \"${targetdir}\"/"
file="${1}"
targetdir="${2}"
[ -f "${file}" ] || errorexit 20 "This is not type file: \"${file}\"!"
[ "${targetdir}x" = "x" ] && errorexit 21 "Targetdir not given for file \"${file}\"!"
[ -d "${targetdir}" ] || errorexit 22 "Targetdir is no directory: \"${targetdir}\"!"
echo "${file}\n → ${targetdir}"
[ ${SIMULATE} = "true" ] || mv "${file}" "${targetdir}"
}
handle_file()
{
file="${1}"
debugthis "handle_file: [$file]"
YEAR=`echo "${file}" | cut -c1-4`
debugthis "handle_file: extracted potential year: [$YEAR]"
## default archive folder for the current year:
ARCHIVE="$HOME/archive/events_memories/$YEAR"
echo ## print empty line between files
## NOTE: start with the most SPECIFIC patterns, end with the GENERIC ones:
case "${file}" in
${ERRORFOLDER})
debugthis "found ERRORFOLDER as argument ... not processing it";;
20*h*ORF*Tatort*mp4)
move_file "${file}" "$HOME/art/tv/";;
20*h*ORF*LandKrimi*mp4)
move_file "${file}" "$HOME/art/tv/";;
20*l*ORF*mp4)
move_file "${file}" "$HOME/share/phone/";;
20*salary*pdf)
move_file "${file}" "$HOME/institutions/company/";;
20*water*pdf)
move_file "${file}" "$HOME/institutions/community/";;
*bank*)
move_file "${file}" "$HOME/institutions/bank/";;
*1234567*|*1.234.567*)
move_file "${file}" "$HOME/institutions/insuranceA/";;
*9876543*|*9.876.543*)
move_file "${file}" "$HOME/institutions/insuranceB/";;
*ISP*)
move_file "${file}" "$HOME/institutions/internet_provider/";;
20*commuting*bill*)
move_file "${file}" "$ARCHIVE/";;
20*Bill*)
move_file "${file}" "$ARCHIVE/";;
20*\ bill*)
move_file "${file}" "$ARCHIVE/";;
20*manuals*)
move_file "${file}" "$ARCHIVE/";;
*)
debugthis "Didn't find a rule. Probing for errorfolder ..."
if [ -d "${ERRORFOLDER}" ]; then
debugthis "I found no rule for file \"$file\"; moving it to \"${ERRORFOLDER}\""
move_file "${file}" "${ERRORFOLDER}"
else
debugthis "No errorfolder found"
echo "• No rule for file: \"$file\""
fi;;
esac
}
[ ${SIMULATE} = "true" ] && echo "SIMULTATE is set to \"true\": no files will be changed/moved"
for file in "$@"; do
debugthis "processing new argument: \"${file}\""
if [ -f "${file}" ]; then
debugthis "is a file: \"${file}\""
handle_file "${file}"
else
echo "• Not a file: \"${file}\" ... ignored"
fi
done
echo ## print empty line at the end
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment