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.
[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.