Skip to content

Indexing

Ayooluwa Isaiah edited this page Feb 2, 2023 · 7 revisions

Renaming with an index

F2 supports indexing using an auto incrementing number. This makes it easy to product output such as log001.txt, log002.txt, log003.txt e.t.c. The indexing format can be as simple or as complex as you want.

It also supports several optional configurations such as step, padding with zeros, numbers to skip, and where the index should begin. The complete syntax is {<start>%<padding>d<format><step><skip>} where everything enclosed in <> is optional.

Here are some forms of indexing that can be used:

  • %d: The most basic way to rename with an index. Yields: 1, 2, 3, 4, e.t.c.
  • 5%d: 5 is the start number so this yields: 5, 6, 7, 8, e.t.c.
  • $1%d: $1 represents a capture variable (must be a number). The number doesn't auto increment here except when an explicit <step> is used (see example).
  • 5%02d: 02 here is the minimum width of the number. The number will be padded with zeros to reach this width so: 05, 06, 07, 08, e.t.c. Note that padding does not affect other number systems such as Roman numerals, octal, binary and hexadecimal formats.
  • %02dr: r signifies Roman numerals so we get: "I", "II", "III", "IV", e.t.c. This works well up to 3999 (MMMCMXCIX). From 4000 and above, the number will be in decimal format.
  • %db: b signifies binary format: 1, 10, 11, 100, e.t.c. Similarly, the h in %dh signifies hexadecimal formal and the o in %do signifies octal format.
  • 10%03d5: The 5 here signifies the step used to increment the numbers so this yields: 010, 015, 020, 025, e.t.c.
  • %d<1-5;7-8;11>: The numbers between the angle brackets signify what numbers are to be skipped during the indexing. This yields: 6, 9, 10, 12, e.t.c.

Examples

Index with padding

f2 -r "unsplash-image-{%03d}" -e
┌──────────────────────────────────────────────────────────────────────────────────────┐
| ORIGINAL                                          | RENAMED                 | STATUS |
| ************************************************************************************ |
| kevin-wong-tt7aK2yV7xo-unsplash.jpg               | unsplash-image-001.jpg  | ok     |
| nathan-anderson-7TGVEgcTKlY-unsplash.jpg          | unsplash-image-002.jpg  | ok     |
| photo-1434907652076-85f8401482c3.jpg              | unsplash-image-003.jpg  | ok     |
| photo-1510272839903-5112a2e44bc6.jpg              | unsplash-image-004.jpg  | ok     |
| photo-1521579971123-1192931a1452.jpg              | unsplash-image-005.jpg  | ok     |
| photo-1607234771207-e2a79ed7d818.jpeg             | unsplash-image-006.jpeg | ok     |
| photo-1655853473067-606e9c7a5a8e.jpeg             | unsplash-image-007.jpeg | ok     |
| samuele-errico-piccarini-t4OxCpKie70-unsplash.jpg | unsplash-image-008.jpg  | ok     |
| valentin-salja-VMroCCpP648-unsplash.jpg           | unsplash-image-009.jpg  | ok     |
└──────────────────────────────────────────────────────────────────────────────────────┘

Index in roman numerals starting from 10

f2 -r "unsplash-image-{10%03dr}" -e
┌───────────────────────────────────────────────────────────────────────────────────────┐
| ORIGINAL                                          | RENAMED                  | STATUS |
| ************************************************************************************* |
| kevin-wong-tt7aK2yV7xo-unsplash.jpg               | unsplash-image-X.jpg     | ok     |
| nathan-anderson-7TGVEgcTKlY-unsplash.jpg          | unsplash-image-XI.jpg    | ok     |
| photo-1434907652076-85f8401482c3.jpg              | unsplash-image-XII.jpg   | ok     |
| photo-1510272839903-5112a2e44bc6.jpg              | unsplash-image-XIII.jpg  | ok     |
| photo-1521579971123-1192931a1452.jpg              | unsplash-image-XIV.jpg   | ok     |
| photo-1607234771207-e2a79ed7d818.jpeg             | unsplash-image-XV.jpeg   | ok     |
| photo-1655853473067-606e9c7a5a8e.jpeg             | unsplash-image-XVI.jpeg  | ok     |
| samuele-errico-piccarini-t4OxCpKie70-unsplash.jpg | unsplash-image-XVII.jpg  | ok     |
| valentin-salja-VMroCCpP648-unsplash.jpg           | unsplash-image-XVIII.jpg | ok     |
└───────────────────────────────────────────────────────────────────────────────────────┘

Skip some numbers while indexing

f2 -r "unsplash-image-{%03d<1-5;10-20>}" -e
┌──────────────────────────────────────────────────────────────────────────────────────┐
| ORIGINAL                                          | RENAMED                 | STATUS |
| ************************************************************************************ |
| kevin-wong-tt7aK2yV7xo-unsplash.jpg               | unsplash-image-006.jpg  | ok     |
| nathan-anderson-7TGVEgcTKlY-unsplash.jpg          | unsplash-image-007.jpg  | ok     |
| photo-1434907652076-85f8401482c3.jpg              | unsplash-image-008.jpg  | ok     |
| photo-1510272839903-5112a2e44bc6.jpg              | unsplash-image-009.jpg  | ok     |
| photo-1521579971123-1192931a1452.jpg              | unsplash-image-021.jpg  | ok     |
| photo-1607234771207-e2a79ed7d818.jpeg             | unsplash-image-022.jpeg | ok     |
| photo-1655853473067-606e9c7a5a8e.jpeg             | unsplash-image-023.jpeg | ok     |
| samuele-errico-piccarini-t4OxCpKie70-unsplash.jpg | unsplash-image-024.jpg  | ok     |
| valentin-salja-VMroCCpP648-unsplash.jpg           | unsplash-image-025.jpg  | ok     |
└──────────────────────────────────────────────────────────────────────────────────────┘

Auto increment in steps of 5

f2 -r "unsplash-image-{%03d5}" -e
┌──────────────────────────────────────────────────────────────────────────────────────┐
| ORIGINAL                                          | RENAMED                 | STATUS |
| ************************************************************************************ |
| kevin-wong-tt7aK2yV7xo-unsplash.jpg               | unsplash-image-001.jpg  | ok     |
| nathan-anderson-7TGVEgcTKlY-unsplash.jpg          | unsplash-image-006.jpg  | ok     |
| photo-1434907652076-85f8401482c3.jpg              | unsplash-image-011.jpg  | ok     |
| photo-1510272839903-5112a2e44bc6.jpg              | unsplash-image-016.jpg  | ok     |
| photo-1521579971123-1192931a1452.jpg              | unsplash-image-021.jpg  | ok     |
| photo-1607234771207-e2a79ed7d818.jpeg             | unsplash-image-026.jpeg | ok     |
| photo-1655853473067-606e9c7a5a8e.jpeg             | unsplash-image-031.jpeg | ok     |
| samuele-errico-piccarini-t4OxCpKie70-unsplash.jpg | unsplash-image-036.jpg  | ok     |
| valentin-salja-VMroCCpP648-unsplash.jpg           | unsplash-image-041.jpg  | ok     |
└──────────────────────────────────────────────────────────────────────────────────────┘

Auto increment in steps of -2

f2 -r "unsplash-image-{20%03d-2}" -e
┌──────────────────────────────────────────────────────────────────────────────────────┐
| ORIGINAL                                          | RENAMED                 | STATUS |
| ************************************************************************************ |
| kevin-wong-tt7aK2yV7xo-unsplash.jpg               | unsplash-image-020.jpg  | ok     |
| nathan-anderson-7TGVEgcTKlY-unsplash.jpg          | unsplash-image-018.jpg  | ok     |
| photo-1434907652076-85f8401482c3.jpg              | unsplash-image-016.jpg  | ok     |
| photo-1510272839903-5112a2e44bc6.jpg              | unsplash-image-014.jpg  | ok     |
| photo-1521579971123-1192931a1452.jpg              | unsplash-image-012.jpg  | ok     |
| photo-1607234771207-e2a79ed7d818.jpeg             | unsplash-image-010.jpeg | ok     |
| photo-1655853473067-606e9c7a5a8e.jpeg             | unsplash-image-008.jpeg | ok     |
| samuele-errico-piccarini-t4OxCpKie70-unsplash.jpg | unsplash-image-006.jpg  | ok     |
| valentin-salja-VMroCCpP648-unsplash.jpg           | unsplash-image-004.jpg  | ok     |
└──────────────────────────────────────────────────────────────────────────────────────┘

Multiple indices in a filename

f2 -r "{10%05d}-unsplash-image-{%03dr}" -e
┌────────────────────────────────────────────────────────────────────────────────────────────┐
| ORIGINAL                                          | RENAMED                       | STATUS |
| ****************************************************************************************** |
| kevin-wong-tt7aK2yV7xo-unsplash.jpg               | 00010-unsplash-image-I.jpg    | ok     |
| nathan-anderson-7TGVEgcTKlY-unsplash.jpg          | 00011-unsplash-image-II.jpg   | ok     |
| photo-1434907652076-85f8401482c3.jpg              | 00012-unsplash-image-III.jpg  | ok     |
| photo-1510272839903-5112a2e44bc6.jpg              | 00013-unsplash-image-IV.jpg   | ok     |
| photo-1521579971123-1192931a1452.jpg              | 00014-unsplash-image-V.jpg    | ok     |
| photo-1607234771207-e2a79ed7d818.jpeg             | 00015-unsplash-image-VI.jpeg  | ok     |
| photo-1655853473067-606e9c7a5a8e.jpeg             | 00016-unsplash-image-VII.jpeg | ok     |
| samuele-errico-piccarini-t4OxCpKie70-unsplash.jpg | 00017-unsplash-image-VIII.jpg | ok     |
| valentin-salja-VMroCCpP648-unsplash.jpg           | 00018-unsplash-image-IX.jpg   | ok     |
└────────────────────────────────────────────────────────────────────────────────────────────┘

Indexing using a capture variable

f2 -f 'doc(\d+)' -r 'doc_{$1%03d}' -e
┌──────────────────────────────────┐
| ORIGINAL  | RENAMED     | STATUS |
| ******************************** |
| doc1.txt  | doc_001.txt | ok     |
| doc4.txt  | doc_004.txt | ok     |
| doc99.txt | doc_099.txt | ok     |
└──────────────────────────────────┘