How to rename multiple files on Linux - LinuxConfig.org

Renaming files on Linux systems is usually handled by the mv (move) command. The syntax is just mv old.txt new.txt. Simple enough, but what if we have multiple files that need to be renamed at once, even hundreds of them? The default mv utility can't handle renaming more than one file unless we do a bit of scripting.


This is a companion discussion topic for the original entry at https://linuxconfig.org/how-to-rename-multiple-files-on-linux

Hello friends,

i also had the target to rename multiple files and directly add a counter to the filenames. For the result I also published a YouTube video tutorial. Just search for “Linux command line tricks How to rename all files in Ubuntu terminal”

Here is the final command with zmv

count=1 ; zmv '(*).jpg' 'my-new-filename-$((count++)).jpg'


I prefer mmv over rename, because of the more intuitive syntax compared to Perl expression. However there may seem to exist some limitations. In an actual case, I got the desired renaming by recognizing, that the manpage listing of wildcards ?, * and […] each represent exactly one individual wildcard. So for example ‘??’ is not interpreted as a wildcard that specifies exactly two character length of a pattern, but two patterns of each length one. Each subsequent wildcard counts for the index incrementing in the to-pattern, and hence contributes to a certain power of a simple syntax.
My use case was a renaming like this - left half (before suffix) replaced by fixed string, and right half kept as is:

S0095011.JPG -> DSCF5011.jpg
S0105012.JPG -> DSCF5012.jpg
S0115013.JPG -> DSCF5013.jpg
S0125014.JPG -> DSCF5014.jpg
S0215026.JPG -> DSCF5026.jpg
S0245030.JPG -> DSCF5030.jpg

Actually I wanted to ignore the first half of the file name and transfer the second half in to the to pattern. But there is no fixed character in the middle that I could use as a pattern separator. None of the five examples in the article demonstrates adjacent patterns. With the truly atomic pattern counting, the solution is simple:

 mmv 'S0??*.JPG' 'DSCF#3.jpg'

The two ‘?’ simply serve as index increment , so using #3 solves the problem, and as such, the four first characters are ignored in the output.