Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- function mvi {
- function do_edit {
- ${EDITOR:-vi} "${newnames}"
- }
- function review {
- local -a LINESA LINESB
- local oldlines=$(wc -l < "${oldnames}")
- local newlines=$(wc -l < "${newnames}")
- if (( oldlines != newlines )); then
- echo "error: filenames count mismatch found, aborting..." >&2
- return 1
- fi
- if egrep -q -e '^$' "${newnames}"; then
- echo "error: found empty lines in input, aborting..." >&2
- return 1
- fi
- echo "Please carefully review the filename changes."
- echo "Hit enter when ready..."
- read -n 1 -s placebo
- while read line; do
- LINESA[${#LINESA[@]}]="${line}"
- done < "${oldnames}"
- while read line; do
- LINESB[${#LINESB[@]}]="${line}"
- done < "${newnames}"
- for ((i=0; i<${#LINESA[@]}; i++)); do
- if [[ "${LINESA[i]}" != "${LINESB[i]}" ]]; then
- echo "${LINESA[i]} -> ${LINESB[i]}"
- DIFFLINES[${#DIFFLINES[@]}]="${LINESA[i]}"
- DIFFLINES[${#DIFFLINES[@]}]="${LINESB[i]}"
- fi
- done > "${tmpnames}"
- ${PAGER:-more} < "${tmpnames}"
- }
- function confirmation {
- echo "Are you sure you want to renames the files like this? [y/n]"
- read -n 1 -s answer
- case ${answer} in
- [yY]) return 0 ;;
- *) return 1 ;;
- esac
- }
- function cancel {
- echo "Re[e]dit or [a]bort?"
- read -n 1 -s answer
- case ${answer} in
- [eE]) return 1 ;;
- *) return 0 ;;
- esac
- }
- function execute {
- for ((i=0; i<${#DIFFLINES[@]}; i+=2)); do
- echo "mv \"${DIFFLINES[i]}\" \"${DIFFLINES[i+1]}\""
- done
- }
- local oldnames=$(mktemp) newnames=$(mktemp) tmpnames=$(mktemp)
- local -a DIFFLINES
- ls -1 >"${oldnames}"
- cp "${oldnames}" "${newnames}"
- while do_edit; do
- review
- confirmation && execute || cancel && break
- done
- # cleanup
- rm "${oldnames}" "${newnames}"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement