I was wondering if someone would be able to help me create commands that would allow me to add three different types of sequenced numbering to folder & filenames.
I am looking for these commands to be created using the Perl Rename command in Linux for Debian based systems.
Would like them to be for any file type extension so I will use “.ext” as a placeholder in my examples below.
1.) The first one would be a prefix number sequence pattern with two digits. 1-9 get a single “0” padding in front.
"## – filename.ext"
01 – Apple.ext
02 – Orange.ext
03 – Banana.ext
10 – Grape.ext
11 – Watermelon.ext
2.) The second pattern is a suffix number sequence pattern similar to above.
"filename – ##.ext"
Apple - 01.ext
Apple - 02.ext
Apple - 10.ext
3.) The third pattern is a suffix number sequence pattern. 1-9 are only one digit, no “0” padding desired.
"filename (#).ext"
Apple (1).ext
Apple (2).ext
Apple (3).ext
Apple (10).ext
Apple (11).ext
Thank You for any help provided
2
Answers
Thank you again for your response, I am unfamiliar with some of the coding choices so I took what little I do know and combined it with some of the items from your code and was able to generate working commands for both 2.) & 3.). Anyone viewing this in the future just keep in mind that they seem to work but may cause unknown issues. Figured I would share because as mentioned I feel these could really be helpful to others as well.
I am using "Apple" as the example for filenames and ".ext" as example for file extension.
2.) Produces “Apple - ##.ext”
3.) Produces “Apple (#).ext”
If anyone has any advice to improve these, I am open to hearing them, but for now this is the best I can do, hopefully others will find this helpful in the future. Thanks again for your help to get me there.
Perl rename is quite strict about variable declarations, so saving state between each filename is not completely trivial.
The expression passed into
rename
is wrapped into a Perl subroutine using aneval
, and the subroutine is invoked on every filename. So usingmy $var
would result in a variable that is reset on each call.Using pre-existing global variables like
$a
or$b
are good for "one-liners" but if extra persistent variables are needed, or more complex initialisation, it turns out that simply declaring in aBEGIN
or usingour
is also possible.With that, here are some ideas:
The first two use Perl’s magic
++
on a string.An alternative would be to call
sprintf
. For example:Replace
.ext$
with suitable regex to match your definition of "extension".