Create file and then change the extension case.

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Create file and then change the extension case.
# 1  
Old 10-17-2009
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 command line.

Example Input: sh scriptName.sh <file name.extension> <file name.NEWEXTENSION> or vice versa . If the file already exists it should say something.

2. Relevant commands, code, scripts, algorithms:

if [ ! -f $1 ] to check file exists


3. The attempts at a solution (include all code and scripts):

Code:
CreateFile(){
    if [ ! -f $1 ]
    then
        touch $1 > /dev/null 2>&1 && echo "File $1 created"
    else
        echo "Error: $1 file exists!"
    fi

[/SIZE][/FONT][/FONT]  [FONT=Verdana][SIZE=2]OLDEXT=${2/#.}
NEWEXT=${3/#.}

find "${1}" -iname "*.${OLDEXT}" |
while read F
do
  NEWFILE="${F/%${OLDEXT}/${NEWEXT}}"
  echo "mv \"${F}\" \"${NEWFILE}\""
  mv -f "${F}" "${NEWFILE}"
done

Some bitses, but I cant manage to make it all happen together.

4. School (University) and Course Number:
TTU, 1

Last edited by DukeNuke2; 10-17-2009 at 01:42 PM..
# 2  
Old 10-17-2009
Hi Kdenmen,

What happens if you temporarily put extra echo statements after some of the variable assignments, especially those that contain pattern matching operators? Do you get the desired result? Also where does the function "Createfile" end and where does it get to be called? Why do you need the find command if you just need to rename a single file?

S.
# 3  
Old 10-23-2009
I still cant manage, because I am relatively new and never done any scripts.

Maybe someone will help me a bit.

I found this, but I need to get the results I described in my first post.

Thanks ahead.
Code:
#!/bin/bash

usage () {
    echo
    echo "Usage: `basename $0` <search dir> <old ext> <new ext>"
    echo
}
 
if [ -z "$3" ]; then
    usage
    exit 1
fi
 
OLDEXT=${2/#.}
NEWEXT=${3/#.}

find "${1}" -iname "*.${OLDEXT}" |
while read F
do
  NEWFILE="${F/%${OLDEXT}/${NEWEXT}}"
  echo "mv \"${F}\" \"${NEWFILE}\""
  mv -f "${F}" "${NEWFILE}"
done 
 
exit 0

# 4  
Old 10-30-2009
I have to bump my own thread.

Thanks ahead. I made a post to forum, but never got a anwer.

Maybe you know someone who still can help me.

Code:
#!/bin/bash

----------Dont know exactly but my code ---
touch > /tmp/newfilename
umask 077 && mkdir /tmp/tempdir.$$)
exit 2
----------------------------------------

usage () {
    echo
    echo "Usage: `basename $0` <search dir> <old ext> <new ext>"
    echo
}
 
if [ -z "$3" ]; then
    usage
    exit 1
fi
 
OLDEXT=${2/#.}
NEWEXT=${3/#.}

find "${1}" -iname "*.${OLDEXT}" |
while read F
do
  NEWFILE="${F/%${OLDEXT}/${NEWEXT}}"
  echo "mv \"${F}\" \"${NEWFILE}\""
  mv -f "${F}" "${NEWFILE}"
done 
 
exit 0

Basically I would like to type to terminal a line like this:

Code:
sh myshellscriptname.sh temporaryfolder temporaryfile filecase1 filecase2

like : sh script.sh test file.txt txt TXT -> it would make a folder called test a file calldes file.txt and then change .txt to .TXT or vice versa and when i press CTRL + C - then the temporary file and folder will be deleted.
# 5  
Old 11-02-2009
Greetings,
my 2 cents:
I have not much time to understand all you are trying here and I dont use bash...

So my remarks would be more like "If you were using /usr/bin/sh, ..."

1) The beginning:
Code:
#!/usr/bin/sh

#----------Dont know exactly but my code ---  # --- Isnt valid for comments...
touch  ./tmp/newfilename                             # So use <#>
umask 077 && mkdir ./tmp/tempdir.$$             # no ) at the end...
#exit 2                                                     # otherwise your program stops here!
#-----------------------------------------

usage () {
    echo
    echo "Usage: `basename $0` <search dir> <old ext> <new ext>"
    echo
}

if [ -z "$1" ]; then                             #  why $3? what about the other
    usage                                       #  expected arguments ?
    exit 1                                       #  at least now it will show up if you type
fi                                             # nothing more than the programs name...

OLDEXT=$2
NEWEXT=$3

echo  OLDEXT=$OLDEXT                         # when things dont work, display what you loaded
echo NEWEXT=$NEWEXT                         # in order to check you did initialize some VARs
                                              # and have an idea where you are in your code
                                              # at execution...

your main:
Code:
 NEWFILE="${F/%${OLDEXT}/${NEWEXT}}"

Since I dont use bash for me it will be difficult to say what is not working...
In sh though I doubt it can be done that way...

Why are you not using basic sh ( for compliance...)?

So now you have the beginning in working order
Explain me your choice for your while loop (If it should work in bash, Im no help...)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to create sub directories from specific file extension

In the bash below I am trying to create sub-directories inside a directory from files with specific .bam extensions. There may be more then one $RDIR ing the directory and the .bam file(s) are trimmed (removing the extension and IonCode_0000_) and the result is the folder name that is saved in... (2 Replies)
Discussion started by: cmccabe
2 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. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies

4. 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

5. Shell Programming and Scripting

[Solved] Change Upper case to Lower case in C shell

Is there a command that can switch a character variable from UPPER case to lower case? like foreach AC ( ABC BCD PLL QIO) set ac `COMMAND($AC)` ... end Thanks a lot! (3 Replies)
Discussion started by: rockytodd
3 Replies

6. Shell Programming and Scripting

data array needs to change upper case to lower case

Hi all, i have a data array as followes. ARRAY=DFSG345GGG ARRAY=234FDFG090 ARRAY=VDFVGBGHH so on.......... i need all english letters to be change to lower case. So i am expecting to see ARRAY=dfsg345ggg ARRAY=234fdfg090 ARRAY=vdfvgbghh so on........ If i have to copy this data in... (8 Replies)
Discussion started by: usustarr
8 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. Shell Programming and Scripting

How to match the last XML extension by using Case statement

Hi All, I have a status.txt file which contains following three files. 1.xml 2.xml 3.xml Now i have written a shell script 1.sh which contains the following cat status.txt | while read filename do echo $filename case "$filename" in xml) echo "running 1.xml" ;; ... (3 Replies)
Discussion started by: sunitachoudhury
3 Replies

9. Shell Programming and Scripting

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... (10 Replies)
Discussion started by: Astrid
10 Replies

10. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies
Login or Register to Ask a Question