Skip to content

Built in variables

Ayooluwa edited this page May 5, 2021 · 29 revisions

F2 provides several built-in several variables that can be utilised in the replacement string.

File name and extension

These variables help you access the original filename before replacement. They can come in handy if you want to add a prefix or a suffix to a set of files.

  • {{f}}: the original filename (excluding the extension).
  • {{p}}: the parent directory name.
  • {{ext}}: the file extension (including the .).

Example

Assuming the following directory structure:

house-chores/
├── cleaning.md
├── laundry.md
├── mowing.md
└── washing.md

A renaming operation may be performed by prefixing the directory's name into the new names:

$ f2 -r '{{p}}_{{f}}{{ext}}'
+-------------+--------------------------+--------+
|    INPUT    |          OUTPUT          | STATUS |
+-------------+--------------------------+--------+
| cleaning.md | house-chores_cleaning.md | ok     |
| laundry.md  | house-chores_laundry.md  | ok     |
| mowing.md   | house-chores_mowing.md   | ok     |
| washing.md  | house-chores_washing.md  | ok     |
+-------------+--------------------------+--------+

Random string

The {{r}} variable is provided for the insertion of a random string in the new name. By default, it returns a random string of 10 letters but the output length and character set can be customised through the following syntax {{<number>r_<characters>}}. If the number is omitted, it defaults to 10. If the characters are omitted, it defaults to the letters a-z.

For example:

  • {{5r}}: Returns a random string of 5 letters.
  • {{15r_d}}: This returns a random string composed of 15 digits. The _d is a shorthand for digits 0-9.
  • {{8r_ld}}: Returns a random string composed of letters and digits (_ld is shorthand for letters a-z and digits 0-9).
  • {{r<$#@%^&>}}: Returns a random string of 10 characters composed only of $#@%^&.
$ f2 -f 'go' -r '{{15r_ld}}' -e
+--------+---------------------+--------+
| INPUT  |       OUTPUT        | STATUS |
+--------+---------------------+--------+
| go.mod | yuye4fwb76pk0pa.mod | ok     |
| go.sum | 3qshovztgiwoszz.sum | ok     |
+--------+---------------------+--------+

Note that the random string you see in the preview is not going to match what is committed to the filesystem as it will be generated all over again when the operation is executed.

Date variables

The time attributes of a file can be used in the replacement string. F2 provides variables that enable you to access the file creation time, modification time, access time and more.

  • ctime: The time at which file metadata was changed.
  • btime: File birth time (Windows and macOS only).
  • atime: The last time the file was accessed or read.
  • mtime: The last time the contents of the file was modified.
  • now: The current time.

The above variables must be combined with a date token (see below)

Date tokens

Token Explanation Output
YYYY Year represented by a full four digits 1970 1971 ... 2029 2030
YY Year represented only by the last two digits 70 71 ... 29 30
MMMM Name of the month January February ... November December
MMM Abbreviated name of the month Jan Feb ... Nov Dec
MM Month as digits with leading zeros for single-digit months 01 02 ... 11 12
M Month as digits without leading zeros for single-digit months 1 2 ... 11 12
DDDD Name of the day of the week Monday Tuesday ... Saturday Sunday
DDD Abbreviated name of the day of the week Mon Tue ... Sat Sun
DD Day of the week as digit with leading zeros 01 02 ... 06 07
D Day of the week as digit without leading zeros 1 2 ... 6 7
H 24 Hours clock 01 02 ... 22 23
hh Hours with leading zeros for single-digit hours 01 02 ... 11 12
h Hours without leading zeros for single-digit hours 1 2 ... 11 12
mm Minutes with leading zeros for single-digit minutes 01 02 ... 58 59
m Minutes without leading zeros for single-digit minutes 1 2 ... 58 59
ss Seconds with leading zeros for single-digit seconds 01 02 ... 58 59
s Seconds without leading zeros for single-digit seconds 1 2 ... 58 59
A AM PM AM PM
a am pm am pm

Example

The last modified time of each file is being used in the new name. Notice how the date tokens helps with formatting the output:

$ f2 -f 'screenshot' -r '{{mtime.MMM}}-{{mtime.DD}}-{{mtime.YYYY}}-screenshot'
+----------------+----------------------------+--------+
|     INPUT      |           OUTPUT           | STATUS |
+----------------+----------------------------+--------+
| screenshot.jpg | Nov-08-2020-screenshot.jpg | ok     |
| screenshot.png | Mar-04-2021-screenshot.png | ok     |
+----------------+----------------------------+--------+

Exif variables

Note that exif attributes only work for image files such as JPEG, DNG, and most other RAW image formats (CR2, NEF, RAF, ARW, e.t.c.). HEIC/HEIF format is not supported at the moment.

The exif attributes of an image file can be used in its replacement string. F2 provides variables that enable you to access the ISO, width, height, created date, aperture, model, make, dimensions, focal length and exposure time for images. Each variable can be used like this: {{exif.<var>}} or {{x.<var>}} as in {{exif.iso}} or {{x.make}}. If an exif attribute is not present in the image file, the corresponding variable will be replaced with an empty string.

Currently supported variables:

  • iso: The ISO at which the image was captured.
  • w: The image width.
  • h: The image height.
  • model: The camera model (e.g. Canon EOS 5D Mark III).
  • make: The camera maker (e.g. Canon).
  • lens: The lens model.
  • et: The exposure time (e.g. 1/400) which is outputted as 1_400.
  • wh: The image dimensions (e.g 4032x3024).
  • fnum: The aperture (e.g. 1.6).
  • fl: The focal length of the lens (e.g 52)
  • dt: The image creation date. This must be combined with a date token (e.g {{exif.dt.YYYY}}).

Example

It might be handy to utilise exif variables when organising your photography collection:

$ f2 -r '{{x.model}}-{{x.fl}}mm-ISO{{x.iso}}-{{x.make}}-{{f}}{{ext}}' -E 'json'
+-----------------+------------------------------------------------------------+--------+
|      INPUT      |                           OUTPUT                           | STATUS |
+-----------------+------------------------------------------------------------+--------+
| bike.jpeg       | SM-G975F-4.32mm-ISO50-samsung-bike.jpeg                    | ok     |
| leica_m8.dng    | M8 Digital Camera-50mm-ISO160-Leica Camera AG-leica_m8.dng | ok     |
| nikon_d3100.nef | NIKON D3100-18mm-ISO1800-NIKON CORPORATION-nikon_d3100.nef | ok     |
| proraw.dng      | iPhone 12 Pro Max-5.1mm-ISO32-Apple-proraw.dng             | ok     |
| sony_rx100.arw  | DSC-RX100-10.4mm-ISO640-SONY-sony_rx100.arw                | ok     |
| tractor-raw.cr2 | Canon EOS 5D Mark III-24mm-ISO200-Canon-tractor-raw.cr2    | ok     |
+-----------------+------------------------------------------------------------+--------+

ID3 variables

ID3 attributes from audio formats such as MP3, FLAC, OGG, M4A, ACC e.t.c can be accessed via the following ID3 variables:

  • title: The title of the track.
  • artist: The track artist.
  • album_artist: The album artist.
  • album: The album name.
  • format: The file format (e.g VORBIS, ID3v2.3).
  • type: The file type (e.g MP3, OGG, FLAC).
  • year: The release year.
  • track: The track number.
  • total_tracks: The total number of tracks in the album.
  • disc: The disc number.
  • total_discs: The total number of discs in the album.

Utilising each variable is as simple inserting the {{id3.<var>}} in the replacement string. Here's an example that uses ID3 attributes to organise the tracks in Blurryface into the following directory structure artist/album/title:

$ f2 -f '(\d+).*' -r '{{id3.artist}}/{{id3.album}}/$1 {{id3.title}}{{ext}}'
+--------------------------------------+-------------------------------------------------------------------+--------+
|                INPUT                 |                              OUTPUT                               | STATUS |
+--------------------------------------+-------------------------------------------------------------------+--------+
| 01 Heavydirtysoul.m4a                | twenty one pilots/Blurryface/01 Heavydirtysoul.m4a                | ok     |
| 02 Stressed Out.m4a                  | twenty one pilots/Blurryface/02 Stressed Out.m4a                  | ok     |
| 03 Ride.m4a                          | twenty one pilots/Blurryface/03 Ride.m4a                          | ok     |
| 04 Fairly Local.m4a                  | twenty one pilots/Blurryface/04 Fairly Local.m4a                  | ok     |
| 05 Tear In My Heart.m4a              | twenty one pilots/Blurryface/05 Tear in My Heart.m4a              | ok     |
| 06 Lane Boy.m4a                      | twenty one pilots/Blurryface/06 Lane Boy.m4a                      | ok     |
| 07 The Judge.m4a                     | twenty one pilots/Blurryface/07 The Judge.m4a                     | ok     |
| 08 Doubt.m4a                         | twenty one pilots/Blurryface/08 Doubt.m4a                         | ok     |
| 09 Polarize.m4a                      | twenty one pilots/Blurryface/09 Polarize.m4a                      | ok     |
| 10 We Don't Believe What's On TV.m4a | twenty one pilots/Blurryface/10 We Don't Believe What's on TV.m4a | ok     |
| 11 Message Man.m4a                   | twenty one pilots/Blurryface/11 Message Man.m4a                   | ok     |
| 12 Hometown.m4a                      | twenty one pilots/Blurryface/12 Hometown.m4a                      | ok     |
| 13 Not Today.m4a                     | twenty one pilots/Blurryface/13 Not Today.m4a                     | ok     |
| 14 Goner.m4a                         | twenty one pilots/Blurryface/14 Goner.m4a                         | ok     |
+--------------------------------------+-------------------------------------------------------------------+--------+

If a specified ID3 tag is not present in the audio file, the corresponding variable will be replaced with an empty string.

File hashes

F2 supports the use of file hashes such as md5, sha256, sha1, and sha512 in the replacement string. All you need to do is add the {{hash.<var>}} variable in the replacement string.

$ f2 -f 'go' -r '{{hash.md5}}_{{hash.sha1}}_{{f}}' -e
+--------+----------------------------------------------------------------------------------+--------+
| INPUT  |                                      OUTPUT                                      | STATUS |
+--------+----------------------------------------------------------------------------------+--------+
| go.mod | 410755ff3eede8293b6e7536c346efe5_d6c801d7174304b41624263eb207b3a316a3890a_go.mod | ok     |
| go.sum | 665d2776064448f882a592814ac62447_43c73785e3c06b2b06b857dd600d5ffabdc81eb3_go.sum | ok     |
+--------+----------------------------------------------------------------------------------+--------+
Clone this wiki locally