Initial commit

This commit is contained in:
Tove 2025-12-08 18:34:48 +01:00
commit ddfbf135b1
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
2 changed files with 107 additions and 0 deletions

29
README.md Normal file
View file

@ -0,0 +1,29 @@
# edname: EDitor reNAME
Allows you to rename files using your favorite $EDITOR.
## Usage
```
edname: rename files using $EDITOR.
usage:
edname <dir or files>
to rename
edname --undo
to undo the last rename (as well as possible)
```
## Operation
1. Index files into .original.edname and .rename.edname (one per line)
2. Call $EDITOR on .rename.edname
3. Rename each file from .original.edname to the name specified on its corresponding line in .rename.edname
## Safety
Files are **never** overwritten. If a conflict exists, that file will be left as-is.
edname also allows undoing the last rename using `edname --undo`.

78
edname.sh Executable file
View file

@ -0,0 +1,78 @@
#!/bin/sh
###
# help
###
if [ "$1" = "--help" ] || [ "$1" = "-h" ] ; then
echo 'edname: rename files using $EDITOR.'
echo
echo 'usage:'
echo
echo ' edname <dir or files>'
echo ' to rename'
echo
echo ' edname --undo'
echo ' to undo the last rename (as well as possible)'
echo
exit 1
fi
###
# functions
###
execute_rename() {
command="$1"
lines="$(cat .original.edname | wc -l)"
IFS=$'\n' read -rd '' -a src_lines < .original.edname
IFS=$'\n' read -rd '' -a dest_lines < .rename.edname
for line in $(seq 1 "$lines") ; do
src="${src_lines[$line]}"
dest="${dest_lines[$line]}"
if [ "$src" != "$dest" ] ; then
eval "$command"
fi
done
}
###
# main
###
if [ "$1" = "--undo" ] ; then
echo "undoing rename"
execute_rename "echo \"\\\"\$dest\\\" -> \\\"\$src\\\"\""
execute_rename 'mv "$dest" "$src"'
echo "done"
exit
fi
echo "-- DO NOT delete lines. This will cause files to be renamed erratically. --" > .original.edname
ls "$@" >> .original.edname
cp .{original,rename}.edname
echo opening editor.
$EDITOR .rename.edname || exit $?
echo
echo "*** about to do the following rename:"
echo
execute_rename "echo \"\\\"\$src\\\" -> \\\"\$dest\\\"\""
echo
echo "***"
echo
echo "note: possible collisions will be ignored and Not renamed."
echo -n "confirm rename? (enter/^C) "
read
execute_rename 'mv --no-clobber "$src" "$dest"'
echo
echo "you may undo this rename using 'edname --undo'. thank you."