Skip to main content

Batch Renaming Files with f2

·3 mins

f2 is very powerful tool to rename multiple files at once. It’s written in Go. You don’t have to worry about dependencies. Just get the binary here and you’re good to go.

f2 is not just for basic renaming stuff. It can do way more than that. Read its wiki.

The search and replace pattern will mosty revolve around regex. If it’s a small pattern, you can disable regex by specifying -s for string mode. I’ll be using regex for this brief tutorial.

Here’s my list of dummy files:

y2meta.com - Alive (Lyric Video) - Hillsong Young & Free (320 kbps).mp3
y2meta.com - Citizen Way - Bulletproof (Official Lyric Video) (320 kbps).mp3
y2meta.com - Jordan Feliz - The River (Official Lyric Video) (320 kbps).mp3
y2meta.com - Unspoken - Good Fight (Official Lyric Video) (320 kbps).mp3

I want the following words and characters to be removed:

y2meta.com -
(Lyric Video)
(Official Lyric Video)
(320 kbps)

The 4 major flags are:

  • -f: Find patterns
  • -r: Found patterns which should be replaced with
  • -u: Undo last operation performed
  • -x: Execute the operation. By default, it’s a dry-run.

To only strip out unwanted text, you just need to find the right patterns -f. My regex is:

'y2meta.com - | \((.*?)\)'

The \((.*?)\) will match pair of parenthesis and remove everything inside. I used grep for readability:

MacBook-Pro:kavish$ f2 -f 'y2meta.com - | \((.*?)\)' | grep -vi "^+" | cut -d "|" -f 3 | grep -vi info
              OUTPUT
 Alive - Hillsong Young & Free.mp3
 Citizen Way - Bulletproof.mp3
 Jordan Feliz - The River.mp3
 Unspoken - Good Fight.mp3
MacBook-Pro:fun_with_f2 kavish$

Looks good. To perform the operation, use -x:

MacBook-Pro:kavish$ f2 -f 'y2meta.com - | \((.*?)\)' -x > /dev/null
MacBook-Pro:kavish$
MacBook-Pro:kavish$ ls *.mp3
Alive - Hillsong Young & Free.mp3 Jordan Feliz - The River.mp3
Citizen Way - Bulletproof.mp3     Unspoken - Good Fight.mp3

You can -u to see how it works:

MacBook-Pro:kavish$ f2 -u | grep -vi "^+" | cut -d "|" -f 3 | grep -vi info
                                    OUTPUT
 y2meta.com - Alive (Lyric Video) - Hillsong Young & Free (320 kbps).mp3
 y2meta.com - Citizen Way - Bulletproof (Official Lyric Video) (320 kbps).mp3
 y2meta.com - Jordan Feliz - The River (Official Lyric Video) (320 kbps).mp3
 y2meta.com - Unspoken - Good Fight (Official Lyric Video) (320 kbps).mp3

Again, use -x to commit the changes:

MacBook-Pro:kavish$ f2 -u -x > /dev/null
MacBook-Pro:kavish$
MacBook-Pro:kavish$ ls *.mp3
y2meta.com - Alive (Lyric Video) - Hillsong Young & Free (320 kbps).mp3
y2meta.com - Citizen Way - Bulletproof (Official Lyric Video) (320 kbps).mp3
y2meta.com - Jordan Feliz - The River (Official Lyric Video) (320 kbps).mp3
y2meta.com - Unspoken - Good Fight (Official Lyric Video) (320 kbps).mp3

I think it’s a pretty nice tool to add to your toolset.

“Darkness cannot drive out darkness: only light can do that. Hate cannot drive out hate: only love can do that.”Martin Luther King Jr.