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

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

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?

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


miku86

Hi! I'm Michael 👋 I'm a Mentor & Educator & Senior Web Developer - I help you to reach your (career) goals.