tags:

views:

544

answers:

4

Hi everyone!

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 is not working.

Can anyone suggest me a solution?

Thanks in advance!

+6  A: 

Try this:

for x in *.png;do mv $x test/${x%.png}test.png;done
catwalk
here's what it returns:usage: mv [-f | -i | -n] [-v] source target mv [-f | -i | -n] [-v] source ... directory
mxg
@mxg: sorry, I did not give 100% correct command: you need `cd images` first and prepend `test/` with `..`
catwalk
You should put quotes around the variable names in case there are spaces in the filenames: `for x in *.png; do mv "$x" "test/${x%.png}test.png"; done`
Dennis Williamson
It works, but it leaves one file in the folder.@catwalk, you're not paid to give solutions, but 'paid' to give good indications:)Thanks!
mxg
actually, if the filenames contain spaces, you're screwed with `for` with or without quotes. that's why it's better to use the *generator | while read line; do something with "$line"; done* idiom. in this case: `ls | grep '\.png$' | while read x; do ... ; done`
just somebody
It is very useful for me now!
mxg
+5  A: 

If you are on a linux, check special rename command which would do just that - wildcard renaming. Otherwise, write a bash cycle over the filenames as catwalk suggested.

kibitzer
It may be called `prename` on some systems.
Dennis Williamson
+2  A: 
$ for old in ./images*.png; do
    new=$(echo $old | sed -e 's/\.png$/test.png/')
    mv -v "$old" "$new"
  done
Greg Bacon
A: 

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

koeckera