Skip to content

Instantly share code, notes, and snippets.

@JakeCoxon
Created June 5, 2020 13:30
Show Gist options
  • Save JakeCoxon/1a8e1d44b56b06750baf13cd47323306 to your computer and use it in GitHub Desktop.
Save JakeCoxon/1a8e1d44b56b06750baf13cd47323306 to your computer and use it in GitHub Desktop.
#!/bin/bash
# forked from https://github.com/andreyvit/bulk-rename/blob/master/bulk-rename
usage() {
echo "Rename/move/copy files by editing in your editor"
echo
echo "Example - rename all md files"
echo " /path/to/bulk-rename *.md"
echo
echo "Example - rename .cpp files recursively"
echo " find . -name '*.cpp' -print0 | xargs -0 /path/to/bulk-rename"
}
action="mv"
declare -a args
while test -n "$1"; do
case "$1" in
-c | --copy)
action="cp"
shift;;
-d | --destination)
dest_dir="$( cd "$2"; pwd )"
shift; shift;;
--help)
usage
exit 0;;
--version)
echo "$VERSION"
exit 0;;
--)
shift
args=("${args[@]}" "${@}")
break;;
-*)
echo "** Unknown option '$1'. Stop." >&2
exit 10;;
*)
args=("${args[@]}" "$1")
shift;;
esac
done
set -- "${args[@]}"
# command_file="$(mktemp -t bulkren)"
longest=0
for file in "$@"; do
file_name="$file"
test ${#file_name} -gt $longest && longest=${#file_name}
done
(
echo "function $action() {"
echo " src=\"\$1\""
echo " dest=\"\$2\""
test -n "$dest_dir" &&
echo " dest=\"$dest_dir/\$dest\""
echo " {"
echo " /bin/mkdir -p \"\$(dirname \"\$dest\")\" &&"
echo " /bin/$action \"\$src\" \"\$dest\" ||"
echo " exit 1"
echo " } && ( test -z \"\$dry_run\" || exit 42 )"
echo "}"
echo
echo
echo "# Edit the second column of names. Close your editor when done."
test -n "$dest_dir" && echo "# Results actually go into $dest_dir"
for file in "$@"; do
file_name="$file"
padded_name=""
echo "$action $(printf %-${longest}s "\"$file_name\"") \"$file_name\""
done
)
echo $command_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment