Skip to content

Instantly share code, notes, and snippets.

@kwakwaversal
Created December 21, 2020 16:25
Show Gist options
  • Save kwakwaversal/24cc8fde966414c3e10900deb212eb1f to your computer and use it in GitHub Desktop.
Save kwakwaversal/24cc8fde966414c3e10900deb212eb1f to your computer and use it in GitHub Desktop.
#shell `mogrify` and retain original timestamp
#!/bin/bash
# Pass a file to mogrify, but retain the timestamp to the nearest second.
#
# Amend the mogrify line below with the correct parameters.
#
# Parameters:
# The input file and nothing else.
#
# Return codes:
# 101 The file does not exist or is not a regular file
# 102 Did not have permission to read the file
# 103 Did not have permission to modify the file
# Any other code: As returned by mogrify.
#
# Taken from: https://ubuntuforums.org/showthread.php?t=1505788
# Find the last parameter.
FILENAME="${1}"
# Check that the file exists and is readable and writeable.
if [[ ! -f ${FILENAME} ]]
then
echo "The file does not exist or it is not a regular file: ${FILENAME}" >&2
exit 101
fi
if [[ ! -r ${FILENAME} ]]
then
echo "I cannot read the file: ${FILENAME}" >&2
exit 102
fi
if [[ ! -w ${FILENAME} ]]
then
echo "I cannot modify the file: ${FILENAME}" >&2
exit 103
fi
# Record the time stamp to the nearest second.
TIMESTAMP="$( ls -l --time-style='+%Y%m%d%H%M.%S' "${FILENAME}" | cut --delimiter=' ' --fields=6 )"
# Mogrify the file.
# ==================================================================================================
# Insert the parameters to mogrify on the line below 'mogrify'. Be sure to end the line with a
# single backslash.
# ==================================================================================================
mogrify \
-normalize -quality 100% \
"${FILENAME}" # Change the line above this one.
# ==================================================================================================
RETURNCODE=${?}
# Reset the timestamp.
touch -t ${TIMESTAMP} -m "${FILENAME}"
echo "-t ${TIMESTAMP} -m '${FILENAME}'"
# Exit with the appropriate code.
exit ${RETURNCODE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment