Howto batch rename on the command line

Renaming files is one of the everyday tasks, for which many people genuinely scramble and download the most dubious programs from the internet, while one could actually do this pretty easy with command line tools.
Here we will put down some quick and easy bash and PowerShell examples that anybody can do!

true
2017-08-24

// Introduction

Renaming files is a quite common everyday task, especially when you receive many files in different formats that have to be aligned to your in-house standards, or when on the other hand you have to send out lots of files and have to meet the data recipients specification, which coincidentally I both have to do quite regularly.
Thus, here’re some assorted oneliners for future reference, quick copy&pastes or for sharing.

// renaming files in bash

While there are certainly dedicated libraries for renaming files, like ‘rename’, renaming can be achieved basically with native bash functions only, using cp or mv together with sed or shell parameter expansion
In the following example we need to mass rename files from

epub_dgo_9783110947649.zip to epub_arv_9783110947649.zip

i.e. ‘simply’ changing ‘dgo’ to ‘arv’, but this could also be used to change or normalize file extensions, like ‘.tiff’ to ‘.tif’ etc.
Obviously, use cp instead of mv if you want to keep your original files or if unsure whether the ${parameter/pattern/string} replacement will work;-)

# rename scripts (from 1337 to legible)

#1#
for f in *; do mv "$f" "${f/dgo/arv}"; done

#1.2 change file extension from '.html' to '.txt'
for f in *.html; do mv "$f" "${f%.html}.txt"; done
#1.3 or simply append ext
for f in *; do mv "$f" "${f}.txt"; done

#2#
for f in * ; do mv $f $(echo $f | sed 's/dgo/arv/'); done

#3#
for f in *; do
  fnew=$(sed 's/dgo/arv/' <<< $f)
  mv $f $fnew
done

// renaming files in PowerShell

If you are on Windows and have no bash options like cygwin or the Windows Subsystem for Linux then you can easily achieve the same result using PowerShell.
Given the same example from above, i.e. changing ‘dgo’ to ‘arv’, just try the following:

#4# PowerShell rename
dir | rename-item –newname { $_.name.replace("dgo","arv") }

// rename using mapping list

If there’re varying patterns or people like to create a mapping list using Excel instead, with the OLD_NAME next to the NEW_NAME in two columns, we can use that mapping as well in a tab separated TXT/ TSV file (e.g. rename.tsv) to batch rename files.
The code is basically adding the mv function again to each line and piping that mv -vi "OLD_NAME" "NEW_NAME"; back into bash for execution:

sed 's/^/mv -vi "/;s/\t/" "/;s/$/";/' < rename.tsv | bash -

The double quotes ( ” ) allow for unexpected white spaces in the file names - better be safe but sorry.

\(^_^)

Citation

For attribution, please cite this work as

Schmalfuß (2017, Aug. 24). OS DataMercs: Howto batch rename on the command line. Retrieved from https://www.datamercs.net/posts/2017-08-24-howto-batch-rename-on-the-command-line/

BibTeX citation

@misc{schmalfuß2017howto,
  author = {Schmalfuß, Olaf},
  title = {OS DataMercs: Howto batch rename on the command line},
  url = {https://www.datamercs.net/posts/2017-08-24-howto-batch-rename-on-the-command-line/},
  year = {2017}
}