I have a directory of markdown files that I'm trying to accomplish the following with:
- Grab the filename of the markdown file and store it in a variable
- Take that variable and replace a series of strings in the file with the stored filename variable
- loop through all of the files in the directory and do the same thing
I'm close, but the following code is pulling out the filename of only the first markdown file and applying the variable to all strings in the files. Here's my working code so far:
#!/bin/bash
for file in /home/user/dir/*; do
str="somestring"
filename=$(basename $file)
fn="$(echo "${filename%.*}")"
find ./ -type f -exec sed -i '' -e "s/${str}/${fn}/g" {} \;
done
Assuming that the markdown file looks like this:
123456789.md
and is at /home/user/dir/123456789.md
with several other .md files with other, random numerical names.
Structure of the .md files is similar to:
---
layout: default
date: 2010-03-28
original: /orig/somestring.jpg
thumbnail: /thumb/somestring_thumb.jpg
permalink: /images/somestring/
---
and my goal would be for the script to make each file look like this, based on the filename of the .md file itself:
---
layout: default
date: 2010-03-28
original: /orig/123456789.jpg
thumbnail: /thumb/123456789_thumb.jpg
permalink: /images/123456789/
---
Any thoughts on the best way to edit the sed call, or another way to write this? Occasionally in my testing, sed was returning sed: RE error: illegal byte sequence
, but was going through with the rename of the string anyway, even if it was the wrong string.