-
Notifications
You must be signed in to change notification settings - Fork 0
Built in variables
F2 provides several built-in several variables that can be utilised in the replacement string.
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.
).
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 |
+-------------+--------------------------+--------+
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
\dis a shorthand for digits
0-9`. -
{{8r\ld}}
: Returns a random string composed of letters and digits (\ld
is shorthand for lettersa-z
and digits0-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.
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 (seen below)
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 |
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 |
+----------------+----------------------------+--------+
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/400s). -
wh
: The image dimensions (e.g 4032x3024). -
fnum
: The aperture (e.g. f/1.6). -
fl
: The focal length of the lens (e.g 52mm) -
dt
: The image creation date. This must be combined with a date token (e.g{{exif.dt.YYYY}}
).
It might be handy to utilise exif variables when organising your photography collection:
$ f2 -r '{{exif.model}}_{{exif.fl}}_{{exif.et}}_{{exif.wh}}_{{f}}{{ext}}'
+-----------------+-------------------------------------------------------------+--------+
| INPUT | OUTPUT | STATUS |
+-----------------+-------------------------------------------------------------+--------+
| bike.jpeg | SM-G975F_4.32mm_1_100s_4032x3024_bike.jpeg | ok |
| proraw.dng | iPhone 12 Pro Max_5.1mm_1_121s_4032x3024_proraw.dng | ok |
| tractor-raw.cr2 | Canon EOS 5D Mark III_24mm_1_125s_5760x3840_tractor-raw.cr2 | ok |
+-----------------+-------------------------------------------------------------+--------+
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 |
+--------------------------------------+-------------------------------------------------------------------+--------+