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
# 8  
Old 02-16-2008
Quote:
Originally Posted by Astrid
$ ./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'.
# 9  
Old 02-16-2008
Considering the initial requirement (recursively mv .img to .iso),
with zsh:

Code:
cd /Users/astrid/NFS/scripts/img2iso/
autoload -U zmv # put it in your .zshrc
zmv -n '(**/)(*).img' '$1$2.iso'

If the result is correct
remove the n flag:

Code:
zmv '(**/)(*).img' '$1$2.iso'

Otherwise:

Code:
find /Users/astrid/NFS/scripts/img2iso -name '*.img' -exec sh -c 'f="${1%.img}.iso"
[ ! -f "$f" ]&&mv "$1" "$f"' - {} \;


Last edited by radoulov; 02-16-2008 at 09:09 AM..
# 10  
Old 02-16-2008
With all this help I know I will get it going!
Thank you so much for your attention, it gives a lot of inspiration. I havn't come around to finish the script yet (it's that other life with kids, laundry, office work etc.) but I'll check back when I'm finished, real soon I hope.


By the way, bash seems to be living here:

Code:
$ ls -l /bin/bash
-rwxr-xr-x   1 root  wheel  1068844 Dec 12  2006 /bin/bash


(and the same goes for zsh, nice!)
# 11  
Old 02-17-2008
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

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