A bit optimized (without the useless
sed), parametric and verbose version:
Code:
test ~ $ cat img2iso.sh
#!/bin/bash
#VIDEO_ROOT_DIRECTORY="/Users/astrid/NFS/scripts/img2iso"
VIDEO_ROOT_DIRECTORY="/home/test/video"
IN_EXT=".img"
OUT_EXT=".iso"
for infile in $(find "$VIDEO_ROOT_DIRECTORY" -name "*${IN_EXT}" -type f)
do
echo "----------"
echo "Processing INFILE <$infile>"
n=0
exit=""
suffix=""
while [ ! "$exit" ]
do
newname="${infile%${IN_EXT}}${suffix}${OUT_EXT}"
if [ ! -f "$newname" ]
then
echo "NEW NAME OK! Renaming to <$newname>"
mv "$infile" "$newname"
exit="y"
else
echo "NEW NAME <$newname> already exists! Incrementing suffix"
n=$((n+1))
suffix="-$n"
fi
done
done
And the output before and after execution:
Code:
test ~ $ ls -lR /home/test/video
/home/test/video:
total 16
-rw-r--r-- 1 test test 0 Feb 17 12:00 aaa.img
-rw-r--r-- 1 test test 0 Feb 17 12:00 bbb.img
-rw-r--r-- 1 test test 0 Feb 17 12:00 bbb.iso
-rw-r--r-- 1 test test 0 Feb 17 12:00 ccc-1.iso
-rw-r--r-- 1 test test 0 Feb 17 12:00 ccc.img
-rw-r--r-- 1 test test 0 Feb 17 12:00 ccc.iso
-rw-r--r-- 1 test test 0 Feb 17 12:00 ddd-1.iso
-rw-r--r-- 1 test test 0 Feb 17 12:00 ddd.img
-rw-r--r-- 1 test test 0 Feb 17 12:00 eee.img
drwxr-xr-x 2 test test 4096 Feb 17 12:06 sub1
drwxr-xr-x 2 test test 4096 Feb 17 12:06 sub2
/home/test/video/sub1:
total 4
-rw-r--r-- 1 test test 0 Feb 17 12:06 fff.img
-rw-r--r-- 1 test test 0 Feb 17 12:06 fff.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 ggg-1.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 ggg-2.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 ggg.img
-rw-r--r-- 1 test test 0 Feb 17 12:06 ggg.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 hhh-1.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 hhh.img
/home/test/video/sub2:
total 4
-rw-r--r-- 1 test test 0 Feb 17 12:06 iii.img
-rw-r--r-- 1 test test 0 Feb 17 12:06 lll.img
-rw-r--r-- 1 test test 0 Feb 17 12:06 mmm.img
test ~ $ ./img2iso.sh
----------
Processing INFILE </home/test/video/ccc.img>
NEW NAME </home/test/video/ccc.iso> already exists! Incrementing suffix
NEW NAME </home/test/video/ccc-1.iso> already exists! Incrementing suffix
NEW NAME OK! Renaming to </home/test/video/ccc-2.iso>
----------
Processing INFILE </home/test/video/aaa.img>
NEW NAME OK! Renaming to </home/test/video/aaa.iso>
----------
Processing INFILE </home/test/video/sub1/ggg.img>
NEW NAME </home/test/video/sub1/ggg.iso> already exists! Incrementing suffix
NEW NAME </home/test/video/sub1/ggg-1.iso> already exists! Incrementing suffix
NEW NAME </home/test/video/sub1/ggg-2.iso> already exists! Incrementing suffix
NEW NAME OK! Renaming to </home/test/video/sub1/ggg-3.iso>
----------
Processing INFILE </home/test/video/sub1/fff.img>
NEW NAME </home/test/video/sub1/fff.iso> already exists! Incrementing suffix
NEW NAME OK! Renaming to </home/test/video/sub1/fff-1.iso>
----------
Processing INFILE </home/test/video/sub1/hhh.img>
NEW NAME OK! Renaming to </home/test/video/sub1/hhh.iso>
----------
Processing INFILE </home/test/video/eee.img>
NEW NAME OK! Renaming to </home/test/video/eee.iso>
----------
Processing INFILE </home/test/video/sub2/mmm.img>
NEW NAME OK! Renaming to </home/test/video/sub2/mmm.iso>
----------
Processing INFILE </home/test/video/sub2/iii.img>
NEW NAME OK! Renaming to </home/test/video/sub2/iii.iso>
----------
Processing INFILE </home/test/video/sub2/lll.img>
NEW NAME OK! Renaming to </home/test/video/sub2/lll.iso>
----------
Processing INFILE </home/test/video/bbb.img>
NEW NAME </home/test/video/bbb.iso> already exists! Incrementing suffix
NEW NAME OK! Renaming to </home/test/video/bbb-1.iso>
----------
Processing INFILE </home/test/video/ddd.img>
NEW NAME OK! Renaming to </home/test/video/ddd.iso>
test ~ $ ls -lR /home/test/video
/home/test/video:
total 16
-rw-r--r-- 1 test test 0 Feb 17 12:00 aaa.iso
-rw-r--r-- 1 test test 0 Feb 17 12:00 bbb-1.iso
-rw-r--r-- 1 test test 0 Feb 17 12:00 bbb.iso
-rw-r--r-- 1 test test 0 Feb 17 12:00 ccc-1.iso
-rw-r--r-- 1 test test 0 Feb 17 12:00 ccc-2.iso
-rw-r--r-- 1 test test 0 Feb 17 12:00 ccc.iso
-rw-r--r-- 1 test test 0 Feb 17 12:00 ddd-1.iso
-rw-r--r-- 1 test test 0 Feb 17 12:00 ddd.iso
-rw-r--r-- 1 test test 0 Feb 17 12:00 eee.iso
drwxr-xr-x 2 test test 4096 Feb 17 12:09 sub1
drwxr-xr-x 2 test test 4096 Feb 17 12:09 sub2
/home/test/video/sub1:
total 4
-rw-r--r-- 1 test test 0 Feb 17 12:06 fff-1.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 fff.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 ggg-1.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 ggg-2.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 ggg-3.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 ggg.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 hhh-1.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 hhh.iso
/home/test/video/sub2:
total 4
-rw-r--r-- 1 test test 0 Feb 17 12:06 iii.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 lll.iso
-rw-r--r-- 1 test test 0 Feb 17 12:06 mmm.iso