Batch Rename One-Liner

Following is a simple way to perform batch renaming, using the Command line:

ls *.jpg | awk '{print("mv "$1" "$1)}' | sed 's/jpg/jpeg/' | /bin/sh

If you wish to see the renaming commands before they are performed (just to make sure it does what you want it to do), then simply omit the last pipe:

ls *.jpg | awk '{print("mv "$1" "$1)}' | sed 's/jpg/jpeg/'

This will show you what it is going to do, without actually doing it.

By the way, the same technique can be used for other purposes. For example the following example copies the files foo.jpg, bar.jpg and glu.jpg to subdirectory temp:

echo foo,bar,glu | perl -pi -e 's/,/\n/g;s/.+/cp $&.jpg temp\//g' | /bin/sh

 

This entry was posted in Unix Tidbits. Bookmark the permalink.

Leave a Reply

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