Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2003
    Posts
    4

    Unanswered: Renaming files in Bash - Script Help

    I'm trying to write a script that allow the user to delete the prefix of a file, insert (append) a prefix, rename a prefix and rename the extension. So far all the options but i) work. Any suggestions how I can have bash insert a prefix to an existing file name ex.
    (29.txt to burp29.txt) in the context of this script?

    Thanks Pete

    #!bash
    clear

    echo
    echo -n -e "\033[40;33m RENAME FILES\033[0m" "\033[40;32m$PIXEL_SIZE \033[0m"
    echo
    echo
    echo "******************************************"
    echo "* Choose the correct Spots Method *"
    echo "* ------------------------------- *"
    echo "* *"
    echo "* d)Delete text (burp29.txt to 29.txt) *"
    echo "* *"
    echo "* i)Insert text (29.txt to burp29.txt) *"
    echo "* *"
    echo "* f)rename filetext (29.txt to bur.txt) *"
    echo "* *"
    echo "* e)rename ext (29.txt to 29.xxx) *"
    echo "* *"
    echo "* x)it *"
    echo "* *"
    echo "******************************************"
    echo;echo;echo;echo;echo;echo
    echo
    read job

    ################################################## ################################

    if [ $job = d ]; then
    clear
    ls
    echo
    echo -n -e "\033[40;32m ENTER TEXT TO DELETE \033[0m"
    read DEL
    echo
    null=""
    if [ $DEL = $null ]; then
    echo
    echo
    echo
    echo "ERROR: ENTER TEXT:"
    echo
    echo
    echo
    exit 1
    fi

    array=($DEL*)
    for file in ${array[@]};do
    file1=${file//$DEL/}
    mv $file $file1
    done

    exit 1

    ################################################## ################################

    ################################################## ################################
    elif [ $job = i ]; then
    clear
    ls
    EXT=*.*
    echo
    echo -n -e "\033[40;32m ENTER TEXT TO INSERT \033[0m"
    read INS
    echo
    null=""
    if [ $INS = $null ]; then
    echo
    echo
    echo
    echo "ERROR: ENTER TEXT:"
    echo
    echo
    echo
    exit 1
    fi


    array=($INS)
    for file in ${array[@]};do
    file1=${file//$INS/$EXT/}
    mv $file $file1
    done

    exit 1


    ################################################## ################################
    elif [ $job = f ]; then
    clear
    ls
    echo
    echo -n -e "\033[40;32m ENTER TEXT TO CHANGE \033[0m"
    read TC
    echo
    null=""
    if [ $TC = $null ]; then
    echo
    echo
    echo
    echo "ERROR: ENTER TEXT TO CHANGE:"
    echo
    echo
    echo
    fi

    echo
    echo -n -e "\033[40;32m ENTER TEXT TO CHANGE TO: \033[0m"
    read TW
    echo
    null=""
    if [ $TW = $null ]; then
    echo
    echo
    echo
    echo "ERROR: ENTER TEXT TO CHANGE TO:"
    echo
    echo
    echo
    exit 1
    fi
    array=($TC*)
    for file in ${array[@]};do
    file1=${file//$TC/$TW}
    mv $file $file1
    done

    exit 1

    ################################################## ################################
    elif [ $job = e ]; then
    clear
    ls
    echo
    echo -n -e "\033[40;32m ENTER EXT TO CHANGE (case sensitive): \033[0m"
    read E1
    echo
    null=""
    if [ $E1 = $null ]; then
    echo
    echo
    echo
    echo "ERROR: ENTER EXT TO CHANGE:"
    echo
    echo
    echo
    exit 1
    fi

    echo
    echo -n -e "\033[40;32m ENTER EXT TO CHANGE TO (case sensitive): \033[0m"
    read E2
    echo
    null=""
    if [ $E2 = $null ]; then
    echo
    echo
    echo
    echo "ERROR: ENTER EXT TO CHANGE TO:"
    echo
    echo
    echo
    exit 1
    fi



    array=(*.$E1)
    for file in ${array[@]};do
    file1=${file//$E1/$E2}
    mv $file $file1
    done

    exit 1
    ################################################## ################################
    ################################################## ###############################
    elif [ $job = x ]; then
    echo
    echo -n -e "\033[40;31m RENAME ABORTED \033[0m"
    echo
    echo
    exit 1
    ################################################## ################################
    else
    echo " "
    echo "*** Pick either d, i, f, e or x***"
    echo
    fi


    #EOF
    Last edited by warlord; 10-30-03 at 12:11.

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Lightbulb

    Try this:

    Code:
    
    array=($EXT)
    for file in ${array[@]};do
      file1="${INS}${file}"
      mv $file $file1
    done
    
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  3. #3
    Join Date
    Oct 2003
    Posts
    4
    Nope that didn't seem to work either. Keeps saying Urinary expected or something like that...

  4. #4
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Exclamation

    The error you are getting is unrelated to the code I posted, try reviewing your logic, specially around this statement:

    elif [ $job = i ]; then

    because you have:

    if [ $job = d ]; then
    ...blah...blah...
    exit 1
    fi
    ...more blah....
    exit 1
    elif ...

    It is very doubtfull the job = i statement will ever be executed!

    Also you may want to code your if statements like this:

    if [[ "$job" == "i" ]]; then


    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  5. #5
    Join Date
    Oct 2003
    Posts
    4
    Ok, the problem is I still can't insert any prefixes to files. Just simply then say I have 20 files called 1.tif, 2.tif, 3.tif etc and I wanted to write a simple script to add the prefix (bos) to all the *.tif files as in (bos1.tif, bos2.tif).

    That might be the easiet way for me to work this problem out as I'm no Bash expert here.

    Thanks...

  6. #6
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Exclamation

    Same coding:

    Code:
    
    #!/bin/bash
    INS='bos'
    array=$(ls *.tif)
    for file in ${array[@]};do
      file1="${INS}${file}"
      mv $file $file1
    done
    
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  7. #7
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Originally posted by LKBrwn_DBA [/i]
    Same coding:

    Code:
    
    #!/bin/bash
    INS='bos'
    array=$(ls *.tif)
    for file in ${array[@]};do
      file1="${INS}${file}"
      mv $file $file1
    done
    
    Isn't 'array' just going to be a string in this instance? Using ${array[@]} in that case would simply split the array string where it finds an IFS character and assign each token to 'file'.

    I would try something like this..

    INS=bos
    for file in *.tif; do
    ! test -f $INS$file && mv $file $INS$file || echo Could not rename file $file
    done

    HTH

  8. #8
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Exclamation

    Originally posted by Damian Ibbotson
    Isn't 'array' just going to be a string in this instance? Using ${array[@]} in that case would simply split the array string where it finds an IFS character and assign each token to 'file'.

    I would try something like this..

    INS=bos
    for file in *.tif; do
    ! test -f $INS$file && mv $file $INS$file || echo Could not rename file $file
    done

    HTH
    True, I was only trying to follow the original code posted, in fact, array=$(ls *.tif) is incorrect in my code, it should be array=(ls *.tif) (without the '$')
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  9. #9
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Originally posted by LKBrwn_DBA
    True, I was only trying to follow the original code posted, in fact, array=$(ls *.tif) is incorrect in my code, it should be array=(ls *.tif) (without the '$')
    I've seen that syntax before but I'm not sure which shells it works with. Probably bash!

    I'm stuck with...

    set -A array a b c; echo ${array[@]}

    :-(

  10. #10
    Join Date
    Oct 2003
    Posts
    4
    Ok I'll try that out. My Bash is pretty remedial, but then again I guess so is the simple thing I'm trying to do here. Thanks guys...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •