89

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?

2
  • 2
    Essentially the same as many other questions - such as SO 1086502 (stackoverflow.com/questions/1086502). There was one asked yesterday, even. Commented Dec 25, 2009 at 15:14
  • I disagree that this is a duplicate per se. I wanted to use a regex capture, as I had filenames like blah_blah_15_blah_blah_948ABCD.txt and the important part for me was the first number, whereas the second number was some kind of checksum. The capture is shown at stackoverflow.com/a/1961273/1168342 Commented Sep 20, 2020 at 17:00

4 Answers 4

141

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.

Sign up to request clarification or add additional context in comments.

8 Comments

It may be called prename on some systems.
much less to remember / type, thanks! rename 's/_20/ /g' *.mp4
also available on mac via homebrew brew install rename
I got here because I wanted to use a capture $1 in the destination name, to rename files like blah_blah_15_blah_blah_948ABCD1.txt to 15.txt via 's/blah_blah(\d\d)blah_blah([0-9A-F]{8})/$1/'
To be clear, it's the rename from pstray, not util-linux. Many systems have the latter installed by default, but not the former. This got me confused for a few minutes into thinking that it's some kind of weirdly under-documented tool.
|
73

Try this:

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

9 Comments

here's what it returns: usage: mv [-f | -i | -n] [-v] source target mv [-f | -i | -n] [-v] source ... directory
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
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
@justsomebody: simply use IFS=$'\n'; in front of your line then
Isn't that using globs and not regex?
|
63
$ for old in ./images*.png; do
    new=$(echo $old | sed -e 's/\.png$/test.png/')
    mv -v "$old" "$new"
  done

6 Comments

I like the use of sed since it allows me to replace this with whatever I like. in my case replace "%20" with " ". I used the 'g' at the end of the regex to allow that: for old in *.pdf; do new=$( echo $old | sed -e 's/\%20/ /g'); mv $old "$new";done
Thanks! I derived the one-liner mv file_20name.mp4 "$(echo file_20name.mp4 | sed -e 's/_20/ /g')" from this
beautiful as this allows full power of sed regex syntax. Great combination of commands, and good prototype for many other bash scripts.
This worked beautifully for what I needed (removing underscores from file names). for old in `ls`; do new=$(echo $old | sed -e 's/_//g'); mv "$old" "$new"; done
I looked at the docs for -e flags but don't understand what it does
|
6

Yet another solution would be a tool called mmv:

mmv "./images/*.png" "./test/#1test.png"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.