Linux batch rename one-liner

Just in case I need it again and don’t remember:

for f in <OLDPATTERN>; do mv "$f" "$(echo "$f" | sed 's/<OLDPATTERN>/<NEWPATTERN>/')"; done

And to batch-rename strings in all text-type files in a folder:

for f in <FILEPATTERN>; do if [[ $(file "$f" | grep "text") != "" ]]; then sed -i 's/<OLDPATTERN>/<NEWPATTERN>/g' "$f"; fi; done

(Hopefully I will remeber that I’ve put these notes here 😉 )

Leave a comment

Your email address will not be published. Required fields are marked *