The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-15-2008
Astrid Astrid is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 4
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
I assume this doesn't check subdirectories, even though I'd like it to. Maybe a combination of find, grep and sed should be used instead? These commands seems to work marvels in almost any situation.

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..
  #2 (permalink)  
Old 02-15-2008
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
This should work for you:

Code:
#!/bin/bash

VIDEO_ROOT_DIRECTORY="/Users/astrid/NFS/scripts/img2iso"

for imgfile in `find "$VIDEO_ROOT_DIRECTORY" -name "*.img" -type f`
do
   n=0
   exit=""
   suffix=""

   while [ ! "$exit" ]
   do
      newname=`echo "$imgfile" | sed "s/\.img$/$suffix\.iso/"`
      if [ ! -f "$newname" ]
      then
         mv "$imgfile" "$newname"
         exit="y"
      else
         n="$((n+1))"
         suffix="-$n"
      fi
   done
done
  #3 (permalink)  
Old 02-15-2008
Astrid Astrid is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 4
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
  #4 (permalink)  
Old 02-15-2008
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
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
You can also run the script with "sh -x img2iso.sh" to trace the execution.
  #5 (permalink)  
Old 02-15-2008
Astrid Astrid is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 4
$ ./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..
  #6 (permalink)  
Old 02-15-2008
fimblo fimblo is offline
Registered User
  
 

Join Date: Feb 2008
Location: stockholm sweden
Posts: 31
how bout this:

Code:
for file in $(find . -type f -iname '*.img'); do
  mv $file ${file/img/iso}
done
This doesnt take filenames nor directories with whitespaces in them, to adapt the above to that, do:

Code:
save_ifs=$IFS ; IFS='
'
for file in $(find . -type f -iname '*.img'); do
  mv "$file" "${file/img/iso}"
done
IFS=$save_ifs
hope this helps
cheers

EDIT: This works for bash, but if I remember correctly the default shell on macosX is bash...
  #7 (permalink)  
Old 02-16-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by Astrid View Post
$ ./img2iso.sh
#: bad interpreter: No such file or directory
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
Not tested tho'.
Closed Thread

Bookmarks

Tags
linux, linux commands, unix commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0