Rename all the files in a directory to add a prefix

This is a bit off topic, but it is something I frequently want to do!

When I am producing a new post I usually end up with a number of images that I want to include.  I normally create these in PowerPoint, and then save them as PNG images.  This gives me a directory with a bunch of files called Slide1.png, Slide2.png, etc.

I like to add a prefix to make it easier and cleaner and ensure there are no name clashes, but how to do this quickly and easily?

This is the fastest was I have found to do this:

for file in $(echo *.png); do mv ${file} ubuntu.${file}; done

I run this in a terminal window, and it does the job, leaving me with a bunch of files call ubuntu.Slide1.png, ubuntu.Slide2.png, etc. (in this case).

About Mark Nelson

Mark Nelson is a Developer Evangelist at Oracle, focusing on microservices and AI. Mark has served as a Section Leader in Stanford's Code in Place program that has introduced tens of thousands of people to the joy of programming, he is a published author, a reviewer and contributor, a content creator and a lifelong learner. He enjoys traveling, meeting people and learning about foods and cultures of the world. Mark has worked at Oracle since 2006 and before that at IBM since 1994.
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a comment