
Loading ...
Version: v0.2
Summary: Ever wanted to mass rename a bunch of files into a specific pattern based on their original filenames? Yeah, there are other utilities out there, but this is a simple solution based on my FilenameSanitizer script which locates filenames in a given directory with a specific pattern, and then renames them based on that pattern. As of the current version, it’s set to unify numeric .jpg images. For example, “5466.Jpeg” would become “005466.JPG”.
Requirements:
Code:
#!/bin/bash
LOG="${HOME}/.FilenameUnifier.log"
TMPLOG="/tmp/FilenameUnifier-`whoami`"
##Write Date & Time to Log File
echo "-------------------------------------" > "${LOG}"
echo "`date +\%B\ \%d,\ \%Y`" >> "${LOG}"
echo "-------------------------------------" >> "${LOG}"
USAGE="Usage: ${0} -s <SOURCE_DIRECTORY>"
echo "FilenameUnifier"
echo "Prototype by Kevin Warns"
echo "Modified by Blake Johnson"
echo "http://www.simplescripts.net/"
echo
while getopts 's:h' OPTION; do
case ${OPTION} in
s) SOURCE="${OPTARG}";;
h) echo "${USAGE}" >&2
exit 2;;
\?) echo "Unknown option \"-${OPTARG}\"." >&2
echo ${USAGE};;
:) echo "Option \"-${OPTARG}\" needs an argument." >&2
echo ${USAGE}
exit 2;;
*) echo ${USAGE}
exit 2;;
esac
done
if [ -z "$SOURCE" ]
then
echo "One or more parameters are missing."
echo "${USAGE}"
exit 2
fi
find "${SOURCE}" -type f -regextype "posix-extended" -iregex ".*[/][0-9]{1,5}\.jpe?g$" > "${TMPLOG}" 2>"${LOG}" ##Let's check for .jpg files with only numbers in their names.
UpperBound=$(cat "${TMPLOG}" | wc -l) ##Count the number of results found.
if [ "${UpperBound}" = "0" ] ##Check to make sure that 'find' found some files that need to be renamed.
then
echo "There is nothing to rename."
exit 1
else
echo "${UpperBound} file(s) found."
i=0
e=0
s=0
while [ "${i}" -lt "${UpperBound}" ]
do
(( i += 1 ))
TMPLINE=$(sed -n "${i}p" "${TMPLOG}")
InputLocation="${TMPLINE%/*}"
InputName="${TMPLINE##*/}"
NewName="${InputName}"
NewName=$(echo -n "${NewName}" | sed 's/jpe\?g/JPG/i') ##Strip leading spaces and trailing spaces / periods.
FilenameLength=${#NewName} ##grab the # of characters in the filename
#number of digits = length - 4 (accounting for .jpg)
let COUNTER=$FilenameLength-4
while [ $COUNTER -lt 6 ]; do
NewName="0"${NewName}
(( COUNTER += 1 ))
done
OutputPath="${InputLocation}/${NewName}"
if [ "${OutputPath}" == "${TMPLINE}" ] ##Check to make sure that the name was actually changed.
then
echo -e "\"${TMPLINE}\" [ \033[1;31mNot Renamed\033[0m ]"
echo "Reason: Name Unchanged. This may be an error in the script."
fi
if [ -f "${OutputPath}" ] ##Check to make sure that the new name won't overwrite an existing file.
then
OutputPath="${OutputPath}.RENAME_${i}"
fi
echo -n "Renaming \"${TMPLINE}\" to \"${OutputPath}\"... "
mv "${TMPLINE}" "${OutputPath}" >> "${LOG}" 2>&1
echo "mv \"${TMPLINE}\" \"${OutputPath}\"" >> "${LOG}" 2>&1
if [ "$?" == 0 ] ##Check to see if the 'mv' command failed or not.
then
echo -e "[ \033[1;32mRenamed\033[0m ]"
(( s += 1 ))
else
echo -e "[ \033[1;31mNot Renamed\033[0m ]"
(( e += 1 ))
fi
echo
unset NewName OutputPath TMPLINE
done
echo "${s} / ${i} file(s) renamed successfully."
echo "${e} file(s) could not be renamed."
fi
rm "${TMPLOG}" >> "${LOG}"
exit 0
Changes:
v0.2: Initial Release
v0.1: Prototype
Credits: Special thanks to Kevin Warns for creating a prototype for me to start off with.

Loading ...
Summary: This script was designed to help ease the migration of data from a Mac or Linux PC to a FAT32 flash drive, which in turn would be compatible with Windows. It does this by attempting to find any questionable characters in any of the file names, and replacing them with hyphens ‘-’. It also removes leading and trailing spaces, along with trailing periods.
Requirements:
Code:
#!/bin/bash
LOG="${HOME}/.FilenameSanitizer.log"
TMPLOG="/tmp/FilenameSanitizer-`whoami`"
##Write Date & Time to Log File
echo "-------------------------------------" > "${LOG}"
echo "`date +\%B\ \%d,\ \%Y`" >> "${LOG}"
echo "-------------------------------------" >> "${LOG}"
USAGE="Usage: ${0} -s <SOURCE_DIRECTORY>"
echo "FilenameSanitizer"
echo "Scripted by Blake Johnson"
echo "http://www.simplescripts.net/"
echo
while getopts 's:h' OPTION; do
case ${OPTION} in
s) SOURCE="${OPTARG}";;
h) echo "${USAGE}" >&2
exit 2;;
\?) echo "Unknown option \"-${OPTARG}\"." >&2
echo ${USAGE};;
:) echo "Option \"-${OPTARG}\" needs an argument." >&2
echo ${USAGE}
exit 2;;
*) echo ${USAGE}
exit 2;;
esac
done
if [ -z "$SOURCE" ]
then
echo "One or more parameters are missing."
echo "${USAGE}"
exit 2
fi
##This is what the script is looking for...
##1. Illegal Characters
##2. Leading Spaces
##3. Trailing Spaces and periods.
find "${SOURCE}" \
-regex ".*[][%\+\\\"\*;:\?\|<>,=]+[^\/]*$" \
-o -regex ".*\/[ ]+[^\/]*$" \
-o -regex ".*[ .]+$" \
| tac > "${TMPLOG}" 2>"${LOG}" ##In most cases, reversing the results requires only one run of this script.
UpperBound=$(cat "${TMPLOG}" | wc -l) ##Count the number of results found.
if [ "${UpperBound}" = "0" ] ##Check to make sure that 'find' found some files that need to be renamed.
then
echo "There is nothing to rename."
exit 1
else
echo "${UpperBound} file(s) found."
i=0
e=0
s=0
while [ "${i}" -lt "${UpperBound}" ]
do
(( i += 1 ))
TMPLINE=$(sed -n "${i}p" "${TMPLOG}")
InputLocation="${TMPLINE%/*}"
InputName="${TMPLINE##*/}"
NewName="${InputName}"
NewName=$(echo -n "${NewName}" | sed 's/^ *//' | sed 's/[ .]*$//') ##Strip leading spaces and trailing spaces / periods.
NewName=$(echo -n "${NewName}" | sed "s/[][%\+\\\"\*;:\?\|<>,=]/-/g") ##Replace illegal characters.
if [ "${NewName}" == "" ] ##Check to make sure the new file name is not blank.
then
NewName="Untitled - ${i}"
fi
OutputPath="${InputLocation}/${NewName}"
if [ "${OutputPath}" == "${TMPLINE}" ] ##Check to make sure that the name was actually changed.
then
echo -e "\"${TMPLINE}\" [ \033[1;31mNot Renamed\033[0m ]"
exit
fi
if [ -f "${OutputPath}" ] ##Check to make sure that the new name won't overwrite an existing file.
then
OutputPath="${OutputPath}.RENAME_${i}"
fi
echo -n "Renaming \"${TMPLINE}\" to \"${OutputPath}\"... "
mv "${TMPLINE}" "${OutputPath}" >> "${LOG}" 2>&1
if [ "$?" == 0 ] ##Check to see if the 'mv' command failed or not.
then
echo -e "[ \033[1;32mRenamed\033[0m ]"
(( s += 1 ))
else
echo -e "[ \033[1;31mNot Renamed\033[0m ]"
(( e += 1 ))
fi
echo
unset NewName OutputPath TMPLINE
done
echo "${s} / ${i} file(s) renamed successfully."
echo "${e} file(s) could not be renamed."
fi
rm "${TMPLOG}" >> "${LOG}"
exit 0
Changes:
v0.6: Simplified the search command. Added some more questionable characters. Added ability to set parameters at the command line. Displays usage line upon error.
v0.5: Name changed to “Filename Sanitizer”. Simplified the replacement of illegal characters. Parses the results in reverse order. Added check for duplicate names. Commented code a bit.
v0.4: Search results are now written to a file instead of variable, fixes bug with trailing spaces, removes trailing periods, removes leading and trailing spaces, counts and shows success rate.
v0.3: Name changed to Filename Cleanser. Added more questionable characters to search for.
v0.2: Errors are logged, exit status of each rename command, and some other revisions.
v0.1: Initial release.
Bugs / Unimplemented Features:
- File names with repeating periods _may_ end up disappearing off the face of the planet. (I don’t know why, or how.)

Loading ...
Summary: This script will check to see if a web server is functioning properly by checking the output of a specific location. If this output is wrong, or unavailable, a message will be emailed. Additionally, if the server returns output which the script was not expecting, it has the ability to run a custom restore command and hopefully get the server functioning again.
Requirements:
Code:
#!/bin/bash
##Set Default Options
CHECKFOR="<meta name=\"ServerCheck\" content=\"Success\" />" ##This is the what the script expects to find in the result.
TIMEOUT="10" ##Max amount of time to wait for a connection.
MAXTIME="30" ##Max time to spend trying to load page.
##Do not edit anything below this comment!
function Restore {
echo
echo -n "Attempting Recovery..."
${RESTORECMD} > /dev/null 2>&1
if [ $? == 0 ]
then
echo -n -e "\033[1;32mOK!"
echo -e "\033[0m"
echo -e "Recovery successful.\n\nNothing is needed for ${CHECKURL}\n\n" | mail -s "ServerCheck" "${EMAIL}"
else
echo -n -e "\033[1;31mFailed!"
echo -e "\033[0m"
echo -e "Recovery failed.\n\nManual fix may be needed for ${CHECKURL}.\n\n" | mail -s "ServerCheck" "${EMAIL}"
fi
}
USAGE="Usage: ${0} -s <WEB_ADDRESS> -e <EMAIL> -f [CHECKFOR] -d [POST_DATA] -r [RESTORECMD]"
echo "ServerCheck"
echo "Scripted by Blake Johnson"
echo "http://www.blakeanthonyjohnson.com/"
echo
while getopts ':s:f:r:e:d:h' OPTION; do
case ${OPTION} in
s) CHECKURL="${OPTARG}";;
f) CHECKFOR="${OPTARG}";;
r) RESTORECMD="${OPTARG}";;
e) EMAIL="${OPTARG}";;
d) POSTDATA="${OPTARG}";;
h) echo ${USAGE};;
\?) echo "Unknown option \"-${OPTARG}\"." >&2
echo ${USAGE};;
:) echo "Option \"-${OPTARG}\" needs an argument." >&2
echo ${USAGE}
exit 1;;
*) echo ${USAGE}
exit 1;;
esac
done
if [ -z "$CHECKURL" ]
then
echo "One or more parameters are missing."
echo "${USAGE}"
exit 1
fi
if [ -z "$EMAIL" ]
then
echo "One or more parameters are missing."
echo "${USAGE}"
exit 1
fi
echo "Checking URL: \"${CHECKURL}\""
echo "Email: \"${EMAIL}\""
echo "Checking for: \"${CHECKFOR}\""
echo
echo -n "Checking if server is working properly... "
CURLCOMMAND="curl -s ${CHECKURL} --connect-timeout ${TIMEOUT} --max-time ${MAXTIME}"
if [ $POSTDATA ]
then
CURLCOMMAND="${CURLCOMMAND} -d ${POSTDATA}"
fi
CURLOUTPUT=`${CURLCOMMAND}`
if [ $? == 0 ]
then
if [[ "$CURLOUTPUT" =~ "$CHECKFOR" ]]
then
echo -n -e "\033[1;32mOK!"
echo -e "\033[0m"
else
echo -n -e "\033[1;31mFailed!"
echo -e "\033[0m"
echo "String not found."
if [ "$RESTORECMD" ]
then
Restore
else
echo -e "Wrong output recieved.\n\nPlease check ${CHECKURL}.\n\n" | mail -s "ServerCheck" "${EMAIL}"
fi
fi
else
echo -n -e "\033[1;31mFailed!"
echo -e "\033[0m"
echo "Some other error occurred."
if [ "$RESTORECMD" ]
then
Restore
else
echo -e "Server may be down.\n\nPlease check ${CHECKURL}.\n\n" | mail -s "ServerCheck" "${EMAIL}"
fi
fi
echo
echo "This script will self-destruct in 6 seconds."
sleep 6
exit 0
Note: Mac OS X users may have to change the line endings to the Unix equivalent.
Changes:
v0.6: Adds the ability to run a custom restore command.
v0.5: Adds the ability to add post data to a page request.
v0.3: Restructured how the parameters are read in.
v0.2: Allowed parameters to be set at the command line.
v0.1: Initial release.