David McCracken - 2019-02-28

This example is from https://unix.stackexchange.com/questions/40523/rename-files-by-incrementing-a-number-within-the-filename. The question is how to increment a numeric fragment in the name of multiple files but only those whose fragment is above a certain value. The purpose of this is to enable inserting a new file into the sequence. The files have different extensions. e.g. 01.png, 02.png, 03.png, 03.svg, 04.png, 05.png, 06.jpg, 07.png, 08.png, 09.png, 09.svg, 10.png and there may be as many as 80 in the directory. This is under Fedora-Bash. rene's bump rule and range filter are both needed for this. The name collision that would occur on every file could be avoided by bumping up 101 and then down 100 but the range would also have to be changed. An easier solution is to bump up by 1 while adding a unique character that can be removed by a simple command applied to all files. For example, a two-step solution to opening the name space at 07 is:

rene ?.*/#7-80  %?.* B
rene %* *

/#7-80 is a "filter extension". Each filter extension is applied to the corresponding ? in the filter. If no filter extensions are defined, ? in the filter means any single character. A complete filter extension specifies width (character count) and semantic filter. If only the filter is specified, as in this example, the width defaults to any. 7-80 specifies the range of accepted values. The leading # defines this as numeric. Ranging is lexical (e.g. '9' > '10') by default even if the characters are digits.
B is a bump rule applied to the ? in the replacement. By default the bump is +1 but it can be any positive or negative value. When applied to a numeric fragment, bump retains leading 0s and propagates carry through the entire fragment, doing everything that is needed in this case with relatively simple syntax.

 

Last edit: David McCracken 2019-02-28