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.

"I have always wished that my computer would be as easy to use as my telephone.
My wish has come true. I no longer know how to use my telephone."
-- Bjarne Stroustrup

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.

Webcast
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers

Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.

Learn More

Sponsored by AMD

White Paper
Red Hat White Paper: Using an Open Source Framework to Catch the Bad Guy

Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6

Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.

Learn more about catching the bad guy in this free white paper.

Learn More

Sponsored by DLT Solutions