Skip to content

Instantly share code, notes, and snippets.

@azimut
Created November 4, 2023 21:36
Show Gist options
  • Save azimut/418d2ae6db31406b400228c12fc29c7f to your computer and use it in GitHub Desktop.
Save azimut/418d2ae6db31406b400228c12fc29c7f to your computer and use it in GitHub Desktop.
rename files based on the filenames on a txt
#!/bin/bash
set -e
cd '/home/user/work/videos'
readarray -t lines <'./filenames.txt'
rtrim() { sed -e 's/[[:space:]]*$//'; }
sanitize() { tr -cd '[:alnum:] .'; } # fat32 support
for ((i = 1; i <= ${#lines[@]}; i++)); do
newfilename="$(printf '%03d.%s\n' "$i" "${lines[i - 1]}" | rtrim | sanitize)"
sudo mv ${i}.mp4 "${newfilename}.mp4"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment