Back

Bash: Rename specific files in a folder

Recently I’ve built a script to download all my articles from dev.to into markdown files.

I used the timestamp and the name of the article.

Alt Text

This is working, but there is one annoying thing: every post has a date in its file name and its frontmatter.

Alt Text

That leads to:

  • Increased complexity: Would have to change the date in both places if I would want to change the date.
  • What would be the source of truth in case of two different dates?

Problem

Alt Text

Desired Outcome

Alt Text

Let’s tackle this problem

# loop over every file with `md` extension, assign it to `file`
for file in *.md
do
  # rename (by variable expansion starting from position 11)
  mv "$file" "${file:11}"
done

Further Reading