Anna's notepad

Editing filenames using bash

These command will affect all the files in whichever folder you're currently in (inside bash).

Remove string from all filenames

I decided to add this to my online notepad because I use it a lot. When ripping CDs or buying mp3s, the tracks often come out with the artist name and album title in every single filename, which I don't want. Instead of editing them all one-by-one, I looked into how to use bash to do it for me.

for file in *; do mv "${file}" "${file/STRING_TO_REMOVE /}"; done

Change all spaces to underscores

I decided to add this to my online notepad because it's such a simple thing to want to do, but trying to look it up often returns excessively complex solutions (using Perl, for example). It's doable with one mv command.

for file in *; do mv "$file" `echo $file | tr ' ' '_'`; done

Add a prefix to all filenames

for file in * ; do mv -- "$file" "PREFIX$file" ; done

#bash shell