There are times when you need to rename multiple files at once. For example, you might want to rename a file named “MyFile1” to “MyFile2” and a file named “MyFile3” to “MyFile4”. To rename multiple files at once, use the following command: Rename-file -name ‘MyFile1’ -name ‘MyFile2’ -name ‘MyFile3’


The “for” command works like this:

You can replace  with any file match pattern, such as * or *.txt, and you can replace with any linux command. The command will be run in sequence on each of the files matched by the file match pattern.

This is where the bash variable handling makes it even more interesting. Instead of just doing something like “mv $var”, we can replace text in the filename using this syntax:

So now, if we run this command on our directory:

For each file matched by *, bash will execute a command similar to this:

I’ve found that knowledge of the shell is invaluable when administering servers or just for managing your file collection, and has saved me hours of what would have otherwise been manual work.

And yes, I realize there are a number of tools that can accomplish renaming of multiple files.