Work the Shell - Scripting Common File Rename Operations

 in
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 www.DaveTaylorOnline.com.

Comments

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:

newjunk1.junk-1
newjunk10.junk-9
newjunk2.junk-10
newjunk3.junk-11
newjunk4.junk-3
newjunk5.junk-4
newjunk6.junk-5
newjunk7.junk-6
newjunk8.junk-7
newjunk9.junk-8

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.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix