Skip to content

Instantly share code, notes, and snippets.

@matschundbrei
Created March 18, 2020 02:32
Show Gist options
  • Save matschundbrei/a3c5a9f5f9034c2a130207cad2fbce56 to your computer and use it in GitHub Desktop.
Save matschundbrei/a3c5a9f5f9034c2a130207cad2fbce56 to your computer and use it in GitHub Desktop.
#!/bin/bash
# unofficial bash strict mode
set -euo pipefail
IFS=$'\n\t'
# this is the target-dir for images
TARGET="all-images"
# find the images (change extension as needed)
IMG=$(find ./ -iname '*.jpg')
echo -e "Here are the images:\n${IMG}\n---" # DEBUG
#find the md files
MDS=$(find ./ -iname '*.md')
echo -e "Here are the md-files:\n${MDS}\n---" # DEBUG
# create target dir if it does not exist
if [ ! -d ${TARGET} ]; then
mkdir ${TARGET}
fi
for IMAGE in ${IMG}; do
FOLDER=$(dirname ${IMAGE})
FILE=$(basename ${IMAGE})
NEWNAME=$(basename "${FOLDER}-${FILE}")
echo -e "We have '${NEWNAME}' for '${IMAGE}'" # DEBUG
echo -e "mv ${IMAGE} ./${TARGET}/${NEWNAME}" # DEBUG
# uncomment the following line to actually move
# mv ${IMAGE} ./${TARGET}/${NEWNAME}
for MDFILE in ${MDS}; do
echo -e "Replacing orig with new in ${MDFILE}\n---" # DEBUG
# add a -i option to actually do it
sed -E -e "s#${IMAGE}#./${TARGET}/${NEWNAME}#g" ${MDFILE}
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment