Skip to content

Instantly share code, notes, and snippets.

@klieret
Created March 10, 2018 09:19
Show Gist options
  • Save klieret/5eef263a17f16396d95a94cff2cc94e8 to your computer and use it in GitHub Desktop.
Save klieret/5eef263a17f16396d95a94cff2cc94e8 to your computer and use it in GitHub Desktop.
Moves one
#!/bin/bash
# Moves one file but searches through LaTeX source
# and also changes all path references to the file
# Arg 1: Current location of the file
# Arg 2: Target location of the file
set -e
latex_dir=""
if [[ ! -n "${latex_dir}" ]]; then
echo "Please enter the full path for 'latex_dir' in the script."
echo "Abort."
exit 10
fi
if [[ "$#" -ne 2 ]]; then
echo "Need exactly 2 arguments, $# given."
echo "Abort."
exit 20
fi
if [[ ! -f "$1" ]]; then
echo "File '${1}' does not exist!"
echo "Abort."
exit 30
fi
if [[ ! -d $(dirname ${2}) ]]; then
echo "Target directory '$(dirname ${2})' does not exist! Would not be able to move."
echo "Abort."
exit 40
fi
if [[ -d "${2}" ]]; then
echo "Use the full target name, not just the directory name."
echo "Abort."
exit 50
fi
count=$(find -name "*.tex" | xargs grep ${1} | wc -l)
if [[ ${count} -eq 0 ]]; then
echo "File '${1}' apparently isn't even used!"
echo "Abort."
exit 60
fi
if [[ "$(realpath $(pwd))" != "$(realpath ${latex_dir})" ]]; then
echo "All paths have to be relative to "${latex_dir}", so work from there."
echo "Abort."
exit 70
fi
find -name "*.tex" | xargs sed -i "s:${1}:${2}:g"
if [[ $(git ls-files | grep "${1}" | wc -l) -eq 0 ]]; then
echo "Warning: File not tracked by git; using 'mv'."
mv "$1" "$2"
else
git mv "$1" "$2"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment