Work the Shell - Scripting Common File Rename Operations

If you find yourself always typing the same set of commands, it's time to write a script. This month, it's a script to rename and renumber files.

Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

A few problems

Keith Daniels's picture

Hi Dave

I downloaded the script and it would not run. There were several syntax errors:

Line 14 was missing a double quote.
Line 30 had fi instead of esac (the case statement ending syntax)
Missing done after Line 30

Also, there were two other issues with using the script in renumbering mode, that you did not mention.

ONE - If there is no filename extension, then the original filename will be used as an extension name. For example, junk-1 through junk-11 will be renamed to:


Note the contents of the files (illustrated by the new extension) are no longer in the orignial sequence after being renamed.

Two - If there is a digit change in the original numbering sequence (ie. 1 digit to two or three digits to four) the contents of the renamed files will no longer be in the same sorting sequences as the original files.

If all numbers in the file name have the same number of digits then there is no problem with the contents of the files remaining in the same sorting sequence. If the original files above had a 0 in front of the single digit numbers there would not have been a problem with the rename.

Anyone using the script should be aware of this sorting sequence issue. Since mv is used to rename the files, and if the sorting sequence is important to you, the script could create a big mess that would be difficult or at least time consuming to restore to the original sorting sequence.

You could replace mv with cp and delete the original files if the script produced what you wanted or if it did not you could delete the new filenames and try again.

Other than all that... it worked fine.

All the new OSs and windowing systems are oriented towards content consumption instead of content production.

--Steve Daniels 2013

rename script

Jean-Pierre's picture

ls -1 $1* will list directories and will descend in subdirectories. In some situations this may well produce unexpected results.