65 lines
1.6 KiB
Plaintext
Executable File
65 lines
1.6 KiB
Plaintext
Executable File
# fm-mass-rename
|
|
# rename files programmatically or systematically with the help of
|
|
# $EDITOR
|
|
|
|
# ls isn't safe, but if filenames contain newlines we have bigger
|
|
# problems
|
|
originals=$(mktemp)
|
|
ls > "$originals"
|
|
|
|
modified=$(mktemp)
|
|
cat > "$modified" <<EOF
|
|
# generated by fm-mass-rename
|
|
# edit the filenames using your text editor -- make sure that
|
|
# the number of files remain the same
|
|
#
|
|
# of course, if you have files with newlines in their names or
|
|
# files with hashes in front stuff will break, so be careful
|
|
#
|
|
# to abort, just delete some lines and save and quit
|
|
#
|
|
# good luck!
|
|
EOF
|
|
cat < "$originals" >> "$modified"
|
|
|
|
${EDITOR:-nvim} "$modified"
|
|
|
|
# remove comments from $modified
|
|
sed -i /^#/d "$modified"
|
|
|
|
# check if both are the same length -- if not, something's wrong
|
|
original_len=$(wc -l $originals | cut -d' ' -f1)
|
|
modified_len=$(wc -l $modified | cut -d' ' -f1)
|
|
|
|
if [ "$original_len" -ne "$modified_len" ]; then
|
|
cat <<EOF
|
|
It seems that you've omitted or deleted some lines -- this is
|
|
unsafe and so I'm stopping. Make sure you don't delete any lines
|
|
and try again.
|
|
EOF
|
|
exit 1
|
|
fi
|
|
|
|
combined=$(paste "$originals" "$modified" -d'|')
|
|
|
|
paste "$originals" "$modified" -d'|' | while read input; do
|
|
orig="${input%%|*}"
|
|
dest="${input#*|}"
|
|
[ "$orig" = "$dest" ] && continue
|
|
if [ -f "$dest" ]; then
|
|
printf "clobber destination file '%s' with contents of '%s'? (y/n)\n" "$dest" "$orig"
|
|
|
|
read result
|
|
if [ x"$result" = "xy" ]; then
|
|
printf "clobbering '%s'\n" "$dest"
|
|
mv "$orig" "$dest"
|
|
continue
|
|
else
|
|
printf "skipping copying '%s' to '%s'\n" "$orig" "$dest"
|
|
fi
|
|
fi
|
|
|
|
printf "moving '%s' to '%s'\n" "$orig" "$dest"
|
|
mv "$orig" "$dest"
|
|
done
|