← Blog

Bash: Rename specific files in a folder

October, 2019

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?


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
  # rename (by variable expansion starting from position 11)
  mv "$file" "${file:11}"

Further Reading


Hi! I'm Michael 👋 I'm a problem solver and mentor. My goal is to build great products and help you to become a better developer.