![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Root Password change | bullz26 | SUN Solaris | 2 | 03-30-2008 11:09 AM |
| How to change ROOT password. | angelofhell | HP-UX | 3 | 02-25-2008 06:44 AM |
| change root password | duke0001 | SUN Solaris | 3 | 02-22-2007 12:10 PM |
| how do i change extension | kswaraj | Shell Programming and Scripting | 2 | 06-28-2004 08:07 PM |
| How to change extension? | prkwan | Shell Programming and Scripting | 4 | 11-16-2002 07:14 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
change file extension from root and subdirectories
Hello, my first post!
I'd appreciate help with this script, I'm new to this. I have a media directory where I want to batch convert image file names from .img to .iso. I've tried but get: $ ./img2iso2.sh ./img2iso2.sh: line 13: syntax error: unexpected end of file ![]() This is my unfinished script: Code:
#!/bin/bash
# Set the video directory here
VIDEO_ROOT_DIRECTORY="/Users/astrid/NFS/scripts/img2iso/";
# Check if the directory exist or not
if [ -d "$VIDEO_ROOT_DIRECTORY" ]; then
cd $VIDEO_ROOT_DIRECTORY
else
print "ERROR: Unable to reach directory - or it does not exist!";
fi
for imgfile do
case $imgfile *.img
do mv $imgfile.img $imgfile.iso;
done
print "img-files renamed to iso";
esac
done
exit 0
Also, I'd think it would be smart to include some sort of check: if there is a file called image.iso and one called image.img in the same (sub)directory; I'd like to change the name of that .img file to image-1.iso. But I don't even know where to start with this check! If I understand the error message there's a problem with quoting? I've made so many trials that I can't remember where I started anymore. Any help is greatly appreciated. EDIT: Searching the forum I found a better way to do the replacement of the file extension (here), but I wasn't so lucky with the subdirs. Last edited by Astrid; 02-15-2008 at 03:43 PM.. |
|
||||
|
Thank you for the quick reply, robotronic!
But I am sorry to say that I could not get it to work. The script runs and then I get prompt again but there is no indication if it goes wrong somewhere, I even tried to change the video root to a dummy directory to invoke some feedback but to no avail. ![]() I've used "touch" to make some dummy files in the specified directory and subdirectories: some with the same name but different extension (.img and .iso, obviously, and some other just to see if they are affected), and some files with different permissions (chmod 777 on most). But not one changed the extension. The script itself is located in one directory level above the "media root" (as set in the script). Code:
$ ls -l img2iso total 0 -rwxrwxrwx 1 astrid 619956085 0 Feb 15 13:01 dummy.img -rw-r--r-- 1 astrid 619956085 0 Feb 15 21:50 dummy.iso -rwxrwxrwx 1 astrid 619956085 0 Feb 15 13:01 example.img -rw-r--r-- 1 astrid 619956085 0 Feb 15 21:35 example_no_permission.img -rwxrwxrwx 1 astrid 619956085 0 Feb 15 13:01 image.img -rw-r--r-- 1 astrid 619956085 0 Feb 15 13:01 isoimg.iso drwxr-xr-x 2 astrid 619956085 68 Feb 15 12:53 level1a drwxr-xr-x 9 astrid 619956085 306 Feb 15 13:00 level1b |
|
|||||
|
Very strange, I've tested the script with some dummy files and it works on my linux box.
Try inserting some echo commands in the script and try to re-run it: Code:
#!/bin/bash
# Set the video directory here
VIDEO_ROOT_DIRECTORY="/Users/astrid/NFS/scripts/img2iso"
for imgfile in `find "$VIDEO_ROOT_DIRECTORY" -name "*.img" -type f`
do
echo "Processing IMGFILE <$imgfile>"
n=0
exit=""
suffix=""
while [ ! "$exit" ]
do
newname=`echo "$imgfile" | sed "s/\.img$/$suffix\.iso/"`
if [ ! -f "$newname" ]
then
echo "NEW NAME OK! Renaming to <$newname>"
mv "$imgfile" "$newname"
exit="y"
else
echo "NEW NAME <$newname> already exists! Incrementing suffix"
n="$((n+1))"
suffix="-$n"
fi
done
|
|
||||
|
$ ./img2iso.sh
#: bad interpreter: No such file or directory and $ sh -x img2iso.sh $ What is more strange: $ cat img2iso.sh done$ exists! Incrementing suffix" I have triple checked that the directory does exist and is spelled correctly with proper case. In the second case nothing happens but a prompt change. No echoes. In the img2iso directory no files have changed extension after running the script. The concatenate I can't even begin to understand. It seems very odd to prompt the last echo like that. I will have to look this over carefully again and again as I'm sure your code is good. I just can't see what I'm missing on my side. I'm on a MacOS X 10.4, perhaps this matters? I use Smultron as editor. Last edited by Astrid; 02-15-2008 at 07:29 PM.. |
|
||||
|
how bout this:
Code:
for file in $(find . -type f -iname '*.img'); do
mv $file ${file/img/iso}
done
Code:
save_ifs=$IFS ; IFS='
'
for file in $(find . -type f -iname '*.img'); do
mv "$file" "${file/img/iso}"
done
IFS=$save_ifs
cheers EDIT: This works for bash, but if I remember correctly the default shell on macosX is bash... |
|
|||||
|
Perhaps a dos2unix or something similiar in Mac.
For your original script Code:
#!/bin/bash
# Set the video directory here
VIDEO_ROOT_DIRECTORY="/Users/astrid/NFS/scripts/img2iso/";
# Check if the directory exist or not
if [ -d "$VIDEO_ROOT_DIRECTORY" ]; then
cd $VIDEO_ROOT_DIRECTORY
else
print "ERROR: Unable to reach directory - or it does not exist!";
exit 1
fi
for imgfile in $(find . -type f -iname "*.img")
do
if [ ! -f ${imgfile%.img}.iso ] ; then
mv ${imgfile} "${imgfile%.img}.iso"
print "[${imgfile} -> ${imgfile%.img}.iso]"
else
mv ${imgfile} "${imgfile%.img}-1.iso"
print "[${imgfile} -> ${imgfile%.img}-1.iso]"
done
exit 0
|
![]() |
| Bookmarks |
| Tags |
| linux, linux commands, unix commands |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|