change file extension from root and subdirectories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change file extension from root and subdirectories
# 1  
Old 02-15-2008
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 Smilie

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  
Old 02-15-2008
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  
Old 02-15-2008
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. Smilie

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  
Old 02-15-2008
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  
Old 02-15-2008
$ ./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  
Old 02-15-2008
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  
Old 02-16-2008
Now it's clear why the script doesn't work Smilie

Unfortunately I really don't know anything about the Mac's world, so I can't help you anymore.

The obvious thing about the message "bad interpreter" is that it can't find the bash binary "/bin/bash" defined at the top of the script. If you issue "ls -l /bin/bash" what's the output?

About the "sh -x" thing, on Linux "sh" is a symbolic link to bash and it does work. On Mac I dont'know. To overcome this you can insert a line with the command "set -x" just before the video directory variable assignment. The effect is the same, but before you have to fix the interpreter problem or this is totally useless.

And about the "cat" command, it seems that your script file is a bit messed up.....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Can a root role change the root password in Solaris 10?

i do not have root on a solairs 10 server , however i do have the root role, i was wondering if I can change the root password as a a role with the passwd command? I have not tried yet. and do i have to use the # chgkey -p afterwards? i need to patch is why i am asking. thanks (1 Reply)
Discussion started by: goya
1 Replies

2. Shell Programming and Scripting

Change file extension

Hi Guys, i am trying to redirect a file wherein i need to change the extension of the file from .sh to .tmp, but getting an error a=test.txt sh test.txt > path/$(basename "$a" .sh).tmp i need test.tmp ---------- Post updated at 02:09 AM ---------- Previous update was at... (3 Replies)
Discussion started by: rohit_shinez
3 Replies

3. UNIX for Beginners Questions & Answers

UNIX command to display Owner,Group,Root and Subdirectories list

Hi Team, Am a newbie to Unix. As I would like to see the Server Name,Owner Name ( not numeric form), Group Name ( not numeric ID), ROOT path. I would like to send this list as an attachment to my personal mail. Can any one please help me out to to resolve this . Here is the sample result... (6 Replies)
Discussion started by: vasuvv
6 Replies

4. Ubuntu

Root access that can't change root password?

We are having a little problem on a server. We want that some users should be able to do e.g. sudo and become root, but with the restriction that the user can't change root password. That is, a guarantee that we still can login to that server and become root no matter of what the other users will... (2 Replies)
Discussion started by: 244an
2 Replies

5. Shell Programming and Scripting

List files with *.i extension in a directory and all its subdirectories + 30days old then remove

I need to write a script to : list files with *.i extension in a directory and all its subdirectories + 30days old, save it in a file and then remove (2 Replies)
Discussion started by: lena keung
2 Replies

6. Shell Programming and Scripting

shell script to change the extension of a file

I have a directory that contains several files, out of which some files are have an extra extension for example file1.new.new.new file2.new.new.new file3.new.new.new file4.new.new.new i want to write a shell script that rename all such file with only single extension like file1.new... (7 Replies)
Discussion started by: mukulverma2408
7 Replies

7. UNIX for Dummies Questions & Answers

Shell script to rename or change file extension case.

I searched the forum, but there was different type of rename. Hello. I have files in folder. Like: xxxxxxxx1.html or xxxxxxxx2.txt or xxxxxxxx3.tar.gz and how to rename or change file extension case to xxxxxxxx1.htm or xxxxxxx2.TXT or (5 Replies)
Discussion started by: Sheldon
5 Replies

8. Homework & Coursework Questions

Create file and then change the extension case.

Interpreter should be bash. 1. The problem statement, all variables and given/known data: I need to make a file (myText.txt or song.mp3 or cloud.tar.gz or whatever) and then change the extension to (myText.TXT , song.MP3, cloud.TAR.GZ). It would be good if I can add all information in... (4 Replies)
Discussion started by: Kdenmen
4 Replies

9. Shell Programming and Scripting

how do i change extension

Hi I am writing a script which does an FTP of a set of files onto another machine and then would have to rename the files into a different extension on the source machine. for example if the file being sent via FTP is sample.txt. Once the file has been transferred i would want to modify the... (2 Replies)
Discussion started by: kswaraj
2 Replies

10. Shell Programming and Scripting

How to change extension?

How do you write a shell script that change the extension of all the files? e.g chext rtf doc where .rtf is the original extension and .doc is the new extension is it something to do with basename? do I need a for loop? Please help! Unix SuperNewbie (4 Replies)
Discussion started by: prkwan
4 Replies
Login or Register to Ask a Question