Moving referenced images in a markdown file to another folder

The other day I was writing a brief report in multimarkdown which had several images and some code. After I finished the report I decided I would move the images from their original folder to another one. Since there were quite a large number of images, and not all of them were residing in the same directory I thought I’d write a small script.

The following is a bash script for extracting the image path from the markdown file and copying the files to another directory. The syntax for referencing an image in multimarkdown looks like this

[Set02-03]: ../../Documents/MATLAB/myproject/results/set02-03.png width="40%"

What I needed to do was grep the file for every line that has that type of expression. This wouldn’t be possible without the use of regular expressions. The grep -o command with the regular expression '\..*png' greps the file and prints out


I then use this to copy every image file to another directory. The following one-liner script does it all:

 for f in `grep -o '\..*png' myreport.txt`; do cp $f ../Public/myproject/ ; done 

Bear in mind that if your images are referenced in a relative path like the one shown here, you have to run the script from the directory where the markdown text file resides. It should also work for LaTeX files.


If you liked this post please leave a comment or consider subscribing.

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s