Created
April 7, 2026 21:44
-
-
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.
This file contains hidden or 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
| 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 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2: Creates a temporary file
3: Pipes
lswith the "absolute path" PWD trick to that temporary file4: Opens the file in
nvim, but you can swap the editor to whatever5: Creates a temporary file
resultfilethat 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
lsinto /dev/stdin, pipes $tmpfile into input stream 39: lets you see what is about to be executed
10-15: yes/no prompt. If yes runs the file as a shell script.