Batch Renaming image file in .md file to just contain the filename

annie spratt oLbyiENtuGs unsplash scaled
Genx Avatar

Go to root folder of your Hugo project. Use below code on terminal. Make sure you back up your content folder first.

find content/posts -name '*.md' -exec perl -pi -E 's/(image\s*=\s*")([^"]*\/)([^\/"]+\.(jpg|jpeg|png|webp|gif))(")/$1$3$5/g' {} +

For other folders (custom post type) within content folder, use below. Replace foldername with the real folder name.

find content/foldername -name '.md' -exec perl -pi -E 's/(image\s=\s")([^"]\/)([^\/"]+.(jpg|jpeg|png|webp|gif))(")/$1$3$5/g' {} +

Share This Post:


Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

16 + eight =

Webmention: Have you posted a response to this article? Let me know the URL: