Rename multiple files based on pattern in Unix Rename multiple files based on pattern in Unix unix unix

Rename multiple files based on pattern in Unix


There are several ways, but using rename will probably be the easiest.

Using one version of rename:

rename 's/^fgh/jkl/' fgh*

Using another version of rename (same as Judy2K's answer):

rename fgh jkl fgh*

You should check your platform's man page to see which of the above applies.


This is how sed and mv can be used together to do rename:

for f in fgh*; do mv "$f" $(echo "$f" | sed 's/^fgh/jkl/g'); done

As per comment below, if the file names have spaces in them, quotes may need to surround the sub-function that returns the name to move the files to:

for f in fgh*; do mv "$f" "$(echo $f | sed 's/^fgh/jkl/g')"; done


rename might not be in every system. so if you don't have it, use the shellthis example in bash shell

for f in fgh*; do mv "$f" "${f/fgh/xxx}";done