Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

Rename Files Downloaded Using youtube-dl

June 10, 2023 — Gregg Szumowski

If you have used youtube-dl to download content from YouTube then you know that the file names can get to be rather long. In addition to the video’s title there is a dash followed by what appears to be a random sequence of characters just before the file extension. If you find this to be annoying then here is a code snippet that you can use to remove it.

This will remove the last field separated by a ‘-’ and replace it with the file extension of your choice, .mp4 is used in this example:

#!/bin/bash
for i in *.mp4
do
# Remove everything from the last hyphen to the end
file="${i%-*}"
# Rename the file and remove the space between 'file'
# and the file extension on the fly
mv "${i}" "${file%%*( )}.mp4"
done

Tags: cli, youtube, renaming, motd