Tips & Tricks : การ Rename ไฟล์ด้วย command-line ทีละหลายๆไฟล์

Rename files on the command line

This fella on a mailing list I’m on wants to rename files (specifically, .mpe to .mpg) on his Mac OS X box using the command line, but doesn’t know how. The great Robert Citek to the rescue! Here’s his suggestion:

for file in *.mpe ; do mv $file ${file%.mpe}.mpg ; done

However, Robert then went on to make this interesting suggestion: “Personally, I like to echo the command and proof it. If I like it I can then pipe it into bash. That allows you to catch the case when there are several .mpg files and no .mpe files.”

for file in *.mpe ; do echo mv $file ${file%.mpe}.mpg; done | bash

Great idea, Robert! Thanks!

Oh, and by the way: this same technique works just fine and dandy on a Linux box as well.



Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out /  เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out /  เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out /  เปลี่ยนแปลง )


Connecting to %s