Batch renaming using shell script Batch renaming using shell script unix unix

Batch renaming using shell script


That is because bash for split the element with space ' ' so you are commanding it to move 'input' to '(1)'.

The way to solve this is to tell bash to split by new line using IFS variable.

Like this:

IFS=$'\n'

Then do your command.

However, I suggest you to use find to do this instead using -exec command.

For example:

find *.txt -exec mv "{}" `echo "{}" | sed -e 's/input\ (\([0-9]*\))\.txt/input_\1.in/'` \;

NOTE: I write this from memory and I did test this so let try and adjust it.

Hope this helps.


You're forgetting to quote your arguments.

... mv "$f" "$(echo "$f" | ... )" ; done


no need to call external commands

#!/bin/bashshopt -s nullglobshopt -s extglobfor file in *.txtdo  newfile="${file//[)]/}"  newfile="${file// [(]/_}"  mv "$file" "${newfile%.txt}.in"done