Rename files using a regex with bash

RegexBash

Regex Problem Overview


> Possible Duplicate:
> rename multiple files at once in unix

I would like to rename all files from a folder using a regex (add a name to the end of name) and move to another folder.

It my opinion, it should be looking like this:

mv -v ./images/*.png ./test/*test.png

but it does not work.

Can anyone suggest me a solution?

Regex Solutions


Solution 1 - Regex

If you are on a linux, check special rename command which would do just that - renaming using regular expressions.

rename 's/^images\/(.+)/test\/$1.png/s' images/*.png

Otherwise, write a bash cycle over the filenames as catwalk suggested.

Solution 2 - Regex

Try this:

for x in *.png;do mv $x test/${x%.png}test.png;done

Solution 3 - Regex

$ for old in ./images*.png; do
    new=$(echo $old | sed -e 's/\.png$/test.png/')
    mv -v "$old" "$new"
  done

Solution 4 - Regex

Yet another solution would be a tool called "mmv": mmv "./images/*.png" "./test/#1test.png"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmxgView Question on Stackoverflow
Solution 1 - RegexkibitzerView Answer on Stackoverflow
Solution 2 - RegexcatwalkView Answer on Stackoverflow
Solution 3 - RegexGreg BaconView Answer on Stackoverflow
Solution 4 - RegexkoeckeraView Answer on Stackoverflow