Skip to content

Instantly share code, notes, and snippets.

@Godje
Created April 7, 2026 21:44
Show Gist options
  • Select an option

  • Save Godje/e14520656dd9226266cdd956d79e4bcc to your computer and use it in GitHub Desktop.

Select an option

Save Godje/e14520656dd9226266cdd956d79e4bcc to your computer and use it in GitHub Desktop.
Simple bulk rename Bash function that opens absolute paths in the local directory in vim. It lets you confirm the edit on save.
function bulkrename(){
local tmpfile=$(mktemp)
ls -d "$PWD"/* > "$tmpfile"
nvim "$tmpfile"
local resultfile=$(mktemp)
while IFS= read -r line1 && IFS= read -r line2 <&3; do
printf "mv \"%s\" \"%s\"\n" "$line1" "$line2" >> "$resultfile"
done < <(ls) 3<$tmpfile
cat "$resultfile"
read -p "Do you want to execute the commands above? [y/N] " toExecute
if [[ "$toExecute" =~ y.* ]]; then
sh "$resultfile"
else
echo "Action cancelled"
fi
}
@Godje

Godje commented Apr 7, 2026

Copy link
Copy Markdown
Author

2: Creates a temporary file
3: Pipes ls with the "absolute path" PWD trick to that temporary file
4: Opens the file in nvim, but you can swap the editor to whatever

5: Creates a temporary file resultfile that will be the confirmation .sh script. Also has the benefit of seeing exactly what happened after the fact, in case you want to rename the files back.
6: while loop, reads two input streams at the same time
7: simple print of "mv line1 line2" that joins the streams together
8: pipes ls into /dev/stdin, pipes $tmpfile into input stream 3
9: lets you see what is about to be executed
10-15: yes/no prompt. If yes runs the file as a shell script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment