Problem
As I try to write a script to rename massive files in accordance with some regular expression requirement, the command works fine on my iTerm2, but the same command does not work in the script.
Plus, some of my file names include some Chinese and Korean characters. (don't know if this is a problem or not)
code
So, my code accepts three inputs: the old regular expression, the new regular expression, and the files that need to be renamed.
There is no code here:
#!/bin/bash
if [ $# -lt 3 ] ; then
cat << HELP
ren -- renames a number of files using sed regular expressions USAGE: ren 'regexp'
'replacement' files...
EXAMPLE: rename all *.HTM files into *.html:
ren 'HTM' 'html' *.HTM
HELP
exit 0
fi
OLD="$1"
NEW="$2"
shift
shift
for file in "$@"; do
if [ -f "$file" ] ; then
newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`
if [ -f "$newfile" ]; then
echo "ERROR: $newfile exists already"
else
echo "renaming $file to $newfile ..."
mv "$file" "$newfile"
fi
fi
done
I will register the bash command in .profile as:
alias ren="bash /pathtothefile/ren.sh"
Test
The original file name is "제 01 과 .mp3" and I want it to become "第 01 课 .mp3".
So, with my script, I use:
$ ren "제\([0-9]*\)과" "第\1课" *.mp3
, sed script .
, , :
$ echo "제01과.mp3" | sed s/"제\([0-9]*\)과\.mp3"/"第\1课\.mp3"/g
?
script, :
newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`
echo "The ${file} is changed to ${newfile}"
:
The 제01과.mp3 is changed into 제01과.mp3
ERROR: 제01과.mp3 exists already
, .
( bash 4.2.45 (2), Mac OS 10.9)
bash . for. - . files.txt, :
$ ls | grep mp3 > files.txt
-. bash, :
$ file="제01과.mp3"
$ echo $file | sed s/"제\([0-9]*\)과\.mp3"/"第\1课\.mp3"/g
第01课.mp3
:
files=`cat files.txt`
for file in $files
do
echo $file | sed s/"제\([0-9]*\)과\.mp3"/"第\1课\.mp3"/g
done
!
:
echo $file
:
$ 제30과.mp3
( 30)
, :
$ echo $file | sed s/"제\([0-9]*\)과\.mp3"/"第\1课\.mp3"/g
:
$ 제30과.mp3
, :
$ newfile="제30과.mp3"
$ echo $newfile | sed s/"제\([0-9]*\)과\.mp3"/"第\1课\.mp3"/g
:
$第30课.mp3
WOW ORZ... ! ! ! , , , , :
if [[ $file == $new ]]; then
echo True
else
echo False
fi
:
False
, , , - ? .
2
, , . , :
file="제30과.mp3"
script, sed . , $@ :
file=./*mp3
sed . . btw, mac sed -r, ubuntu -r , .