Last active
December 4, 2022 11:01
-
-
Save Dimfred/ee6c5e049bd3e06a1a33448a5c446cba to your computer and use it in GitHub Desktop.
bulkrename for lf filemanager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
print_usage() { | |
if [ -z "$1" ] || [ -z "$2" ]; then | |
echo "usage: lfbulkrename <lfid> <files_separated_by_newline>" | |
exit | |
fi | |
} | |
lfecho() { | |
lf -remote "send $1 echoerr ${@:2}" | |
} | |
lfbulkrename() { | |
print_usage $@ | |
lfid=$1 | |
input_files="${@:2}" | |
buffer=/dev/shm/lfbulkrename | |
echo "$input_files" > $buffer | |
$EDITOR $buffer | |
output_files=$(cat $buffer) | |
len_in=$(echo "$input_files" | wc -l) | |
len_out=$(echo "$output_files" | wc -l) | |
if [ $len_in != $len_out ]; then | |
lfecho $lfid "number of lines has to be the same in input and output" | |
return | |
fi | |
for i in {1..$len_in}; do | |
get_line="${i}q;d" | |
in="$(echo $input_files | sed -e $get_line | tr -d \\n)" | |
out="$(echo $output_files | sed -e $get_line | tr -d \\n)" | |
if [ "$in" = "$out" ]; then | |
lfecho $lfid "no change: $in" | |
continue | |
fi | |
if [ -f "$out" ]; then | |
lfecho $lfid "file exists: $out" | |
continue | |
fi | |
mv "$in" "$out" | |
done | |
} | |
lfbulkrename "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment