Bash: rename all files in directory, replacing all hyphens (-) with underscores (_)

I don’t want to waste time doing things manually when I can run a script that will help me save time and effort.

When using Linux or Mac command line (bash?), if you want to rename all files in your present directory and replace all hyphens - with underscores _, run this command:

And if you want to do the reverse, i.e., replace all underscores _ with hyphens -, run this command instead:

To remove substring from file names, (e.g., remove @3x string), run this:

I had to do this after importing image files with hyphens in their names into my Android project. I can’t use images with hyphens in their names.

Catzie

A Filipino who is a mobile & web developer on weekdays, and a baker on weekends. When I have time to spare, I blog, play video games, watch series (usually anime), cook dishes, draw, or spend a little time with pets. I also like traveling and recording song covers, but I only do so occasionally. Follow on Twitter: @catzie

Leave a Reply

Your email address will not be published. Required fields are marked *